The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 408 Weeks Ago, 1 Day Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
Task
Write the following programs, each in a separate file. Filenames should be:
A????1Â Â Â Dice.java
A????1Â Â Â Primes.java
A????1Â Â Â DiceStats.java
Exercise 1
Filename: Dice.java
1. Write a method called rollDice that returns the result from rolling two standard six-sided dice)
o The method will take no parameters, but it will return the resulting total
of the dice roll as an integer
o Remember that the rolling of a die invol ves randomness, so you'll need to use the random number generation library discussed in class
o Note that rolling two 6-sided dice is NOT equivalent to picking a
random number from 2 through 12. This is because when you roll 2 dice, there is a much larger change of hitting some totals than others. You
need to roll two dice and add them together
2. To test this method, write a main() routine that does the following:
o Ask the user to enter how many times they want to roll the dice, and let them enter a value (for this writeup, I'll call it N)
o Call the rollDice method this many times (i.e. N times), and count how many times a total of 2 (known as "Snake Eyes") appears, as well as how many times a total of 7 appears.
o For each of these totals (2 and 7), print out how many times that total appeared, as well as what percentage this is of the total number of rolls. Percentages should be printed to 2 decimal places.
3. Note: When you declare the Random object, instead of putting it inside of main(), declare this variable inside the class block, but NOT inside of either of the methods. Declare it to be static. This way, it will be accessible to both
your rollDice method, as well as to main
Sample Run 1
(user input underlined)
How many times would you like to roll the two dice? 10000
Snake eyes (double 1s) appeared
268 times
2.68 % of the time
A roll of 7 appeared
1675 times
16.75 % of the time
Sample Run 2
(user input underlined)
How many times would you like to roll the two dice? 500000
Snake eyes (double 1s) appeared
13706 times
2.74 % of the time
A roll of 7 appeared
83300 times
16.66 % of the time
Sample Run 3
(user input underlined)
How many times would you like to roll the two dice? 500000
Snake eyes (double 1s) appeared
13921 times
2.78 % of the time
A roll of 7 appeared
83789 times
16.76 % of the time
Exercise 2
Filename: Primes.java
1. Recall that an integer is a prime number if it is divisible only by 1 and itself.
Write a method called isPrime that takes in one integer parameter X and
determines whether or not X is prime. The method should return a boolean value:
o return true if X is a prime number
o return false if X is not a prime number.
(Hint: The % operator is good for checking for divisibility of one integer by another)
2. To test this method, write a main() routine that asks the user to input a positive integer N. Using your isPrime() method, find and print all the prime numbers less than or equal to N, where the output has 8 numbers per line (you can use tab characters to separate numbers on a line).
Sample Run
(user input underlined)
Â
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Please input a positive number: 500
The
2
23
59
97
137
179
227
269
313
367
419
461
Requirements for each program
A????1 The required tasks must be performed with the methods specified (not just with a single main() routine)
o Note that each exercise requires the writing of a method, and a main routine to test that method.
o Each method should do exactly the task specified in item 1 of each exercise
o Item 2 of each exercise specifies what to do in main() -- this will always
involve CALLING the method, sometimes more than once
o Note that there is NO keyboard-input/screen-output specified in the
methods themselves (i.e. "return" does not mean "print") -- this means you should NOT have any print statements or Scanner usage inside these named methods. Any printing and/or keyboard input is done by main() in each exercise
A????1Â Â Â When you write source code, it should be readable and well-documented.
Exercise 3
Filename: DiceStats.java
This is based on Exercise 7.17 from the textbook, and it expands on the Dice exercise
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). o Hint: The idea of this array is to be a "frequency array", like in example
7.7 from the book
o Hint: How many counters will you need? Well, the lowest possible total is the number of dice rolled (all 1s). The highest possible total is all 6s,
so (6 * number_of_dice). Use this information to decide on the size of your frequency array.
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
Requirements
A????1Â Â Â Use the Scanner class for input
A????1Â Â Â Use the Random class for random number generation
A????1Â Â Â When you write source code, it should be readable and well-documented.
A????1Â Â Â Declare methods to be public and static
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------acq-----------uis-----------iti-----------on -----------of -----------my -----------pos-----------ted----------- so-----------lut-----------ion-----------.Pl-----------eas-----------e p-----------ing----------- me----------- on-----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be-----------