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.
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.
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
Orange Banana Cherries
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
No winner. Please try again.
***************** Winner ***************** You won $(amount of pay off)
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. Pull Lever 2. Quit -->
SlotMachine.java
PlaySlots.java
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