Tutorial 12

advertisement
Name: _____________________________
Student ID:____________________
Tutorial 12
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:
PairOfDice:
BoxCars:
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. Note: the instance
variables for Class Die have been done for you.
Class Name
Die
Instance Variables int numFaces //number of sides on a die
(you should have two int faceValue //the current face value on the die
variables)
Instance Methods
(you should have
two methods)
1
Name: _____________________________
Student ID:____________________
Constructor
(Note: won’t take in
parameters, but will
initialize variables)
Class Name
PairOfDice
Instance Variables
(you should have 5
variables)
Instance Methods
(you should have 3
methods)
Constructor
(Note: won’t take in
parameters, but will
initialize variables)
Class Name
BoxCars
Instance Variables
Pseudocode for
Main
c) Write the classes (fill in the blanks with your names for the 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 ____ _______________; //
2
Name: _____________________________
Student ID:____________________
private ____ _______________;
//Constructor
public Die () {
}
//Instance Methods
//
public ____ _____________ () {
}
//
public ___ _______________ () {
}
}// end class
//Class 2: PairOfDice This uses two “Die” objects. It rolls them to get the total face
//value of the two die objects
public class PairOfDice {
//Instance Variables
private ____ _______________;
private ____ _______________;
3
Name: _____________________________
Student ID:____________________
private ____ ______________;
private ____ ______________;
private ____ ______________;
private ____ ______________;
//Constructor – construct two Die objects, both with an initial face value of one
public PairOfDice () {
}
//Instance Methods
//
public ____ _____________ () {
}
//
public ____ _____________ () {
}
//
public ____ _____________ () {
}
}//end class
4
Name: _____________________________
Student ID:____________________
//Class 3: BoxCars This class rolls a pair of dice 1000 times, counting the number
//of box cars (two ‘6’) that occur
public class BoxCars {
public static void main (String args [] ) {
//Variables
final int ROLLS = 1000, TARGET =12; //you may have used different names
int total, count=0;
//Code
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
5
Download