Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEEDED IN BASIC BEGINNER JAVA A slot machine is a gambling device that the user

ID: 3775583 • Letter: N

Question

NEEDED IN BASIC BEGINNER JAVA A slot machine is a gambling device that the user inserts money into then pulls a lever (or presses a button). The slot machine then displays a set of random images. If two or more of the images match, the user wins an amount of money that the slot machine dispenses back to the user. Create a program that simulates a slot machine. The program should contain at least 2 methods (in addition to the main method) – one void method and one value-returning method – at least one of the methods should include arguments. 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. You should validate this input to make sure the user enters a positive value for the bid amount. Instead of displaying images, the program will randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars. To select a word, the program will 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 this list three times and display all three of the words. If none of the randomly selected words match, the program will inform the user that he or she has won $0. If two of the words match, the program will inform the user that he or she has won two times the amount entered. If three of the words match, the program will inform the user that he or she has won three times the amount entered. The program will ask whether or not 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 for this session, the total amount won this session, and the net loss/gain. In addition, the program will keep a record of the highest gain ever achieved while playing the game. The value will be written to a file between game sessions, so that it can be read from the file with each new session. If the user exceeds the record gain, a message indicating this is a record gain should be displayed, the value of the previous record gain should be displayed, and the new record gain should be stored. (Hint: If the file doesn’t exist, no record has been set yet.) Please remember to use good programming practices – naming conventions, indentation, comments, etc. All currency output should be formatted appropriately

Explanation / Answer

import java.util.Random;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import static java.nio.file.StandardOpenOption.APPEND;
public class HelloWorld{

     public static int amount = 0, big = 0, a[]= new int[6];
     public static float new_amount = 0;
     static int count1=0, count2=0, count3=0, count4=0, count5=0, count6=0;
     public static String[] words = {"Cherries", "Oranges", "Plums", "Bells", "Melons", "Bars"};
     public static void main(String []args) throws IOException{
        
          int net; char CH;
         System.out.println("enter the amount of money he or she wants to enter into the slot machine : ");
         Scanner sc = new Scanner(System.in);
         amount = sc.nextInt();
         if(amount < 0){
                System.out.println("Invalid amount");
         }
         rand_number();
        do{
            a[0] = count1; a[1] = count2; a[2] = count3; a[3] = count4;
            a[4] = count5; a[5] = count6;
            big=a[0]; int n = 0;
            for(int i=1; i<a.length; i++)
            {
              if(big<a[i]){
                 big=a[i];
                 n = i;
               }
            }
            for(int j=0; j<big; j++)
             System.out.println(words[n]);
             new_amount = amount * big;
           if(big <= 0)
                 System.out.println("you won $0 ");
           else       
                System.out.println("won " +big +" times the amount entered : $"+new_amount);
           System.out.println("total amount of money entered into the slot machine for this session : $" +new_amount);
           if(new_amount > amount)
               System.out.println("Gain");
            else
               System.out.println("Loss");
            try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("file.txt"))){
                  writer.write("total amount of money entered into the slot machine for this session : $" +new_amount);
               
            }
          
           System.out.println("you wants to play more (y/Y)?");
           Scanner sc1 = new Scanner(System.in);
             CH = sc1.next().charAt(0);
             if(CH == 'Y' || CH == 'y'){
               big = 0;amount = 0;
               System.out.println("enter the amount of money he or she wants to enter into the slot machine : ");
                 Scanner sc2 = new Scanner(System.in);
                 amount = sc2.nextInt();
                 if(amount < 0){
                     System.out.println("Invalid amount");
                }
                rand_number();
             }
        }while(CH == 'Y' || CH == 'y');
      
     }
public static void cal_amount(int r){

        if(words[r] == "Cherries" )
              count1++;
        if(words[r] == "Oranges" )
              count2++;
        if(words[r] == "Plums" )
              count3++;       
        if(words[r] == "Bells" )
              count4++;
        if(words[r] == "Melons" )
              count5++;
        if(words[r] == "Bars" )
              count6++;
}
public static void rand_number()
{
      Random rn = new Random();
      count1=0; count2=0; count3=0; count4=0; count5=0; count6=0;
       for(int i = 0; i < 5; i++)
       {
          int rand = rn.nextInt(5);
          System.out.println(rand);
          cal_amount(rand);
       }
}
}