Sunteți pe pagina 1din 5

CS 202 Programming Project 8

CoinTossSimulator, FlipRace & Coin Class


This programming exercise will continue your experience working with the concept of a
Class that models a real world entity. It will also extend your knowledge of Classes with
the coding and use of constructor methods, various service methods, and the toString()
method. It will also provide you an opportunity to see a single Class be reused in two
separate programs.

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:

Coin Class Requirements:


This Coin class only has five fields of
Coin
information. One data attribute indicates what
- HEADS : int = 0
side of the coin is currently facing up. Notice in
- TAILS : int = 1
the UML diagram that this data is an integer data
- sideUp : int
type. This integer value will hold the values of 0
- headsCnt : int
(zero) for heads or 1 (one) for tails. Two
- tailsCnt : int
other data attributes are integer counter values
+ Coin()
that hold the amount of times a coin objects
+ Coin(side : int)
head or tail side has appeared face up after a
+ toss() : void
simulated toss. There are also two named
+ getSideUp() : int
constants, which hold the integer values that
+ getSideCnt(side : int) : int
represents heads and tails.
+ resetSideCnts() : void
+ isHeads() : boolean
This class has two constructor methods and six
+ toString() : String
general service methods. The functionality of
these methods are the following:

No-Arg Constructor Method:


The no-arg constructor for this class will initialize the Coin object setting the counter
fields to 0 and the sideUp field to a default value of 0 for heads.

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.

CoinToss Simulator Program Requirements:


The program that uses the Coin class and that simulates a series of coin tosses is called,
CoinTossSimulator. The functionality of this simulation program is the following:
The program needs to create an instance/object of the Coin class. The object can
be created either with the no-arg or the parameterized constructor.
The program will display what side is up initially.
The program should perform a series of 20 coin tosses (Hint: Think about loops).

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).

See Example Program Execution on the last couple of pages of this


document.

FlipRace Program Requirements:


This program will utilize the Coin class to create two Coin objects. With those two
Coin objects, the program will repeatedly invoke the Coin objects toss method to
simulate flipping the coin. This simulated flipping of the coins will continue until one or
both of the Coin objects have flipped to a heads position three consecutive times.

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:

Coin 1: Tails Coin 2: Tails

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.

CoinTossSimulator Example Execution:


The side initially facing up: heads
Now I will toss the coin 20 times.
Toss: heads
Toss: tails
Toss: tails
Toss: tails Remember that the results of the
Toss: tails simulated flip are random, so
Toss: heads your execution results will not
Toss: heads
Toss: heads be exactly as shown here.
Toss: heads
Toss: heads
Toss: tails
Toss: tails
Toss: tails
Toss: tails
Toss: tails
Toss: heads
Toss: tails
Toss: heads
Toss: tails
Toss: heads
Heads occurred: 9
Tails occurred: 11

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

S-ar putea să vă placă și