Lab 8
Random Objects (redux)


Preliminary Setup

For this lab, you will be creating an object-oriented (OO) program to play a simple dice game. Start with the following:

  1. Create an IntelliJ project. Make sure you use your name somewhere in the project name (like CassLab8).
  2. Create the Main class in This is where you will put the public static void main(String[] args) method.

    Note that to create a class in IntelliJ, right click on the src folder in the project, and select New > Java Class.

  3. Create a Dice class in

Step I: Write the Dice class

In, write the Dice class. A Dice object should have two properties/attributes: the number of sides the dice has, and the current value showing on the dice. These properties should be instance variables in your class. Make sure you declare the instance variables private.

Add the following methods:

Step II: Write main

In the Main class, write the main() method to play a simple game according to the following rules:

  1. The game uses a D6 and a D12 (6-sided and 12-sided dice).
  2. On each turn, we roll both dice.
  3. The game is won if one of the dice shows a value that's exactly twice the value of the other.

Your main method should create variables for the two dice: each should be an object of type Dice.

Your program should ask the user to press the return key before each turn. This can be accomplished with a Scanner object [docs here]. Scanner can be imported with import java.util.Scanner;. For example, you can create a Scanner like this:

Scanner input = new Scanner(;
Once you have the scanner initialized like this, you can ask the user for input like this:
System.out.println("Press enter");
String line = input.nextLine();

After each turn, the program should check if we've won. If we've won, print a message saying so and end the program.

Step III: Add a default [Extra Credit]

In Python, we could give default values to parameters (to constructors or any other methods). In Java, a similar result can be obtained using method overloading: we can have two methods with the same name as long as the parameter lists differ.

Now, go back and modify the Dice class so that when we construct a Dice object without specifying the number of sides, then we get a D6 (but if we do specify a number of sides, then we get a dice with the given number of sides).

Modify main() so it uses this default as appropriate.

How to turn in this lab

Before turning in any program in this class, remember this mantra:

Just because it works doesn't mean it's good.

Part of your grade will also come from things like how understandable and readable your code is. You will also be graded on the neatness, presentation, and style of your program code.

For all labs, turn in only an electronic version. Please compress your program and email the zip file or tarball to me at

Ask for help if you're having problems!

1. I've checked, and "Dice" is an acceptable singular form. If this bothers you because you consider "Dice" as plural only, then you should instead create a "Die" class. Classes should be named with singular nouns.