This sample class is by Ronnie Smith, with very minor revisions. The idea is that an object of class Dice can simulate rolling a single die. It relies on a pseudo-random number generator, which produces a sequence of apparently random numbers that are not really random at all.
import java.util.*; public class Dice { //=================================================== // numSides is the number of sides that this die has. // // numRolls keeps track of the number of times this // die has been rolled. // // generator is a pseudo-random number generator. //=================================================== private int numSides; private int numRolls=0; private Random generator = new Random(); //=================================================== // Constructors //=================================================== //-------------------------------------------------- // new Dice() returns a new 6-sided die. //-------------------------------------------------- public Dice() { numSides = 6; } //-------------------------------------------------- // new Dice(n) returns a new die with n sides. //-------------------------------------------------- public Dice(int n) { numSides = n; } //=================================================== // Accessors //=================================================== //-------------------------------------------------- // x.getNumSides() returns the number of sides of // die x. //-------------------------------------------------- public int getNumSides() { return numSides; } //-------------------------------------------------- // x.getNumRolls() returns the number of rolls // stored in die x. //-------------------------------------------------- public int getNumRolls() { return numRolls; } //=================================================== // Mutators //=================================================== //-------------------------------------------------- // x.setNumSides(n) sets the number of sides of x // to n. //-------------------------------------------------- public void setNumSides(int n) { numSides = n; } //-------------------------------------------------- // x.resetNumRolls sets the number of rolls in x // to 0. //-------------------------------------------------- public void resetNumRolls() { numRolls = 0; } //=================================================== // Actions //=================================================== //-------------------------------------------------- // x.roll() returns a pseudo-random number between // 1 and x.getNumSides(), inclusive. //-------------------------------------------------- public int roll() { ++numRolls; return 1 + generator.nextInt(numSides); } }
The following class illustrates how the Dice class can be used.
import java.util.*; // Display rolls of 3 different dice, 1 set of rolls per line. // The command line contains the number of sides of two of // the dice. The third has the default 6 sides. public class DiceTest6 { public static void main(String[] args) { Dice die1 = new Dice(Integer.parseInt(args[0])); Dice die2 = new Dice(Integer.parseInt(args[1])); Dice die3 = new Dice(); Scanner keyboard = new Scanner(System.in); System.out.print("Enter number of rolls "); int timesToRoll = keyboard.nextInt(); System.out.printf("%14s\n","Results"); for (int rollNum = 1; rollNum <= timesToRoll; rollNum++) { System.out.printf("%5d %5d %5d\n", die1.roll(),die2.roll(),die3.roll()); } } }