- Create a Die class with one of its instance variables representing the number of sides the die object has.
- Use the following example code to show students how to set instance variables:
- Note from CS Teaching Tips Team: To extend this analogy, have students create a method
roll
that returns the result of one roll of the die.
public class Die {
private int numSides;
public void setSides(int sides) {
numSides = sides;
}
}
public static void main(String[] args) {
Die die1 = new Die();
int x = 6;
die1.setSides(x);
}