Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

The Slot Machine

Viktor
Viktor 


Overview The Slot Machine is a popular casino game that comes in a lot of shapes and sizes, but all have the same basic functionality: Multiple wheels are spun simultaneously, and then stop randomly. When all wheels have stopped, the prize value (if any) is based on the number of matching wheel values. You will create three incremental versions of the game called SlotMachine1, SlotMachine2 and SlotMachine3. After you complete SlotMachine1, copy your code into SlotMachine2, then add features to it. You’ll do the same by copying your SlotMachine2 code as your starting point for SlotMachine3. Read the instructions for each program carefully, and make sure the format of your output matches the sample output in the instructions. Pre-Lab (5 Points) Create the shell programs for SlotMachine1.java with the method signatures and comments provided in the instructions. Bring to your lab class for credit. Of course if you work ahead, these can include whatever code you have written so far. Version 1 (30 Points) Create a Java solution for the game SlotMachine1. In this incomplete version of the game, player simply spins until choosing to quit. The spun values will be displayed after each spin, as shown in the sample output on the right à Your solution should have the following two methods: main method Create a loop which does the following: Prompt user whether to Spin or Quit If Q, exit the loop if S, call the spinWheel method three times - Each time spinWheel is called, a String is returned - Save those returned String values in three String variables - Print the three spin values spinWheel method Create a String array containing four Strings of your choice. Generate a random number which represents the index location of one of the 4 Strings in the array. Return that String from the array to the main method. (Sample Output: SlotMachine1 >java SlotMachine1 Spin/Quit (S/Q) :s Wilma - Fred - Wilma Spin/Quit (S/Q) :s Wilma - Fred - Barney Spin/Quit (S/Q) :s Fred - Barney - Fred Spin/Quit (S/Q) :s Betty - Barney - Betty Spin/Quit (S/Q) :s Fred - Barney - Fred Spin/Quit (S/Q) : q) Version 2 (35 Points) Create a Java solution for the game SlotMachine2. Start by copying your working code from SlotMachine1 as the starting point for this version. Revision of SlotMachine1 with the following changes: Add a method called checkWinner which accepts three String values as parameters, and compares them. This method should return a double with the prize amount of the spin. If all three Strings are the same, print "Won $1" and return 1.0 to the main method. If two of the Strings match, print "Won 50 cents" and return 0.5 to the main method. Otherwise, print "No prize" and return 0.0 to the main method. In the main method, after the line that displays the three spun values, add a line that calls the checkWinner method, passing the three String values from calling spinWheel. The number (double) returned by checkWinner will be needed in version 3. (Sample Output: SlotMachine2 >java SlotMachine2 Spin/Quit (S/Q) : s Wilma - Wilma - Wilma Won $1 Spin/Quit (S/Q) : s Barney - Barney - Barney Won $1 Spin/Quit (S/Q) : s Betty - Wilma - Fred No prize Spin/Quit (S/Q) : s Fred - Fred - Barney Won 50 cents Spin/Quit (S/Q) : q) Version 3 (30 Points) Create a Java solution for the game SlotMachine3. Start by copying your working code from SlotMachine2 as the starting point for this version. Your solution should have three methods: main, checkWinner, and spinWheel Revision of SlotMachine2 with the following changes: In the main method: Add a double variable to store the player's money. Prompt the user for how much money to insert when starting the game, and store that value in the money variable. Each time the player chooses to Spin, subtract 25 cents to pay for the spin, then add back any amount the player won using the value returned by the checkWinner method. Add print statements to match the sample output. (Sample Output: SlotMachine3 >java SlotMachine3 How much money would you like to insert? 100 Spin/Quit (S/Q) : s Paid 0.25 to spin.... Barney - Betty - Betty Won 50 cents You now have $ 100.25 Spin/Quit (S/Q) : s Paid 0.25 to spin.... Betty - Wilma - Fred No prize You now have $ 100.00 Spin/Quit (S/Q) : s Paid 0.25 to spin.... Wilma - Betty - Betty Won 50 cents You now have $ 100.25 Spin/Quit (S/Q) : s Paid 0.25 to spin.... Wilma - Fred - Fred Won 50 cents You now have $ 100.50 Spin/Quit (S/Q) : q Attachments
Tags Clarifications

Answers

SlotMachine - Solution

mariobeka
mariobeka 



Find below the zip file containing the solution you need. Attachments
Solution.zip (2 K) Download
Preview
static final String names[] = {"Wilma", "Fred", "Barney", "Betty"}; public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input; String

Preview
(!input.equalsIgnoreCase("q")); } private static String spinWheel() { return names[new Random().nextInt(names.length)]; } private static double checkWinner(String first, String second, String third) { if (first.equals(second) & second.equals(third)) { System.out.println("Won $1"); return 1.0; } else if (first.equals(second) & !second.equals(third)) { System.out.println("Won 50 cents"); return 0.5; }

Preview
private static final String names[] = {"Wilma", "Fred", "Barney", "Betty"}; public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input; String firstResult; String secondResult; String thirdResult; System.out.print("How much money would you like to insert? "); double money = sc.nextDouble(); do { System.out.print("Spin/Quit (S/Q): "); input = sc.next();

Screen Shot 2017-03-25 at 02.12.50.png (49 K) Download Preview

Screenshots
Screenshot
Purchase Answer