Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

mport java.util.Scanner; public class SlotMachine { // Declare keyboard and a constant starting balance of $10 public static final int INITIALBALANCE = 1000; public static

mport 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); kbd.close(); } }

(Here is the starter code)

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.(Here is the example flowchart).

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.

While testing your program, you will need PREDETERMINED VALUES (and not random ones) on the slots - otherwise it will be difficult to effectively test your program for correctness since there will be getting different values every time the program executes. This is the reason the RandomHelper class is included. In order to test your program with completely random numbers, you can start the program by typing:

java SlotMachine

But if you want to test your program with predictable slot machine values, you can start the program by putting the slot numbers on the command line like this:

java SlotMachine 3 5 5 2 4 7

By doing this, you can set the slot values to whatever numbers you wish and therefore can control whether the game will be won or lost. In this example, the first slot machine values will be 3 5 5 and if you keep playing, the second values will be 2 4 7. If you play a third time, it will give you 3 5 5 again.

Trivial Information

The slots will produce a combination containing two matching numbers approximately 27% of the time.

The slots will produce a combination containing three matching numbers approximately 1% of the time.

This means that if you played the slot machine 1000 times, you will get a double match about 270 times. You will get a triple match ONLY about 10 times.

The odds are against you for getting a triple match! If you want one so you can test your program's correctness, you are going to have to force it to happen with the trick I mentioned above!

Game Playing Tips

Trying to win more money than you lose?

The odds are against you! Adding together the two percentages above tells us there is a matching combination about 28% of the time.

A double zero does not pay out money. A double zero is produced about 2.7% of the time. This means that you will receive a pay out only 25.3% of the time.

A double one only pays out half of the bet (so you have still lost money). A double 1 is produced about 2.7% of the time. A double two only pays out your bet (so you break even). A double 2 is produced about 2.7% of the time. This means that you will receive a pay out larger than your bet only 19.9% of the time!

So, any time you play, there is only about a 20% chance you will win more money than you bet.

As you play, always bet the same amount. Set a threshold for winning and a threshold for losing. If your balance increases past the winning threshold then double your bet. If your balance decreases past the losing threshold then cut your bet amount in half. Everytime you change your bet amount, move your threshold amounts

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are association areas, and what roles do they play?

Answered: 1 week ago