Name: _________________________________________

CSC 221: Computer Programming I         Fall 2006

HW 3: Classes and Slots

For this assignment, you will simulate a slots game. If you are not familiar with the game, a slot machine contains three windows or slots in which an image is shown. To play the game, the player inserts a token and pulls on a handle, causing the images in the slots to be randomized (usually by spinning the wheels on which the images are printed). If the three resulting images in the slots are identical, say three lemons, then the player wins and receives some payoff.

The SlotMachine class, which has been provided for you, does not utilize graphics, but instead represents the result of a spin with text. The spin method returns a String that describes the spin and whether it was a win or a loss. For example, a call to spin might return   "cherry-bar-cherry: YOU LOSE!"   or   "lemon-lemon-lemon: YOU WIN!".

EXERCISE 1:    Load this class into the BlueJ IDE and create a SlotMachine object. Call the spin method 5 times and write the returned values below. Were any of your spins winners? Would you expect them to be? Explain.

EXERCISE 2:    Currently, the SlotMachine class has a private field, numSpins, which keeps track of the number of times spins has been called. Define an accessor method named getNumberOfSpins that returns that number. That is, a call to your getNumberOfSpins should return the value of the numSpins field (i.e., number of times that spin has been called). Be sure to include reasonable comments, so that your method is properly documented in the Interface view of BlueJ. Test your method to make sure that it behaves as desired.

Adding State to Maintain a Bankroll

While the SlotMachine class allows us to model a simple slot machine and perform repeated spins, it lacks many essential features of a real slot machine. Most notably, it doesn't support wagering that results in winning or losing tokens. In order to support wagering, the SlotMachine object must keep track of the player's current bankroll, subtracting some number of tokens for each spin and awarding tokens for a win. For our simple machine, we will assume that each spin costs 1 token. If the resulting spin is a winner, then the player is awarded 9 tokens. Thus, a winning spin results in a net gain of 8 tokens.

EXERCISE 3:    Modify the SlotMachine class so that it maintains the customer's bankroll and updates it as spins are made. In particular, you will need to make the following additions/changes:

Be sure to test your modifications carefully by creating objects and inspecting their state before and after method calls.

EXERCISE 4:    Perform 5 different simulations with your modified slot machine. For each simulation, start with a bankroll of 10 tokens, and repeatedly spin until you either reach 30 tokens or else run out of money. For each simulation, list the final bankroll (either 0 or 30) and the number of spins it took to reach that bankroll. Are these results what you expected? Explain.

EXERCISE 5:    What happens if the player runs out of money but continues playing? That is, if the bankroll is 0 and spin is called, will an error occur? What happens to the bankroll? If it becomes negative, is it possible for it ever to become positive again? Explain your answers.

Adding Conditional Execution for Error Checking

While your modifed SlotMachine class can now support wagering, it is not very robust in its behavior. For instance, there is nothing to stop the player from adding a negative amount to the bankroll. More alarmingly, there is nothing to stop the player from continuing to play even though they are out of money.

EXERCISE 6:    In order to make the SlotMachine class more robust, make the following changes:

Be sure to test your modifications carefully to make sure they behave as desired.

Submit your modified file, along with your answers to the exercises, via the Digital Dropbox.