| RHS => Allan => Basic Programming => Exercises => FruitDiceGame |
Updated:
02/02/2004 23:36 WebMaster: allanhn@rhs.dk |
Fruit DiceGame
IMPORTANT!
This assignment must be solved in your studygroup.
Each studygroup must mail me the solution that the group made, before
11th November.
The problem
In a DiceGame you can win points and fruit!
The rules are very simple.
Each player in turn rolls a die.
If you roll 6 you win 10 points
If you roll 5 you win 10 points and an apple
If you roll 2 or 4 you win 5 points
If you roll 1 or 3 you loose 15 points
For statistic reasons we would like to know how many times a player rolled respectively 1, 2, 3, 4, 5 and 6.
Let's look at what we need:
To count how many times a player rolls 1, 2, 3 and so on, we need 6 variables
(count1, count2 and so on).
To keep track of the score we need a variable for points (points) and a variable
for apples (apples).
Finally we need a die - we'll reuse the Die class from the DiceGame exercise.
You can download it here: Die.java
So we need 9 variables: count1, count2, count3, count4, count5, count6, apples, points and die.
To play the game we'll need a method - play().
It would be nice
to know what we won or lost so it should return a String.
The method should resemble this template:
public String play() {
roll the die and get the facevalue.
Use the methods roll() and getFaceValue() from
the Die object.If facevalue is 1 increment count1 else if facevalue is
2 increment count2 else if ... and so on.
Create a method void registerRoll(int faceValue)
that does this.
Call registerRoll from the play method and pass the
facevalue to it.Update the score (points and apples) and get the
String telling what you won or lost.
Create a method String getMessage(int faceValue)
that does this.
Call getMessage from the play method and pass the
faceValue to it.
}
Finally we need a method to give us the state of the object - the usual toString().
So we need 4 methods: registerRoll, getMessage, play and toString.
And of course we'll need a constructor!
Question 1 Worker
class
Discuss carefully in your studygroup how the worker class - FruitDiceGame- should be made.
Take care when you decide how the four methods should look like, how and where
you
activate
them.
Create the FruitDiceGame class and compile it.
Question 2 Application class
In your studygroup, create
the application class - FruitDiceGameApp.
This class has the usual main method. In the main method you should:
Create the FruitDiceGameApp class, compile and run it.
Question 3 public or private?
For each of the 4 methods, discuss whether the method should be public or
private.
Write a short note for each method and tell what you chose and why.