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: 3774516 • 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.io.File;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class SlotMachine {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       Scanner sc=new Scanner(System.in);
       int h = 0;
       String ch = "Y";
      
       File f = new File("e:/high.dat");
       boolean exists = f.exists();
         
       if(exists) {
           try {
               FileReader r = new FileReader("e:/high.dat");
               h = r.read();
               //System.out.println("Highest win: "+h);
               r.close();
              
           } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
             
       }
       int tot = 0, tot2=0;     
      
       while(ch.equals("Y")){
          
           int m = -1;
           while(m<0){
               System.out.println("Enter Money: ");   
               m=sc.nextInt();
           }
           String x1,x2,x3;
           tot2+=m;
           x1 = getChoice();
           x2 = getChoice();
           x3 = getChoice();
           System.out.println(x1+" "+x2+" "+x3);
          
           if(x1.equals(x2) && x2.equals(x3)){
               display(m*3);
               tot+=m*3;
           }
           else if(x1.equals(x2)|| x2.equals(x3) || x3.equals(x1)){
               display(m*2);
               tot+=m*2;
           }
           else
               display(0);
          
           System.out.println("Do you wish to continue? (Y/N): ");
           ch=sc.next();
       }
       System.out.println("Money entered in the slot in this session: "+tot2);
       System.out.println("Total amount won in this session: "+tot);
       if(tot2>tot)
           System.out.println("Total loss: "+(tot2-tot));
       else
           System.out.println("Total gain: "+(tot-tot2));
      
       if(h<(tot - tot2) || !exists){
           try {
               FileWriter out = new FileWriter("E:/high.dat", false);
               out.write(tot-tot2);
               out.close();
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
       sc.close();
      
   }
  
   public static String getChoice(){
      
       String[] c = {"Cherries", "Oranges", "Plums", "Bells", "Melons", "Bars"};
       Random rand = new Random();
       int i = rand.nextInt(5);
       return c[i];
      
   }
  
   public static void display(int s){
       System.out.println("You won: "+s);
   }

}