Question
This programming project will continue your experience with a class that models a real world entity of a single Die as originally described in Chapter
This programming project will continue your experience with a class that models a real world entity of a single Die as originally described in Chapter 6. It will extend your knowledge of Classes with the coding of both a toString() and
an equals() method. In order to build the application program that will solve this problem, you will need to utilize your previous knowledge of decision statements, loop statements and arrays.
Problem Description: This problem requires the development of a class, called Die, which will model a single die object from a pair of Dice. This Die object was minimally introduced to you in Chapter 6, pg. 357. You will extend this initial Die definition with several additional methods that will enable the Die objects to do more.
This problem also requires that you develop a client class (a program called DieRollStatistics) that uses a couple of Die objects. The program will obtain user, keyboard input as to the number of times the Die objects are to be rolled. This program will then roll a single Die object and accumulate statistics as to the number of times each face value occurs. And finally the program will roll two Die objects (a pair of Dice) and accumulate statistics as to the number of times that the two Die objects produce doubles, that is both Die objects roll to the same face value (i.e. 1 and 1 or 2 and 2 or 3 and 3 or so on). The program will also include in the statistics the number of times each face value occurs in a double roll.
To begin, you will need to write a Die class as defined in the following UML diagram.
Die Class Requirements: The Die class has two data attribute fields of information. One data attribute field holds the number of sides of the Die object and the other data attribute field holds the numeric value displayed by the side that is face up on the Die object.
The Die class has a single parameterized constructor method, which accepts the count of sides from its instantiation statement that is written in the application (demo/driver) program. The constructor method insures that the count of sides that is provided through the parameter is equal to or more than 2, which is the minimum number of sides that a Die object can have. If the number of sides is < 2, then the constructor sets the sides variable to a default value of 6. This constructor method also calls the roll method to obtain a random number to indicate what side of the Die object is facing up.
Die |
- sides : int - value : int |
+ Die(numSides : int) + roll( ) : void + getSides( ) : int + getValue( ) : int + toString( ) : String + equals(object2 : Die) : boolean |
Page 1 of 5
The functionality of the other methods in this class are briefly described below:
roll() Method: The roll() method simulates the rolling of a Die object by utilizing a Random object that produces a random integer between 1 and 6 (HINT: Random class nextInt(n-1) method in Chapter 04 Sec. 4.11). The result of the randomly generated face up side will need to be stored in the value field.
getSides() Method: This method returns the number of sides for a Die object.
getValue() Method: This method returns the value or face up side for a Die object.
toString() Method: This method returns a String representation of the Dies face up side. This will require a conversion from the numeric integer data type to a String data type. You may accomplish this with the following command:
String strValue = Integer.toString(value);
equals() Method:
This method compares two Die objects for equality, which is defined as both Die objects have the same face up side value. If the two Die objects are equal, then this method returns a true Boolean value, else it returns a false.
DieRollStatistics Program Requirements: The processing done by this DieRollStatistics application program requires two code segments. The first code segment accumulates the roll statistics for a single Die object and outputs those statistics in a simple report. The second code segment accumulates the roll statistics for two Die objects and outputs those statistics in a simple report.
The overall general functionality of this application program is as follows:
The program must define a constant number of 6 sides for the Die objects.
The program must define a Scanner object, two Die objects, and an integer array
object that has slots or elements that represent each side of a Die object (i.e. if the Die object has 6 sides, then the array has a size of 6 elements or slots, with each element or slot representing the value on each side of the Die.). For example: array position 0 represents or is associated to the side that contains just a single dimple or what we might call side 1; whereas array position 5 represents the side that contains 6 dimples or what would be called side 6 of the Die object.
The program must prompt for, obtain and validate keyboard entry of the number of times the Die objects are to be rolled. A valid number of rolls for a Die object must be 1 or more.
Page 2 of 5
The basic functionality of the first task in this application program is as follows:
The program must have a single Die object complete the number of rolls that were
entered by the user. Each roll iteration must output the side that resulted in being face up after the roll and it must also increment the correct counter in the array for the side that was rolled to face up. Here below is an example of the output:
Die one: 5
When all roll iterations have been completed, then the program will output a simple report of the number of times each side of the Die object was rolled to face up. Remember this is being determined by random number generation, so each execution of the program will produce just a little different statistics on the report. See simple statistics report in example execution output at the bottom of these instructions.
The basic functionality of the second task in this application program is as follows:
The program must have both Die objects complete the number of rolls that were
entered by the user.
Each roll iteration must have one of the Die objects determine if it holds the same
value (is equal) to the second Die object.
If the two Die objects are equal, then a single variable that holds the count of a
doubles roll must be incremented by 1. Additionally, the count of times this side has been rolled face up needs to be incremented by 1 (Hint: remember the array elements are serving as the counters for each of the sides of the Die object).
Each roll iteration must output the side of each Die object that resulted in being face up. Additionally, if the sides were equal then add to the output line that a double was rolled. Here below are examples of the two outputs:
Die one: 5, Die Two: 6 Die one: 6, Die Two: 6 - Doubles rolled
After all of the second set of iterations has finished executing, there should be output statements to produce a simple report of the number of doubles rolled and the number of times/occurrences that each face value was the value rolled in a doubles roll. See statistics report in example execution output at the bottom of these instructions.
Technical Coding Requirements: Your DieRollStatistics program and Die class must demonstrate the following coding techniques:
The Die class must demonstrate all of the methods as specified in the UML diagram.
The Die class must use the Random class to create a random number in the range of 1 to 6 (Note: 1 and 6 are included).
Page 3 of 5
The Die class must include an equals method that is used by the application program to determine if two Die objects are equal (i.e. have the same face value).
The Die class must include a toString method that is used by the application program to display the numeric value of the side that is face up on the Die object.
The DieRollStatistics application program may use either an explicit or implicit toString method call when it needs to print out the side up value of the Die object.
The DieRollStatistics application program must use an integer array to accumulate the number of times each side of the Die objects is rolled to face up.
The DieRollStatistics application program must use variables appropriately and
not unnecessarily define variables, which are either not used or used inefficiently
in the program.
The DieRollStatistics application program must validate its input of number of
Die rolls to be greater than or equal to 1 (one), that is, less than 1 is an input error.
The DieRollStatistics application program must use for loop statements to
implement the repetitive roll operations and the array processing operations in this
project
The DieRollStatistics application program must utilize printf formatted output
statements to display the statistics and it must utilize array index adjustments to correctly display the sides of the Die objects from 1 through 6 not 0 through 5.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started