Chapter 4: Die ADT

Chapter 4 – Writing Classes
Back to topics from the previous lesson.

  1. Describe the instance data of the Die class.
  2. Which of the methods defined for the Die class can change the state of a Die object-that is, which of the methods assign values to the instance data?
  3. What happens when you pass an object to a print or println method?
  4. What is the scope of a variable?
  5. What are UML diagrams designed to do?

Programming Projects::

PP 4.3 Write an application (driver) that rolls a die and displays the result. Let the user pick the number of sides on the die. Use the Die class from this chapter to represent the die in your program.
Include a UML class diagram.

UML Basics

PP 4.4 Design and implement a class called PairOfDice_YI, with two six-sided Die objects. Create a driver class called BoxCars_YI with the main method that rolls a PairOfDice_YI object 1000 times, counting the number of boxcars (two sixes) that occur. Print a full sentence with the outcome. Run the driver/test class at least 3 times.
Include a UML class diagram.

PP 4.5 Using the PairOfDice_YI class from Programming Project 4.4, design and implement a class to play a game called Pig_YI. In this game, the user competes against the computer. On each turn, the player rolls a pair of dice and adds up his or her points. Whoever reaches 100 points first, wins. If a player rolls a 1, he or she loses all points for that round and the dice go to the other player. If a player rolls two 1s in one turn, the player loses all points earned so far in the game and loses control of the dice. The player may voluntarily turn over the dice after each roll. So the player must decide to either roll again (be a pig) and risk losing points, or give up the dice, possibly letting the other player win. Set up the computer player so that it always gives up the dice after getting 20 or more points in a round.
Draw a flowchart.

Flowcharts: Most used shapes and Examples

Use draw.io or google draw for the flowchart.
Screen Shot 2014-09-28 at 3.36.01 PM

Die.java

//********************************************************************
//  Die.java       Author: Lewis/Loftus/Cocking
//
//  Represents one die (singular of dice) with faces showing values
//  between 1 and the number of faces on the die.
//********************************************************************

import java.util.Random;
public class Die
{
   private final int MIN_FACES = 4;

   private static Random generator = new Random();
   private int numFaces;   // number of sides on the die
   private int faceValue;  // current value showing on the die

   //-----------------------------------------------------------------
   //  Defaults to a six-sided die. Initial face value is 1.
   //-----------------------------------------------------------------
   public Die ()
   {
      numFaces = 6;
      faceValue = 1;
   }

   //-----------------------------------------------------------------
   //  Explicitly sets the size of the die. Defaults to a size of
   //  six if the parameter is invalid.  Initial face value is 1. 
   //-----------------------------------------------------------------
   public Die (int faces)
   {
      if (faces < MIN_FACES)
         numFaces = 6;
      else
         numFaces = faces;

      faceValue = 1;
   }

   //-----------------------------------------------------------------
   //  Rolls the die and returns the result.
   //-----------------------------------------------------------------
   public int roll ()
   {
      faceValue = generator.nextInt(numFaces) + 1;
      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Returns the current die value.
   //-----------------------------------------------------------------
   public int getFaceValue ()
   {
      return faceValue;
   }
}

[collapse]