A
Java Course Outline
Using the Java By Dissection
book
by Ira
Pohl and
Charlie McDowell
We will write a program that will be a number of methods to compute the probability that two people at a party have the same sign. The program will also get us to use random numbers. There are 12 signs of the zodiac: listed below. For example, in a party of 12 or more people the probability that two people have the same sign is 1. We want to know this probability for n =2, 3, 4, 5, ..., 11.
We will compute the sign of each party goer using the
expression Math.random()*12
. This yields a number between 0 and
11. Cast this expression to int
and you have the basis for a switch
statement of the form:
switch( (int)(Math.random()*12)) { case 0: leo++; break; case 1: cancer++; break; //other cases and default if desired }
Initially each sign such as leo is an int
variable
of value zero. If there are 5 people in the party we would compute using a loop
the sign of each of the five people. The condition that more than 2 people in
the room have the same sign should yield true if any sign variable such as leo
>= 2
. A method isSameSign(int people)
should be
used to yield a boolean based on 1 trial. Then using 100 trials for each n
( 2 to 11) would give you the estimated probabilities.
Write a method that outputs your results in a legible
manner. It might print out:
For 100 trials of 2 people the probability of the same sign is 0.0831 For 100 trials of 5 people the probability of the same sign is 0.767. For 100 trials of 11 people the probability of the same sign is 0.967.
The Signs of the Zodiac |
|
Aries | March 21-April 19 |
Taurus | April 20-May 2 |
Gemini | May 21-June 20 |
Cancer | June 21-July 22 |
Leo | July 23- Aug. 22 |
Virgo | Aug. 23-Sept. 22 |
Libra | Sept. 23- Oct. 22 |
Scorpio | Oct. 23- Nov. 21 |
Sagittarius | Nov. 22-Dec. 21 |
Capricorn | Dec. 22-Jan. 19 |
Aquarius | Jan. 20-Feb 18 |
Pisces | Feb. 19-March 20 |
Write out your methods in pseudocode. Make sure each method works independently of the entire program. Work out one case first, such as n = 5. After doing one case right it should be easy to write a loop that generates all 10 cases (n = 2, ne =11).
Leverage the work you have already done with Homework 4 to do this one. And his idea of getting a small piece of it working at a time is a really good one. Write and test a subset of the total program. When that is working, extend it to do the next thing... and so on until you have a good working program.
z
for zodiac sign)