SlotMachine

Directions

In this assignment we are going to write a class that simulates the operation of a slot machine. There are many different kinds of slot machines today. One of the more traditional slot machines has three rollers that contain pictures of fruit. A user pulls a lever which causes the rollers to spin. After spinning for a few seconds each roller stops on one of the fruit pictures. If all three rollers display the same fruit, then the user wins. When there is a winner the slot machine pays off at different amounts. There is a 60% chance that it will pay off between 1 and 10 dollars, a 30% chance that it will pay off between 11 and 100 dollars, and a 10% chance the winner will get the jackpot of 10,000 dollars.

Using class decomposition we are going to decompose this problem into two classes. The first class will be named SlotMachine. It's job will be to store the jackpot and provide methods to allow a user to play the game. The second class will be named PlaySlots. It's job will be to manage the program by creating a SlotMachine object that it can send messages to allow the user to play the game. This class will contain the main method for the program.

SlotMachine class

Using method decomposition the SlotMachine class can be decomposed into the following methods.

Create a file named SlotMachine.java and add the following code.

public class SlotMachine
{
    // constants 
	
	
    // instance variables
 
    
    // methods
        
} 

Recall that a class is defined by two things: what it stores (variables) and what it does (methods). What a slot machine stores is the current value of each roller. To simulate the storing of the roller values add the following variables to the SlotMachine class under the instance variables section.

private int roller1;  
private int roller2;   
private int roller3;   

In this class we are adding a new section called constants. A constant is a special kind of variable declared with the keyword final. The difference between a constant and a variable is that a constant cannot be changed once it has been assigned a value. Since constants cannot be changed it is not necessary to declare them with the private visibility modifier instead it is customary to declare them public.

Add the following code to the constants section of the class.

public final int CHERRIES = 0;
public final int APPLE = 1;
public final int ORANGE = 2;
public final int LEMON = 3;
public final int BANANA = 4;

A common programming practice is to use constants to assign names to numbers. In the SlotMachine class 5 constants are defined: CHERRIES, APPLE, ORANGE, LEMON, and BANANA. A value of zero represents cherries, 1 represents apples, 2 represents orange, and so on. Therefore, anytime the class needs to refer cherries the constant CHERRIES can be used instead of the number zero. This makes the program easier to read.

Note: It is a common practice to declare constants in all uppercase letters.

The SlotMachine class provides methods that allow a user to play the game. Add the following methods to the class.

/* This method randomly selects a fruit for each of the three rollers.
 */
public void pullLever()
{

}

/* This method displays the type of fruit contained in each roller.
 */
public void displayRollers()
{

}
/* This method determines the amount of pay off when there is a winner
 * @ return the amount of payoff
 */
private int getPayOff()
{

}

/* This method displays the results of pulling the lever.
 */
public void displayResults()
{

}

Implement SlotMachine Methods

  1. Implement the method pullLever. This method randomly selects a fruit for each of the three rollers by generating 3 random numbers in the range of 0 to 4 and assigning one of the random numbers to each of the three rollers.

  2. Implement the method displayRollers. This method displays the type of fruit contained in each roller. For example, if roller1 has a value of 0 the word Cherries would be displayed, if it has a value of 1 the word Apple would be displayed, and so forth.

    Here is a sample output.
    Orange Banana Cherries

  3. Implement the method getPayOff. This method determines the amount of pay off when there is a winner. There is a 60% chance it will pay out bewteen 1 and 10 dollars, a 30% chance it will pay out between 11 and 100 dollars, and a 10% chance the winner will win the jackpot of 10,000 dollars.

    Here is the pseudocode for the method.
    generate random number between 0 and 9 inclusive
    
    if random number is greater than or equal to 0 and less than 6 
        return a random number between 1 and 10 inclusive
    else if random number is greater than or equal to 6 and less than 9 
        return a random number between 11 and 100 inclusive
    else
        return 10000

  4. Implement the method displayResults. This method displays the results of pulling the lever. If the rollers do not all match then there is not a winner and the method should display the following message.

    No winner. Please try again.
    	
    If there is a winner the method should call the helper method getPayOff to determine the amount of the pay off. The following message should be displayed when there is a winner.

    *****************
         Winner
    *****************
    
    You won $(amount of pay off)
    
    	

PlaySlots class

The job of the PlaySlots class is to manage the program by interacting with a SlotMachine object so that a user can play the game.

Implement the main method

  1. Instaniate a SlotMachine object.
  2. Instaniate a Scanner object.
  3. Display the following message:

    1. Pull Lever 2. Quit -->
    	
    As long as the user responds with a one then play slots game by
    • pulling the lever
    • display the rollers
    • display the results of the lever pull

Source File

SlotMachine.java

PlaySlots.java

Sample Run

1. Pull Lever 2. Quit -->1
Banana Cherries Cherries
No winner. Please try again.

1. Pull Lever 2. Quit -->1
Apple Cherries Lemon
No winner. Please try again.

1. Pull Lever 2. Quit -->1
Apple Orange Apple
No winner. Please try again.

1. Pull Lever 2. Quit -->1
Banana Banana Apple
No winner. Please try again.

1. Pull Lever 2. Quit -->1
Cherries Lemon Cherries
No winner. Please try again.

1. Pull Lever 2. Quit -->1
Cherries Banana Orange
No winner. Please try again.

1. Pull Lever 2. Quit -->1
Orange Banana Apple
No winner. Please try again.

1. Pull Lever 2. Quit -->1
Banana Banana Banana
*****************
     Winner
*****************

You won $5

1. Pull Lever 2. Quit -->2