Class GameDie


public class GameDie extends Object
A GameDie object represents a single game die. It has a set number of sides, each numbered from 1 to the number of sides, and it can be rolled, which results in a number between 1 and the number of sides. Adapted from Astrachan's "A Computer Science Tapestry" 2nd edition, pp. 214, 217. Compile using: javac GameDie.java (but cannot execute by "itself" - no main method!)
  • Constructor Summary

    Constructors
    Constructor
    Description
    construct a new die with the default number of sides, 6, and an initial top value of 1
    GameDie​(int desiredNumSides)
    construct a new die with the user-specified number of sides and an initial top value of 1
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    return the die's current top value
    int
    return the calling die's number of sides
    int
    expects nothing, computes a pseudo-random int in [1, num_sides], changes currTop of the calling GameDie to that computed value, and also returns that computed value as the result of this call to roll

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GameDie

      public GameDie()
      construct a new die with the default number of sides, 6, and an initial top value of 1
    • GameDie

      public GameDie(int desiredNumSides)
      construct a new die with the user-specified number of sides and an initial top value of 1
      Parameters:
      desiredNumSides - number of sides for this die
  • Method Details

    • getNumSides

      public int getNumSides()
      return the calling die's number of sides
      Returns:
      number of sides of calling die
    • getCurrTop

      public int getCurrTop()
      return the die's current top value
      Returns:
      current top value of the calling die
    • roll

      public int roll()
      expects nothing, computes a pseudo-random int in [1, num_sides], changes currTop of the calling GameDie to that computed value, and also returns that computed value as the result of this call to roll
      Returns:
      the value of this roll of the calling die