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

You are to write a program using Java that will simulate a slot machine with four...
You are to write a program using Java that will simulate a slot machine with four wheels. It will determine the amount of money won for each spin. The four wheels spin and stop showing a value between 0 and 9 inclusive. It costs $2 to play. •You win $500 (but not the $2 you bet) if you get 4 wheels the same. •You win $10 (but not the $2 you bet) if you get exactly 3 of a kind....
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 program 1-)write a program that simulates a vending machine, takes input from the...
Write a Java program 1-)write a program that simulates a vending machine, takes input from the user (money), and returns the change to the user. The change means Quarter =   25 cent Dime = 10 cent Nickels = 5 cent Pinneies = 1 cent 2-) What is the output produced by the following code? int result = 11; result /= 2; System.out.println("resu lt is " + result);
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....
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details...
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details don't matter code must just have the following: Design and implement an inheritance hierarchy that includes Vehicle as an abstract superclass and several subclasses. Include a document containing a UML diagram describing your inheritance hierarchy. Include at least one interface that contains at least one method that implementing classes must implement. Include functionality to write the results of the race to a file; this...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
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....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT