• No replies
1 Post

Pinned topic Dice Program

‏2011-11-16T02:51:18Z |
Please help me on the below dice game issue.

Part 1:

Write a dice class that takes a variable number of sides. The dice
class should have an explicit roll method that returns the result of a

Dice d20 = new Dice(20);
System.out.println(d20.roll()); // prints 14 (for example)

Optionally, keep track of the last rolled number and provide a get
Part 2:

Often we want to roll multiple dice of the same type and adjust the
total by a constant amount.

  • This is not the same as multiplying one roll by a number. ***

Modify the dice class to allow the following types of calls:

Dice d20 = new Dice(20);

// The following simulates rolling 4 20-sided dice and adding 5 to
// the result:
System.out.println(d20.roll(4) + 5); // prints 48 (for example)

// The following simulates rolling 5 20-sided dice and subracting 1
// from the result:
System.out.println(d20.roll(5) - 1); // prints 79 (for example)
Part 3:

Wrap it all up in a class that rolls a given type of dice a fixed number
of times, adding a fixed adjustment, whenever its roll method is called:

Roller x = new Roller(4, 20, 5);

Dice d20 = new Dice(20);
Roller y = new Roller(4, d20, 5);

The two examples above will both set up a Roller that will return the
value of rolling a 20-sided dice 4 times and adding 5 to the total (i.e.
the same as doing (new Dice(20)).roll(4) + 5).

The roll() method should always return a number >= 0, so do not allow a
Roller to be instantiated with values that could result in negative
values being returned from roll.