15-110 PRACTICE PROGRAMMING EXAM 1 [CORTINA]

NOTE: The sample question below does not necessarily represent the exact question you will receive on the exam, but it covers similar topics that you are expected to understand.

A slot machine has three wheels that have pictures of various fruit on them (BANANA, ORANGE, RAISIN, CHERRY). A player inserts 1, 2, 3 or 4 dollars as his or her "bet". A player then spins the three wheels. If the player's spin results in a pair (two fruit are the same), then the player wins 5 times the bet. If the player's spin results in a triple (all wheels display the same fruit), then the player wins 50 times the bet. The slot machine will allow the player to spin the wheels 5 times. After each spin, show the amount won and the total winnings so far.

Programming Requirements

Download a copy of the SlotMachine.zip project and uncompress it. Move this folder into the workspace on your desktop.

  1. Using Eclipse, complete the main method to play this game in the Java class SlotMachine provided to you in the downloaded project.

  2. To simulate the 3 slot machine wheels, create three String variables. (You do not need to use an array here, although you can if you wish.) When you "spin" the wheels, set each variable to a random string from the set {"BANANA", "ORANGE", "RAISIN", "CHERRY"}. To help you, there is a static method called getFruit provided for you. If you pass it a value between 0 and 3, it returns the corresponding fruit.

  3. When you ask for the user to input a bet between 1 and 4 dollars (inclusive), you may assume the user will always input an integer. However, if the user inputs a value out of this range, output Error: Invalid Bet and ask for the bet again until the user finally inputs a valid bet.

  4. You should use names for your variables that are self-documenting (i.e. its name explains what information the variable stores) and indent your code using proper Java style.

Program Testing

Use the following sample run to get an idea of how the program should work and what your output should look like. Note that since the slot wheels are random, you will probably not get this output exactly.

Welcome to the SLOT MACHINE! 
Your total winnings = $0 
 
How many dollars do you want to bet (1-4)? 2 
You spun: CHERRY RAISIN BANANA
Sorry, you do not win any money. 
Your total winnings = $0 
 
How many dollars do you want to bet (1-4)? 0 
Error: Invalid Bet 
How many dollars do you want to bet (1-4)? 3 
You spun: RAISIN ORANGE ORANGE
You win $15! 
Your total winnings = $15 
 
How many dollars do you want to bet (1-4)? 4 
You spun: CHERRY CHERRY CHERRY
You win $200! 
Your total winnings = $215 

How many dollars do you want to bet (1-4)? -3 
Error: Invalid Bet 
How many dollars do you want to bet (1-4)? 9 
Error: Invalid Bet 
How many dollars do you want to bet (1-4)? 1 
You spun: BANANA ORANGE RAISIN
Sorry, you do not win any money. 
Your total winnings = $215 
 
How many dollars do you want to bet (1-4)? 4 
You spun: RAISIN BANANA RAISIN
You win $20! 
Your total winnings = $235 
 
THANK YOU FOR PLAYING THE SLOT MACHINE!