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

Here are the assigment\'s instructions for writing a java program. My code works

ID: 3673824 • Letter: H

Question

Here are the assigment's instructions for writing a java program. My code works fine, but I am having problems trying to figure out how to turn separate my code that is fully written in the main() into modules. The assignment calls for at least 3 modules, one of which can take 2 parameters, and one that returns a value.

Here are the instructions for the assignment:

Design 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 s/he wants to insert into the slot machine

Instead of displaying images, the program will randomly select a word from the following list: Cherries, Oranges, Plums, bells, melons, and bars The program will select and display a word from this list three times.

If none of the randomly selected words match, the program will inform the user that s/he has won $0. If two of the words match, the program will inform the user that s/he has won two times the amount of money entered. If three of the words match, the program will inform the user that s/he has won three times the amount entered.

The program will run once, and then exit. In order to get full points, you must have:

A module or function which takes at least two parameters (pass by value)

A module or function which returns a value

A minimum of 3 functions or modules.

Here is my code:

import java.io.*;
import java.util.Random;

public class SlotMachine {

static String[] slotSymbols={"Cherries","Oranges","Plums","Bells","Melons","Bars"};

public static void main(String[] args){

//User input
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);

//Declarations
int wallet = 1000;
int betAmount;

//Catch exceptions
try{
System.out.println("You have $" +wallet);
System.out.println("How much would you like to bet?");

//Input amount to bet
betAmount = Integer.parseInt(reader.readLine());

//Ensure proper funds for bet
if(betAmount > wallet){
System.out.println("You do not have enough money to bet that amount.");
}

else{
Random spinReels = new Random();;
int spin1;
int spin2;
int spin3;

spin1 = spinReels.nextInt(5);
spin2 = spinReels.nextInt(5);
spin3 = spinReels.nextInt(5);


System.out.println(" ---" +slotSymbols[spin1] +"---" +slotSymbols[spin2] +"---" +slotSymbols[spin3] +"--- ");

//3 matching reels
if(spin1 == spin2 && spin2 == spin3){
wallet = wallet + (betAmount * 3);
System.out.println(">\\\\JACKPOT! YOU WIN! +$"+(betAmount * 3)+"//////// ");
}


//2 matching reels
else if(spin1 == spin2 || spin1 == spin3 || spin2 == spin3){
wallet = wallet + (betAmount * 2);
System.out.println(">\\\\YOU WIN +$"+(betAmount*2)+"//////// ");
}


//No matching reels
else{
wallet = wallet - betAmount;
System.out.println(">LOSE -$" +betAmount +" ");
}


//Game over
if(wallet <= 0){
System.out.println("You have lost all of your money. Game over.");
}
}
}
}catch (IOException e){
System.out.println("Error reading from user");
}

/******************************************************************************
* Methods/Functions
******************************************************************************/

//???

}

Explanation / Answer

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

public class SlotMachine {

   static String[] slotSymbols={"Cherries","Oranges","Plums","Bells","Melons","Bars"};
  
   // Method to check equality of two string
   public static boolean isEqual(int s1, int s2){
       return s1==s2;
   }
   // to get bet ammount
   public static int getBetAmmount(){
       Scanner sc = new Scanner(System.in);
       System.out.println("How much would you like to bet?");
       return sc.nextInt();
   }
   public static int getWonAmount(int betAmmount, int n){
       return betAmmount*n;
   }
  

   public static void main(String[] args){

       int wallet = 1000;
       int betAmount;
       System.out.println("You have $" +wallet);
      
       betAmount = getBetAmmount();
       //Ensure proper funds for bet
       if(betAmount > wallet){
           System.out.println("You do not have enough money to bet that amount.");
       }
       else{
           Random spinReels = new Random();;
           int spin1;
           int spin2;
           int spin3;
           spin1 = spinReels.nextInt(5);
           spin2 = spinReels.nextInt(5);
           spin3 = spinReels.nextInt(5);

           System.out.println(" ---" +slotSymbols[spin1] +"---" +slotSymbols[spin2] +"---" +
                               slotSymbols[spin3] +"--- ");

           //3 matching reels
           if(isEqual(spin1, spin2) && isEqual(spin2, spin3)){
               wallet = wallet + getWonAmount(betAmount, 3);
               System.out.println(">\\\\JACKPOT! YOU WIN! +$"+(betAmount * 3)+"//////// ");
           }

           //2 matching reels
           else if(isEqual(spin1, spin2) || isEqual(spin2, spin3) || isEqual(spin1, spin3)){
               wallet = wallet + getWonAmount(betAmount, 2);
               System.out.println(">\\\\YOU WIN +$"+(betAmount*2)+"//////// ");
           }

           //No matching reels
           else{
               wallet = wallet - betAmount;
               System.out.println(">LOSE -$" +betAmount +" ");
           }

           //Game over
           if(wallet <= 0){
               System.out.println("You have lost all of your money. Game over.");
           }
       }
   }
}

/*

Output:

You have $1000
How much would you like to bet?
200

---Melons---Cherries---Plums---

>LOSE -$200

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote