Skip to main content
Use dice to introduce instantiable classes in Java because it’s easier for novice students to create classes based on a familiar object.
- 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:
public class Die {
private int numSides;
public void setSides(int sides) {
}
}
public static void main(String[] args) {
Die die1 = new Die();
int x = 6;
die1.setSides(x);
}
- 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.