CSC1110 Exercise 5

Making Dice!

One of the most common tasks in any game, whether it is a board game or a video game, is randomization. How much damage did the last hit do? What type of enemy is spawned and when? What is in that loot chest? Dice have been a simple form of randomization for thousands of years, and are ubiquitous across most cultures. For this exercise, we will define a Die class, make a few different instances of a Die and perform some simple tasks with the dice we made.

The Die class

In Java, an instantated class is a class that describes what an object knows and can do. The class is often a blueprint for some real world thing we need to model in our program. It is not a program itself, just a collection of the variables and methods that the particular object will have.

To make a single Die, we will first need to know how many sides it has. A typical Die in most games is a cube (six sides) but there are many other types of polyhedral dice popularized by, but not limited to, tabletop roleplaying games such as Dungeons and Dragons. Even a coin can be considered a Die that has two sides.

After that, we will need to know what side is the current value. For most dice, the side that is on top, facing up, is what we consider the current value, though some, such as the four-sided die, read values printed on the sides of the die instead. So those will be the instance variables for the Die

The things we will do with the Die are pretty simple. We need to be able to roll the Die and tell whan the current value is. With this knowledge, we are ready to design our very own Die class.


The Die class will have two constructors. One will be the default six-sided Die. For this constructor, we need no parameters. The second constructor will be for any Die with some number of sides other than six. When we have multiple constructors in a single class, we often want to ensure that one "main" constructor is always used to construct the class. This is done for a number of reasons, chiefly to reduce possible errors, to reduce the complexity of the code, and make it easier to maintain and update the code.

What this means from a code standpoint is that every constructor but the main one will be calling the main constructor. This means usually (but not always), the main constructor is the one with the most parameters in it's definition. To call a constructor from another constructor in the same class requires using the this keyword to represent "this" class. An example of a constructor with one parameter calling a main constructor with two parameters would look like this:

public Fraction(int numerator) {
    this(numerator, 1);

public Fraction(int numerator, int denominator) {
    this.numerator = numerator;
    this.denominator = denominator;

This is because every Fraction must have a numerator and denominator. If only a numerator is given, the assumption is the denominator is 1 (a whole number).


The Unified Modeling Language (UML) was designed, in part, to easily show this kind of information graphically. A UML Class Diagram will have all the basic information needed to start setting up an instantiable class. it will not, however, have the details necessary to fully implement the class, which is why requirements documents like the lab and homework pages are necessary. Below is the UML Class Diagram for the Die class. A general rule of thumb regarding UML diagrams is that they will often not bother to include private helper methods and instead focus on the public-facing interface that users of these classes will interact with.

Here is the UML Class Diagram for the Die class


Driver Class

Below is a partial implementation of the driver class. You will need to instantiate your Die objects and implement the two helper methods.

* Course: CSC-1110
* Fall 2023
* Exercise 5 - Dice
* Name: Sean Jones
* Last Updated: 7-26-23
package exercises.ex5;

* Driver class for some dice
public class Exercise5 {
   public static void main(String[] args) {
       final int twentySides = 20;
       // TODO: Instantiate your Die objects

       System.out.println("Flipping a coin: " + flipCoin(coin));
       System.out.println("Rolling a d6 and d20: " + rollTwoDice(d6, d20));

   private static String flipCoin(Die coin) {
       // TODO: Implement this method. A value of 1 would be heads, 2 would be tails
       return null;

   private static int rollTwoDice(Die d1, Die d2) {
       // TODO: Implement this method. Add the two dice together and return the sum.
       return 0;

Submission Instructions:

Submit your completed and to Canvas.