CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades | Students

Assignment #5

Roll the dice

Due Date: Monday of Week 3
25 points

This project uses a class that defines a die and a class that defines a pair of dice that holds two die objects. Both of these classes have overloaded constructors. The coordination of the classes is somewhat complicated for what seems to be a simple application.

Console

Operation

  • If the user chooses to roll the dice, the application rolls two six-sided dice, displays the results of each, and asks if the user wants to roll again.

Specifications

  • Create a class named Die to store the data about each die. This class should contain these constructors and methods:
  • public Die()              // default to a six-sided die
    public Die(int sides)     // allow a variable number of sides
    public void roll()
    public int getValue()
     	
  • Create a class named PairOfDice to store two dice. This class should contain two instance variables of the Die type, an instance variable that holds the sum of the two dice, and these constructors and methods:
  • public PairOfDice()           // default to six-sided dice
    public PairOfDice(int sides)  // allow a variable number of sides
    public void roll()
    public int getValue1()        // get value of die1
    public int getValue2()        // get value of die2
    public int getSum()           // get the sum of both dice
     	
  • You can use the random method of the Math class to generate a random number from 1 to the number of sides on a die like this:
  • int value = (int) (Math.random() * sides);
  • Create a class named DiceRollerApp that uses the PairOfDice class to roll the dice. This class should display special messages for craps (sum of both dice is 7), snake eyes (double 1's), and box cars (double 6's). For this application, assume that two six-sided dice are used.
  • Create a class named Validator that contains static methods that can be used to validate the data in this application.
  • As you need to concentrate only on working on the dice classes, here is the Validator class that you can use. Just copy and paste below. As there's an input by asking the user to continue or not, so you can use the Validator to validate the y or n. You can either use the validator or do your own as long as you make sure that the user enters only y, Y, n or N.

Include in your program the following comments:
  • Name of program
  • Programmer's name
  • Current Date
  • Computer system and compiler you are using
  • Brief description of the program

Compile and run the Java program.

Take a screen capture by hitting simultaneously the keys Alt and Print Screen (Prt Sc) of the output results (console) and paste it in MS Paint. Save it as a gif file.

When you are ready, upload the java program and the gif file by clicking the link below:

Upload Assignment

I cannot grade if I don't get the files and receive your email.