Assignment #1

Due: Thurs, Sept 12

Objective

This assignment is intended to help you gain practice with basic Java syntax, including basic procedural programming, functions, arrays, and console I/O.

Task

Do the following three exercises, each in a different file. (These are based on textbook exercises in chapters 5, 6, and 7).



Your filenames should be

To do the console input for these exercises, use the java.util.Scanner class. For random numbers in the last exercise, you can use class java.util.Random.

Declare any methods you write to be public and static. You may also use the java.lang.Math class in these programs, where needed. You may assume correct type of user input in these problems.


Exercise 1

Filename: Pi.java

Based on Exercise 5.20

Calculate the value of pi from the infinite series

 pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...
Print a table that shows the value of pi approximated by computing one term of this series, by two terms, by three terms, and so on. Use default precision for output (i.e. do not set any decimal precision).

Start by asking the user how many terms of the series to compute to, and then let the user enter the information. Use this to print a table of the first N terms of the series (where N is the data entered by the user). Assume the user's input will be a non-negative integer. (see sample output)
 

Sample Run

(Sample user input is underlined)
 Exercise 5.20: 'Approximating PI'
 Compute to how many terms of the series? 20
 terms   PI approximation
 1       4.0
 2       2.666666666666667
 3       3.466666666666667
 4       2.8952380952380956
 5       3.3396825396825403
 6       2.9760461760461765
 7       3.2837384837384844
 8       3.017071817071818
 9       3.2523659347188767
 10      3.0418396189294032
 11      3.232315809405594
 12      3.058402765927333
 13      3.2184027659273333
 14      3.0702546177791854
 15      3.208185652261944
 16      3.079153394197428
 17      3.200365515409549
 18      3.0860798011238346
 19      3.1941879092319425
 20      3.09162380666784

Exercise 2

Filename: Reverse.java

Based on Exercise 6.26

Write a method called reverseDigits that takes a long integer value and returns that number with its digits reversed. For example, given the value 7631, the method should return the value 1367 (as a long integer).
Note: This is a math exercise, NOT a string exercise. Do not use class string here.

Write a main() routing that enters a loop, in which the user is prompted and allowed to enter any long integer (0 to exit the loop), and the reverseDigits method is used to compute and return the reversed number. Print this from the main routine. See sample run below.

Sample Run

(Sample user input is underlined)
 Please enter a long integer (0 to quit): 123456

 The number reversed is: 654321

 Please enter a long integer (0 to quit): 8473625145

 The number reversed is: 5415263748

 Please enter a long integer (0 to quit): 23456789

 The number reversed is: 98765432

 Please enter a long integer (0 to quit): 123456789012345678

 The number reversed is: 876543210987654321

 Please enter a long integer (0 to quit): 0

 Goodbye!

Exercise 3

Filename: DiceStats.java

Based on Exercise 7.17 from the textbook, but expanded

Write a program that does the following:

  1. Ask the user to enter how many dice will constitute one roll. (This is based on the idea that different games require different numbers of rolls for a "turn". For example, in Yahtzee, you roll 5 dice. In craps, you roll 2 dice. Etc.)
  2. Ask the user to enter how many rolls they would like to perform
  3. Create and use an array to keep track of how many times each possible dice sum appears. (Essentially, you need a bunch of counters, but how many will depend on how many dice are rolled per "turn". For example, 2 dice can come up with a total of 2 through 12. But 3 dice could yield a total of 3 through 18).
  4. Use a loop to roll the specified number of dice the desired number of times (and calculate the sum of each dice roll). Use the array to count and track the number of times each possible sum appears
  5. Display the results in a table with 3 columns:
    1. the die total
    2. the number of times that total appeared
    3. the percentage of the total rolls that this sum appeared
    Compute and print the percentage to 2 decimal places.

Sample Run 1

(user input is underlined)
How many dice will constitute one roll? 2
How many rolls? 100000

Sum     # of times      Percentage

2       2799            2.80 %
3       5568            5.57 %
4       8261            8.26 %
5       10970           10.97 %
6       13830           13.83 %
7       16862           16.86 %
8       13895           13.90 %
9       11073           11.07 %
10      8391            8.39 %
11      5576            5.58 %
12      2775            2.78 %

Sample Run 2

(user input is underlined)
How many dice will constitute one roll? 4
How many rolls? 100000

Sum     # of times      Percentage

4       78              0.08 %
5       291             0.29 %
6       756             0.76 %
7       1490            1.49 %
8       2710            2.71 %
9       4268            4.27 %
10      6309            6.31 %
11      8060            8.06 %
12      9696            9.70 %
13      10866           10.87 %
14      11132           11.13 %
15      10847           10.85 %
16      9649            9.65 % 
17      7963            7.96 % 
18      6166            6.17 % 
19      4331            4.33 %
20      2657            2.66 %
21      1558            1.56 %
22      778             0.78 %
23      309             0.31 %
24      86              0.09 %

Compiling

Remember that the compile command is "javac", at the unix command prompt. Compile your code on linprog.cs.fsu.edu, and then run your programs with the "java" command.

Preparing for Submission:

Pack up your files into a single jar-file on linprog.cs.fsu.edu with the jar utility. Here's the command:
  jar cvf hw1.jar Pi.java Reverse.java DiceStats.java
Or, if you have all your .java files for this one in a single directory (and no other .java files in that location) :
  jar cvf hw1.jar *.java

Submitting

I've created a Canvas submission link for the assignment. On the Canvas course link, select "Assignments" from the menu, then click the "HW 1" link to get to the submission page.