MET CS 565 Spring 2004 - HW2 (Due Feb 17, 2004 ) 20 Points
Goals
Write your own classes
Build a simple GUI
Problem
Click here to see the applet in actionWe want to simulate two players playing at a casino. Each player walks into the casino with an initial amount they want to gamble. They can wage any amount as long as the wager is less than or equal to their remaining balance. Each slot machine has three slots, each slot will be populated with a random value between 1 and 9. The wins and losses are determined as follows:
Rule 1: (Jackpot) If all the three slots have the same number, the player's wins twice their wager.
Rule 2: (Not bad) If any of the two slots have the same number, the player's gets back half their wager (we will use integer division, so if $1 is the wager, you don't get back anything)
Rule 3: (Oops) If none of the slots match, the player loses the wager
Develop an applet with a simple GUI as shown to simulate the two players. First, write the class Player with the following behavior:
private attributes: name (String), balance (int)
constructor: only one with two arguments, name and initial balance
public methods:
randomValue(), returns int, a random number between 1 and 9
play(int s1, int s2, int s3, int wager), simulates the above rules given the three slot values and the player's wager
toString(), returns String, in the format: name[balance]
getName(), getBalance(), and setBalance(int value)
Now, develop the applet class with two Player objects. Please see the sample code how to get the Buttons working. A play is simulated as follows: Call the randomValue() method of the corresponding player three times to get the three slot values. Then pass these and the wager amount to the play method of that player. Then update the label for the player by calling the toString() method of that player.
Part 2
Extend the player class to create another class called PreferredPlayer. For this player, the rules will be as follows.
Rule 1: (Jackpot) If all the three slots have the same number, the player's wins four times their wager.
Rule 2: (Not bad) If the first and the last slots have the same number, the player's gets back half their wager (we will use integer division, so if $1 is the wager, you don't get back anything)
Rule 3: (Oops) Otherwise, the player loses the wager
So, you have to override the play method for this class. Also, override the toString method so that it concatenates the prefix "Pr:" to the super class's toString method. Provide the constructor as above for this class and class the super's constructor in its body.
Now, develop another applet with a regular Player object and another a PreferredPlayer object. Use polymorphism, i.e, you will be declaring the two variables as of type Player, but instantiating the latter with the PreferredPlayer constructor.