CSC1110 - Week 6 Exercise
Overview
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.
Constructors
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).
UML
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
You must write a Driver class that does the following:
- Contains a helper method called flipCoin() that takes in a Die called coin and returns a String. This method should flip (i.e., roll()) the passed in coin and return "heads" if the value of the coin is 1 or "tails" if the value is 0.
- Contains a helper method called rollDice() that takes in an int called numRolls and a Die called d20 and returns and int. This method will roll the passed in Die numRolls times, sum the resulting values, and return the sum.
- A main() method that
- Makes an instance of a Die with 2 sides. Pass this Die into the flipCoin() method and print out the result.
- Makes an instance of a Die with 20 sides. Pass this Die into the rollDice() method and print out the result.
Below shows a sample output.
Sample output
Result of flipping a coin: heads Result of rolling a d20 4 times: 34
Submission
See your professor's instructions for details on submission guidelines and due dates.