CS 1063, Fall 2005
|
Here is the Dice class from the Week 4 Lectures to use as a worksheet:
public class Dice {
private int mySides;
private int myLastRoll;
//constructor
public Dice (int sides) {
mySides = sides;
myLastRoll = 1;
}
//accessors
public int getSides() {
return mySides;
}
public int getLastRoll() {
return myLastRoll;
}
public String toString(){
return "sides: " + mySides +
", Last Roll: " + myLastRoll;
}
//modifier
public int roll() {
myLastRoll = (int)(Math.random()*
mySides) + 1;
return myLastRoll;
}
}
|
public class DiceTest {
public static void main(String[] args) {
Dice d1 = new Dice(6);
Dice d2 = new Dice(6);
int roll1 = d1.roll();
int roll2 = d2.roll();
int sum = roll1 + roll2;
System.out.println(sum);
}
}
|