Question

In: Computer Science

Java - Create a program that simulates a slot machine. When the program runs, it should...

Java - Create a program that simulates a slot machine. When the program runs, it should do the following: - Ask the user to enter the amount of money he or she wants to enter into the slot machine. - Instead of displaying images, have the program randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars (To select a word, the program can generate a random number in the range of 0 through 5. If the number is 0, the selected word is Cherries, if the number is 1, the selected word is Oranges, and so forth. The program should randomly select a word from the list three times and display all three of the words.) - If none of the randomly selected words match, the program informs the user that he or she has won $0. If two of the words match, the program informs the user that he or she has won two times the amount entered. If three of the words match, the program informs the user that he or she has won three times the amount entered. - The program asks if the user wants to play again. If so, these steps are repeated. If not, the program displays the total amount of money entered into the slot machine and the total amount won.

If you could just take a screen shot of the actual code on java that would be great. A lot of the characters appear differently when answer is copied and pasted on answer forum.

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// SlotMachine.java

import java.util.Random;
import java.util.Scanner;

public class SlotMachine {

   public static void main(String[] args) {
       //Declaring variables
       char ch;
int totalWon=0,totalEntered=0,money,won=0;
String randWord1,randWord2,randWord3;
int randNum;
//Creating an Array
String str[]={"Cherries","Oranges","Plums","Bells","Melons","Bars"};
//Creating an random class object
Random r = new Random();
        /*
        * Creating an Scanner class object which is used to get the inputs
        * entered by the user
        */
        Scanner sc = new Scanner(System.in);
       
    /* This while loop continues to execute
    * until the user enters either 'N' or 'n'
    */
        while(true)
        {
            won=0;
            System.out.print("\nEnter the amount of money you want to enter :$");
            money=sc.nextInt();
            totalEntered+=money;
            randNum=r.nextInt(6);
            randWord1=str[randNum];
            randNum=r.nextInt(6);
            randWord2=str[randNum];
            randNum=r.nextInt(6);
            randWord3=str[randNum];
            int cnt=countSameWords(randWord1,randWord2,randWord3);
            System.out.println("No of words matched :"+cnt);
            if(cnt==0)
            {
                totalWon+=0;
                won+=0;
            }
            else if(cnt==2)
            {
                totalWon+=2*money;
                won+=2*money;
            }
            else if(cnt==3)
            {
                totalWon+=3*money;
                won+=3*money;
            }
           
            System.out.println("You won amount :$"+won);
        System.out.print("\nDo you want to play again (Y/N) ::");
        ch = sc.next(".").charAt(0);
        if(ch=='Y'||ch=='y')
        continue;
        else
        {
        System.out.println(":: Program Exit ::");
        break;
        }

        }
        System.out.println("\nTotal Entered amount :$"+totalEntered);
        System.out.println("Total Won amount :$"+totalWon);
       
   }

   private static int countSameWords(String randWord1, String randWord2,
           String randWord3) {
       if(randWord1.equals(randWord2) && randWord2.equals(randWord3))
       return 3;
       else if(randWord1.equals(randWord2) || randWord2.equals(randWord3))
       return 2;
       else
       return 0;  
   }

}

_____________________

Output:


Enter the amount of money you want to enter :$100
No of words matched :2
You won amount :$200

Do you want to play again (Y/N) ::y

Enter the amount of money you want to enter :$50
No of words matched :2
You won amount :$100

Do you want to play again (Y/N) ::n
:: Program Exit ::

Total Entered amount :$150
Total Won amount :$300

_______________Could you plz rate me well.Thank You


Related Solutions

Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
Write a java console application,. It simulates the vending machine and ask two questions. When you...
Write a java console application,. It simulates the vending machine and ask two questions. When you run your code, it will do following: Computer output: What item you want? User input: Soda If user input is Soda Computer output: How many cans do you want? User input:            3 Computer output: Please pay $3.00. END The vending machine has 3 items for sale: Soda the price is $1.50/can. Computer should ask “How many cans do you want?” Chips, the price is $1.20/bag....
Java Coding Background You will create a Java class that simulates a water holding tank. The...
Java Coding Background You will create a Java class that simulates a water holding tank. The holding tank can hold volumes of water (measured in gallons) that range from 0 (empty) up to a maximum. If more than the maximum capacity is added to the holding tank, an overflow valve causes the excess to be dumped into the sewer system. Assignment The class will be named HoldingTank. The class attributes will consist of two int fields – current and maxCapacity....
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). Your shopping cart should support...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). Using the CLASSES BELOW Your...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). **PLEASE USE THE CLASSES BELOW***...
Write a java program that simulates thousands of games and then calculate the probabilities from the...
Write a java program that simulates thousands of games and then calculate the probabilities from the simulation results. Specifically in the game, throw two dice, the possible summations of the results are: 2, 3, ..., 12. You need to use arrays to count the occurrence and store the probabilities of all possible summations. Try to simulate rolling two dice 100, 1000, 10,0000 times, or more if needed. Choose one simulation number so that the probabilities you calculated is within 1%...
3. Write a program that simulates a vending machine.   The user will be prompted to enter...
3. Write a program that simulates a vending machine.   The user will be prompted to enter a number then a letter. As part of the prompt you must display a message to indicate the number and letter, along with the possible choices. That is, what selections will give user which item.   If the user enters a number or a letter that is not valid, then display the message “Bad Entry” and end the program. (100 pts) Selections with messages. 1a...
Write a program that simulates a vending machine. The machine holds six snack items labeled them...
Write a program that simulates a vending machine. The machine holds six snack items labeled them 1 through 6. The program should initially display a menu of items along with their prices: Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00 The program then should ask the user to enter the item to purchase along with a sum of money....
Design an embedded system using a MSP430 to create a slot machine. Include a complete description...
Design an embedded system using a MSP430 to create a slot machine. Include a complete description of a slot machine designed using logic circuits. You must submit a complete design with all the hardware and software needs, include a c program that utilizes the msp430 for this.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT