Tutorial 12 - Answers
Question 1
(Question 4.1 on page 217 of your textbook). Write a class Die to define die objects. Each Die object has two "instance variables". One is the number of side, the other is the value of current face side. Class Die has a constructor that makes new Die object of 6 sides with the face value as 1.
Write a "instance method" getFaceValue() that returns the current face value of a Die object, and another "instance method" called roll() that returns a randomly generated face value for a Die object.
Design and implement a class called PairOfDice, composed of two Die objects. Write "instance methods" to roll both Die objects and return the total value of the two objects after rolling. In addition, write "instance methods" getDie1() and getDie2() to get the face value of each individual
Die object; method getTotal() to get the total face value of the two die objects.
Create a driver class called BoxCars that has a main method that rolls a PairOfDice object multiple times, counting the number of box cars (two sixes) that occur. a) In your own words (a couple of points) describe what each class is doing.
Die :
Represents one die (singular of dice) with face showing values between 1 and the number of faces on the die (6).
PairOfDice:
Uses two Die objects to roll them both to get the total face value of two die objects.
BoxCars :
Rolls a pair of dice X number of times and count the number of box cars that occurs (that is when both die have a value of “6”). b) For the first two classes, list and describe the Instance Variables and Instance Methods, and define the Constructor for each class. For the driver class, list the Instance Variables needed in the “main” method and write the pseudocode to make the dice game work.
Class Name
Instance Variables
(you should have two
variables)
Instance Methods
(you should have
two methods)
Die int numFaces //number of sides on a die int faceValue //the current face value on the die int getFaceValue() //returns the current face value on the die int roll() //returns a randomly generated face value between 1-6
Constructor
(Note: won’t take in parameters, but will initialize variables)
Die()
//initialize the variables
numFaces=6; //6 sides to a die
faceValue=1; //set the initial faceValue to 1
1
Class Name
Instance Variables
(you should have 5
variables)
PairOfDice
//need two Die objects – since rolling two die
Die die1 //first Die object
Die die2 //second Die object int value1 //the face value of die1 int value2 //the face value of die2 int total //represents the total of both Die objects(value1 + value2)
Instance Methods
(you should have 3
methods)
Constructor
(Note: won’t take in parameters, but will initialize variables) int getDie1() //returns the face value of die1 (calls its own class) int getDie2()//returns the face value of die2 int getTotal() //returns the sum of both die values int roll() //returns the value of rolling both die
PairOfDice()
die1 = new Die() //create a new Die object
die2 = new Die() //create a new Die object
value1 = 1 //set die1 face value to 1
value2 = 1 //set die2 face value to 1
total = value1+value2 //sum the two face values
Class Name BoxCars
Instance Variables final int ROLLS =1000 //roll dice 1000 times final int TARGET =12 //looking for two rolled sixes (6+6=12) int total //keep track of current rolled dice total face values int count //keep track of what current roll are at (i.e. 10 of 1000)
Set Variables: Pseudocode for
Main ROLLS=1000, TARGET=12, total=0, count=0
Main method:
Create a set of dice (PairOfDice) //made up of two Die objects
then for 1000 times do
roll the set of dice (call roll method)
add up face value of each die total
if total = = TARGET
count ++
print the Number of Rolls
print the Total Number of Box Cars (count) c) Write the classes (fill in the blanks with your names for the classes, variables and methods):
//First class: Die Represents one die (single die) with six sides where the face
//value will show a number between 1 and 6. public class Die {
//instance variables – remember instance variables should be private
private int numFaces; // number of sides on the die
private int faceValue; // current value showing on the die
//Construct a six-sided die, initially showing 1.
public Die () {
2
numFaces = 6;
faceValue = 1;
}
//Method to roll the die and return the result.
public int roll () {
faceValue = (int) (Math.random() * numFaces) + 1;
return faceValue;
}
//Method to return the current die value.
public int getFaceValue () {
return faceValue;
}
}//end class
//Class 2: PairOfDice This uses two “Die” objects. It rolls them to get the total face
//value of the two die objects import Die;
public class PairOfDice {
//instance variables
private Die die1, die2;
private int value1, value2, total;
//Construct two Die objects, both with an initial face value of one.
public PairOfDice () {
die1 = new Die();
die2 = new Die();
value1 = 1;
value2 = 1;
total = value1 + value2;
}
//Method to roll both dice and return the combined result.
public int roll () {
value1 = die1.roll();
value2 = die2.roll();
total = value1 + value2;
return total;
}
//Method to returns the current combined dice total.
public int getTotal () {
return total;
}
3
//Method to returns the current value of the first die.
public int getDie1 () {
return value1;
}
//Method to returns the current value of the second die.
public int getDie2 () {
return value2;
}
}//end class
//Class 3: BoxCars This class rolls a pair of dice 1000 times, counting the number
//of box cars (two ‘6’) that occur import PairOfDice;
public class BoxCars {
public static void main (String[] args) {
final int ROLLS = 1000, TARGET = 12;
int total, count = 0;
PairOfDice dice = new PairOfDice();
for (int roll=1; roll <= ROLLS; roll++) {
total = dice.roll();
if (total == TARGET)
count++;
}
System.out.println ("Number of rolls: " + ROLLS);
System.out.println ("Number of Box Cars: " + count);
}//end main
} //end class
Output
For instance if in 1000 rolls, you got 23 box cars (both die having the value six), the output would be:
Number of Rolls: 1000
Number of Box Cars: 23
4