Program 3 (25 points) Write a Java program to implement a dice

advertisement
Program 3 (25 points)
Write a Java program to implement a dice game. You will need to create two classes for this
assignment:
class Die is used to represent a single die (the singular form of dice). This class should have the
following attributes:
• instance variables:
• sides: an int value representing the number of sides your die has (most games use 6sided dice, but there are plenty of variations)
• value: an int value representing the current roll of the die; will be a number between 1
and sides
• a Random object
• instance methods:
• two constructors:
• the default constructor should set the value of sides to 6, initialize the Random
object and roll the die to get an initial value for variable value
• another constructor that takes a single argument representing the number of sides
you want your die to have, sets sides to this value, and does all the same
operations the other constructor does
• one mutator method called roll, which rolls the die (generates a random number between
1 and sides, which is then assigned to value)
• one accessor method called getRoll, which returns value
• a main method for testing that the other methods work
Your second class represents the game; the name of the class will be up to you. This class will also
contain a main method, which is the driver for the game. You may want to include other methods,
depending on how complicated your game is. You must meet the following requirements:
• Your game must use at least two dice (2 Die objects, in other words)
• Your game must have at least two players: the user and the computer
• You must have a way to keep score and to inform the user of his/her score
• The computer must play to win, if that is possible (if not a pure game of chance)
• The user must be given the option to play again once a game is completed
Download