Question
Write a java application program that simulates a simple slot machine in which three numbers between 0 and 9 are randomly selected and printed side
Write a java application program that simulates a simple slot machine in which three numbers between 0 and 9 are randomly selected and printed side by side. The program will allow the user to place a bet, and then make a payout if any two of the numbers are the same, or if all three numbers are the same.
***Note: a Double variable CANNOT be used anywhere in this program, and the following starter code MUST be used
****
import java.util.Scanner;
public class SlotMachine {
// Declare keyboard and a constant starting balance of $10
public static final int INITIALBALANCE = 1000;
public static Scanner kbd = new Scanner(System.in);
public static void main(String[] args) {
// Set up random number generator
RandomHelper generator = new RandomHelper(args);
int startBalance = INITIALBALANCE/100.00;
System.out.printf("Starting Balance = $ "+startBalance);
kbd.close();
}
}
*******
You can download a flowchart here to see how the program should be set up.
The following is a synopsis of how the program should operate:
The game starts with the user having a balance of $10.00.
The program asks the user how much he/she wishes to bet.
If the user enters a number less than zero, or greater than the amount of money in the user's balance, you should repeat step 2.
If the user entered a bet of zero, go to step 9 below.
After a valid bet has been entered, the program should randomly select three numbers from 0-9 and display them on the screen (with some space between for neatness).
If a winning combination is produced, the program should calculate and display the amount of money the user has won. (If there is no win, then do not display anything)
Display the remaining balance. The balance at this point will be the original balance, minus the amount of the bet, plus any winnings.
If the user still has money remaining, the program should ask the user how much he/she wishes to bet.
If the user enters a number less than zero, or greater than the amount of money in the user's balance, you should repeat step 7.
If the user does not have money remaining, you should display a message stating that the user has run out of money, and then go to step 9 below.
Repeat steps 3-8.
At the conclusion of the program, you should display:
The amount of money the user has won or lost.
The final balance that the user will be cashing out.
The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs, and what random numbers are chosen. The user's inputted values are shown below in italics:
Starting Balance = $10.00 Enter your bet (or 0 to quit): $3.50 Slot result: 7 3 3 You have won: $5.25 Balance: $11.75 Enter your bet (or 0 to quit): $2 Slot result: 8 0 0 Balance: $9.75 Enter your bet (or 0 to quit): $0 Overall winnings = $-0.25 Cashing out $9.75
Program Requirements
Follow the formatting that you see in the example program. Space out items with extra line breaks for neatness and ease of reading.
All money values that are printed to the screen should be preceeded by a dollar sign, and should be formatted to display two decimal places.
The starting balance should be stored as a constant in your program and NOT as a variable. It should be declared at the beginning of the main program so it can be found easily. This allows it to be easily modified if we ever decided to change the starting balance. The dollar amount of $10 should ONLY be used ONCE in your program, at the time you are initializing the constant.
Due to the impreciseness of double and float data types, you are NOT ALLOWED to use any double or float variables anywhere in your program. All money values should be stored as integers. Note that a money value can be accurately represented as a whole number by considering the number of pennies in the money value. For example, $1.58 is equivalent to 158 pennies. You can read the user input into your program by using nextDouble(), but you must immediately convert it, and store it, as an integer. Likewise, when printing the dollar amounts to the screen, you need to convert back, and print as a double with two decimal places using printf().
To calculate the winnings:If all three numbers on the slot machine are the same, then
Add 1 to the number on the slot, and then multiply it by the amount of the bet
As an example, if the number shown on all three slots is 4, and the user has bet $2.00, then the winnings would be $10.00. (4 + 1) * $2.00
If any two numbers on the slot machine are the same, then
Multiply the matching number by the amount of the bet and then divide by 2.
As an example, if the number shown on two of the slots is 7, and the user has bet $1.00, then the winnings would be $3.50. 7 * $1.00 / 2
As another example, if the number shown on two of the slots is 2, and the user has bet $3.00, then the user breaks even as the winnings would be $3.00. 2 * $3.00 / 2
As another example, if the number shown on two of the slots is 1, and the user has bet $4.25, then the user gets half of their money back as the winnings would be $2.12. 1 * $4.25 / 2 Note that the answer to this calculation actually is $2.125 but we will not be rounding up. We will simply discard the 0.005 portion of the answer. If all money values in your program are being treated as integers, then this will already be done automatically for you.
As another example, if the number shown on two of the slots is 0, and the user has bet $2.50, then there are no winnings. (0 * $2.50 / 2 produces an answer of 0)
If all three numbers on the slot machine are different, then there are no winnings.
Additional Notes
There is some initial starter code, plus a RandomHelper.class file that must be included in order for your program to be able to be tested properly in Mimir. DO NOT remove any of the starter code.
To choose three random numbers in your program, you MUST use the "generator" variable that is in the starter code, by doing something like this:
int slot1, slot2, slot3; slot1 = generator.nextInt(10); slot2 = generator.nextInt(10); slot3 = generator.nextInt(10);
The first line only needs to be executed once, and therefore should be placed outside of any loops. The last three lines will need to be placed inside a loop since the user may wish to play more than once and will expect to get three different numbers on the second game play.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started