Do the following three exercises, each in a different file. (These are based on textbook exercises in chapters 5, 6, and 7).
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.
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)
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
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.
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!
Based on Exercise 7.17 from the textbook, but expanded
Write a program that does the following:
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 %
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 %
jar cvf hw1.jar Pi.java Reverse.java DiceStats.javaOr, 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