A Java Course Outline
Using the Java By Dissection book
by Ira Pohl and Charlie McDowell


Homework 4 - Using Random Numbers

(Most of this is done and explained in Chapter 4 of Java by Dissection)

We will write a program that will be a number of methods and will use tio for terminal input. The program will also get us to use random numbers.

In chapter four you will find a method isAllHeads(). Modify this method as stated in exercise 8 to compute almostAllHeads(int numberOfTrials, int numberOfSuccesses) which returns true if at least numberOfSuccesses out of numberOfTrials heads are tossed.

Allow the user to select a number of trials, the number of coins to be tossed and how many heads is acceptable. So the user can say I want 100 trials where 10 coins are tossed and at least 6 must turn up heads. The program can obtain these values in main using readInt() from the tio package. The program should be able to compute a probability for this experiment.

Write a method that outputs your results in a legible manner. It might print out:

 For 100 trials of tossing ten coins getting at least 6 heads occurs with probability 0.367.

The 100 trials can be performed by a loop. A summing variable that tracks how often IsAllHeads returns true would keep the count of success and then success trials (remember make sure this is done as a double - how?) would give you the answer.

So main will do the input using tio. Don't forget to prompt the user for each input. Then main will call the other methods to perform the computation and the properly formatted output.


Start by writing out a fake "session" with the computer. An example of this would be:
Hello, ready to sample the random number generator!
Please enter a number of trials: 100
Please enter number of coins to toss at each trial: 10
Please enter the number of heads (out of 10) for a success: 6
Out of 100 trials, we had 32 successes, that's 32%.
Please enter a number of trials: 0
All done, goodbye!

If you can do this, it will make things easier. From what I wrote, can you figure out how many variables that will be input? In what order they should be asked for? When should you do the actual calculation? After reading in all of the numbers or in between? What variables will you want to output? What should we name the variables? Make a habit of doing this, and it will save you hours of grief...

Extra Credit

Use the Counter class to extend your Coin Toss Program.

Use the counter class to keep track of how many heads have been tossed. Create a static counter, and call click for every head. When you have completed a trial, increment another static counter for every success. At the end, read the counter to come up with your answer. Now, can you extend the counter class so that it will do more of the work for you?

Write your own class that will operate as a die.

Write a method for roll which returns an integer between 1 and 6. Create a contructor that will take an integer as an argument indicating how many dice to roll each roll (so you can roll two dice each time if you like). Have a variable that keeps a running total sum of all the rolls by that dice object so far. With the ability to reset the total. Use your dice class to create a new class that will act as a craps game: it will have your main method, and it will create your dice objects to run the game.

Feel free to report any site problems to the Webmaster, Debra Dolsberry.