Documente Academic
Documente Profesional
Documente Cultură
Problem Description:
This problem requires the development of a class, called Coin, that will simulate a
monetary coin that can be tossed to produce either heads or tails face up situations.
In addition, two programs will need to be created, each of which uses the Coin class. The
first program is called CoinTossSimulator. This program will utilize the Coin class to
create a coin object and then use that coin object to perform a simulation of 20 tosses of
the coin. It will report how many times heads or tails was the side up after the toss. The
second program is called FlipRace. It is a program that creates two coin objects. It
repeatedly tosses both coin objects and the first coin that flips 3 consecutive heads
results will be the winner of the race.
To begin, you will need to write a Coin class as defined in the following UML diagram:
Page 1 of 5
Parameterized Constructor Method:
The class also has a parameterized constructor that sets the counter values to 0 and the
sideUp to the integer value that is provided through the parameter. In this parameterized
constructor, the parameter value needs to be validated to insure that it is either 0 for
heads or 1 for tails. If it is not, then the sideUp value will be set to heads by
default.
toss() Method:
The toss() method simulates the tossing of the coin by utilizing a Random object that
produces a random integer between 0 and 1 (HINT: Random class nextInt(n-1) method
in Chapter 04 Sec. 4.11). The random number of 0 represents heads and the random
number of 1 represents tails. The result of the randomly generated coin face up value
will need to be stored in the sideUp field. In addition, the appropriate side up counter
field value will need to be incremented.
isHeads() Method:
This method returns the Boolean TRUE value if the sideUp is heads and FALSE
otherwise.
getSideUp() Method:
This method returns the integer value stored in the sideUP field.
getSideCnt() Method:
This method returns the integer amount from the side counter field based upon which side
is requested via the parameter integer value. For example: if the parameter value is 0 for
heads then the heads counter field value is returned. The parameter value needs to be
validated to be either 0 for heads or 1 for tails; otherwise, this method needs to return
a -1 (negative 1).
resetSideCnts() Method:
This method assigns the heads and tails sideUp counter variables back to a starting value
of 0.
toString() Method:
This method returns a String representation of the Coin object. If the Coin object has a
sideUp value of 0 (zero) then the String Heads is returned; otherwise the String Tails
is returned.
Page 2 of 5
With each coin toss the coin objects sideUp field should be displayed and the
appropriate sideUp counter field should be incremented by 1.
After the 20 coin tosses, the program should output the total number of times
heads and tails came up by asking the coin object for these counts (Hint:
look at the purpose of the getSideCnt method).
The FlipRace program after each call to the Coin objects flip method will need to
call the Coin objects toString method to display the results of the toss action as shown
here below:
The program will also need to call the Coin objects isHeads method to determine
whether each Coin object flipped to a heads or tails position. If a Coin flipped to a
heads position, then a FlipRace program counter variable for the Coin object will need
to be incremented by 1; however, if the Coin object flipped to a tails position, then the
FlipRace program counter variable counter for the Coin object will need to be reset to a
0.
After one or both Coin objects have completed three consecutive flip actions that
produced a head result, then the repetitive flipping of the Coin objects will stop. After
the flipping of the coins has stopped, then the program must determine which Coin
object reached the 3 heads goal first or if both Coin objects reached the goal at the
same time which produces a tie.
After the goal of three consecutive head flip results has been reached, then the program
must print out which Coin object won or whether it was a tie game. The game results
should be shown with one of the following output strings:
Coin 1 Wins!
Or Coin 2 Wins!
Or Its a TIE!
Page 3 of 5
Coding Requirements:
Dont forget to include Documentation comments (general header, variable and
method documenting in both the programs and the class).
You will need to utilize the Random class to generate the integer random numbers
of 0 and 1 to simulate the heads and tails sides of the coin. This will also
require you to restrict the random number generated to the range of 0 and 1. This
may require you to go back and review the Random class methods and how they
work.
Submission Requirements:
When you have completed your development, testing and commenting of the Java class
and the Java programs within this Java project, then export the project as an
archived/zipped file that is named your last name, first name and the assignment name
(For Example: LastnameFirstnameProgAssign08). Return to the Desire2Learn(D2L)
assignment item and complete the submission steps to upload and submit your
archived/zipped file via the D2L assignment item.
Page 4 of 5
FlipRace Example Execution:
Several examples of the output that is produced from your FlipRace client program:
Execution 1 Results:
Coin 1: Tails Coin 2: Tails
Coin 1: Heads Coin 2: Heads
Coin 1: Heads Coin 2: Heads Notice with the bolding and
Coin 1: Heads Coin 2: Tails yellow highlighting that has
Coin 1 Wins! been added in these instructions
that there are 3 consecutive
Execution 2 Results: results of the Coin object
Coin 1: Heads Coin 2: Heads flipping to Heads.
Coin 1: Tails Coin 2: Heads
Coin 1: Heads Coin 2: Heads
Coin 2 Wins! Remember that the results of the
simulated flip are random, so
Execution 3 Results: your first 4 execution results
Coin 1: Tails Coin 2: Tails will not be exactly as shown
Coin 1: Tails Coin 2: Tails here.
Coin 1: Tails Coin 2: Heads
Coin 1: Heads Coin 2: Tails
Coin 1: Tails Coin 2: Heads
Coin 1: Tails Coin 2: Tails
Coin 1: Tails Coin 2: Heads
Coin 1: Tails Coin 2: Heads
Coin 1: Tails Coin 2: Heads
Coin 2 Wins!
Execution 4 Results:
Coin 1: Tails Coin 2: Tails
Coin 1: Heads Coin 2: Heads
Coin 1: Heads Coin 2: Heads
Coin 1: Heads Coin 2: Heads
Its a TIE!
Page 5 of 5