DiceRoller assignment

Download Report

Transcript DiceRoller assignment

DiceRoller
DiceRoller (object class) and DiceRollerViewer client class:
Write a DiceRoller class (similar to Yahtzee) to:



Allow the person to initially roll five dice.
Each die has six faces representing numbers from 1 to 6, so the program should randomly pick a number
between 1 and 6.
Print the results of each die to the console.
After the first roll, the person should input the number of dice (between 0 and 5) that they want to roll
again.



If they enter 0, then don’t roll the dice again.
Else, roll that many dice and print the results of each die to the console again.
Repeat the prompt one more time to see if the person wants to roll some of the dice again (for a maximum
of 3 rolls).
Write a DiceRollerGame client class to:


Instantiate a DiceRoller object
Run a method in DiceRoller (such as playGame() ) to play the game.
EXTRA CREDIT (5 points): Have the player continue to take a turn (3 rolls) until they want to
stop.
HINT: You may want to use:

the Scanner class to input data,

the Random class to generate a random number to simulate rolling a die,

a while loop for rolling each of the 6 dice,

an if statement for checking the input,

System.out.print for printing to the console.
Java Concepts 6.5 (Random Numbers and Simulation)
Standard
Classes and Methods
Random Numbers and Simulation


The Random class of the Java library implements a random
number generator.
To generate random numbers, you construct an object of the
Random class and then apply one of the following methods:
 nextInt(n) – returns a random integer between 0 (inclusive)
and n (exclusive)
 nextDouble() – returns a random floating point number
between 0 (inclusive) and 1 (exclusive)

For example, if a contestant on “Deal or No Deal” randomly
selects their briefcase from the 26 initial briefcases:
import java.util.Random;
//Include this at top of program
Random generator = new Random();
int briefcaseNum = generator.nextInt(26) + 1;
Java Concepts 6.5 (Random Numbers and Simulation)
DiceRoller Design
Use Stepwise Refinement to create the DiceRoller class:
Java Concepts 6.5 (Random Numbers and Simulation)