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

package midtermpractice; import java.util.Random; public class SlotMachine { pri

ID: 3725496 • Letter: P

Question

package midtermpractice; import java.util.Random;
public class SlotMachine
{ private int ranNum1; private int ranNum2; private int ranNum3;
public SlotMachine() { ranNum1 = 0; ranNum2 = 0; ranNum3 = 0; }

public int getRanNum1() { return ranNum1; }
public int getRanNum2() { return ranNum2; }
public int getRanNum3() { return ranNum3; }
public void setRanNum1(int aRanNum1) { ranNum1 = aRanNum1; }
public void setRanNum2(int aRanNum2) { ranNum2 = aRanNum2; }
public void setRanNum3(int aRanNum3) { ranNum3 = aRanNum3; }
public String toString() { return "You pulled the handle, and the slot machine gave you these 3 numbers: " + ranNum1 + " " + ranNum2 + " " + ranNum3; } }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package midtermpractice; import java.util.Random; /** * * @author */ public class MidtermPractice { public static SlotMachine aSM; public static int numTokens = 20;
/** * @param args the command line arguments */ public static void main(String[] args) { String userPlayAgain = "yes"; //Instantiate a SlotMachine object: aSM = new SlotMachine();

// Task #1: // In main, add a loop that will keep repeating the execution of playSlotMachine() and determineWinner(), as long as the user replies "yes" to the question, do you want to play again, // AND the user still has tokens left. The variable numTokens is intialized to 20, and that each time you play the slot machine, you have to "spend" 2 tokens, not just 1.
playSlotMachine(); determineWinner(); }
public static void playSlotMachine() { Random myRan = new Random(); int num1, num2, num3;
// Task #2: // Inside the playSlotMachine() method, generate 3 random numbers between 1 and 7, and use the setter methods of the SlotMachine object to update each one. // Remember that a random number between 1 and 10 is generated with the following code: num1 = myRan.nextInt(10) + 1;




}
package midtermpractice; import java.util.Random;
public class SlotMachine
{ private int ranNum1; private int ranNum2; private int ranNum3;
public SlotMachine() { ranNum1 = 0; ranNum2 = 0; ranNum3 = 0; }

public int getRanNum1() { return ranNum1; }
public int getRanNum2() { return ranNum2; }
public int getRanNum3() { return ranNum3; }
public void setRanNum1(int aRanNum1) { ranNum1 = aRanNum1; }
public void setRanNum2(int aRanNum2) { ranNum2 = aRanNum2; }
public void setRanNum3(int aRanNum3) { ranNum3 = aRanNum3; }
public String toString() { return "You pulled the handle, and the slot machine gave you these 3 numbers: " + ranNum1 + " " + ranNum2 + " " + ranNum3; } }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package midtermpractice; import java.util.Random; /** * * @author */ public class MidtermPractice { public static SlotMachine aSM; public static int numTokens = 20;
/** * @param args the command line arguments */ public static void main(String[] args) { String userPlayAgain = "yes"; //Instantiate a SlotMachine object: aSM = new SlotMachine();

// Task #1: // In main, add a loop that will keep repeating the execution of playSlotMachine() and determineWinner(), as long as the user replies "yes" to the question, do you want to play again, // AND the user still has tokens left. The variable numTokens is intialized to 20, and that each time you play the slot machine, you have to "spend" 2 tokens, not just 1.
playSlotMachine(); determineWinner(); }
public static void playSlotMachine() { Random myRan = new Random(); int num1, num2, num3;
// Task #2: // Inside the playSlotMachine() method, generate 3 random numbers between 1 and 7, and use the setter methods of the SlotMachine object to update each one. // Remember that a random number between 1 and 10 is generated with the following code: num1 = myRan.nextInt(10) + 1;




}
package midtermpractice; import java.util.Random;
public class SlotMachine
{ private int ranNum1; private int ranNum2; private int ranNum3;
public SlotMachine() { ranNum1 = 0; ranNum2 = 0; ranNum3 = 0; }

public int getRanNum1() { return ranNum1; }
public int getRanNum2() { return ranNum2; }
public int getRanNum3() { return ranNum3; }
public void setRanNum1(int aRanNum1) { ranNum1 = aRanNum1; }
public void setRanNum2(int aRanNum2) { ranNum2 = aRanNum2; }
public void setRanNum3(int aRanNum3) { ranNum3 = aRanNum3; }
public String toString() { return "You pulled the handle, and the slot machine gave you these 3 numbers: " + ranNum1 + " " + ranNum2 + " " + ranNum3; } }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package midtermpractice; import java.util.Random; /** * * @author */ public class MidtermPractice { public static SlotMachine aSM; public static int numTokens = 20;
/** * @param args the command line arguments */ public static void main(String[] args) { String userPlayAgain = "yes"; //Instantiate a SlotMachine object: aSM = new SlotMachine();

// Task #1: // In main, add a loop that will keep repeating the execution of playSlotMachine() and determineWinner(), as long as the user replies "yes" to the question, do you want to play again, // AND the user still has tokens left. The variable numTokens is intialized to 20, and that each time you play the slot machine, you have to "spend" 2 tokens, not just 1.
playSlotMachine(); determineWinner(); }
public static void playSlotMachine() { Random myRan = new Random(); int num1, num2, num3;
// Task #2: // Inside the playSlotMachine() method, generate 3 random numbers between 1 and 7, and use the setter methods of the SlotMachine object to update each one. // Remember that a random number between 1 and 10 is generated with the following code: num1 = myRan.nextInt(10) + 1;




}

Explanation / Answer

// Class SlotMachine definition

public class SlotMachine

{

// Instance variables to store random numbers

private int ranNum1;

private int ranNum2;

private int ranNum3;

// Default constructor

public SlotMachine()

{

ranNum1 = 0;

ranNum2 = 0;

ranNum3 = 0;

}// End of constructor

// Method to return random number 1

public int getRanNum1()

{

return ranNum1;

}// End of method

// Method to return random number 2

public int getRanNum2()

{

return ranNum2;

}// End of method

// Method to return random number 3

public int getRanNum3()

{

return ranNum3;

}// End of method

// Method to set random number 1

public void setRanNum1(int aRanNum1)

{

ranNum1 = aRanNum1;

}// End of method

// Method to set random number 2

public void setRanNum2(int aRanNum2)

{

ranNum2 = aRanNum2;

}// End of method

// Method to set random number 3

public void setRanNum3(int aRanNum3)

{

ranNum3 = aRanNum3;

}// End of method

// To display 3 random numbers.

public String toString()

{

return "You pulled the handle, and the slot machine gave you these 3 numbers: " + ranNum1 + " " + ranNum2 + " " + ranNum3;

}// End of method

}// End of class

------------------------------------------------

import java.util.*;

// Class MidtermPractice definition

public class MidtermPractice

{

// Instance variables

public static SlotMachine aSM;

public static int numTokens = 20;

// main method definition

public static void main(String[] args)

{

String userPlayAgain = "yes";

//Instantiate a SlotMachine object:

aSM = new SlotMachine();

// Scanner class object created

Scanner sc = new Scanner(System.in);

// Task #1:

// In main, add a loop that will keep repeating the execution of playSlotMachine() and determineWinner(),

// as long as the user replies "yes" to the question, do you want to play again,

// AND the user still has tokens left. The variable numTokens is intialized to 20,

// and that each time you play the slot machine, you have to "spend" 2 tokens, not just 1.

do

{

// Calls the method to generate 3 random numbers

playSlotMachine();

// Calls the method to determine winner

determineWinner();

// Accepts user choice to continue or not

System.out.println("Do you want to play again? ");

userPlayAgain = sc.next();

// Checks if the numTokens is less than zero or userPlayAgain is not "yes" then stop

if(!userPlayAgain.equalsIgnoreCase("yes") || numTokens < 0)

break;

}while(true); // End of do - while

}// End of main method

// Method to generate three random numbers

public static void playSlotMachine()

{

// Random class object created

Random myRan = new Random();

// To store three random numbers

int num1, num2, num3;

// Task #2:

// Inside the playSlotMachine() method, generate 3 random numbers between 1 and 7,

// and use the setter methods of the SlotMachine object to update each one.

// Remember that a random number between 1 and 10 is generated with the following code:

// num1 = myRan.nextInt(10) + 1;

num1 = myRan.nextInt(7);

num2 = myRan.nextInt(7);

num3 = myRan.nextInt(7);

// Sets three random numbers by calling setRanNum method

aSM.setRanNum1(num1);

aSM.setRanNum2(num2);

aSM.setRanNum3(num3);

// Displays three random numbers

System.out.println(aSM);

}// End of method

// Method to determine the winner

public static void determineWinner()

{

// Checks if number1, number2 and number3 all are same

if(aSM.getRanNum1() == aSM.getRanNum2() && aSM.getRanNum2 ()== aSM.getRanNum3() && aSM.getRanNum1() == aSM.getRanNum3())

// Add three to numTokens

System.out.println("You win: " + (numTokens += 3));

// Otherwise, checks if either number1 and number2 same or numberw2 and number3 same

else if (aSM.getRanNum1() == aSM.getRanNum2() || aSM.getRanNum2() == aSM.getRanNum3())

// Add two to numTokens

System.out.println("You win: " + (numTokens += 2));

// Otherwise deduct two from numTokens

else

System.out.println("Balance " + (numTokens -= 2));

}// End of method

}// End of class

Sample Output:

You pulled the handle, and the slot machine gave you these 3 numbers:
1 3 2
Balance 18
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
1 4 5
Balance 16
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
1 2 0
Balance 14
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
2 3 3
You win: 16
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
3 2 4
Balance 14
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
3 3 2
You win: 16
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
2 2 1
You win: 18
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
3 0 6
Balance 16
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
4 1 3
Balance 14
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
2 3 4
Balance 12
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
2 5 1
Balance 10
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
6 1 5
Balance 8
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
0 2 2
You win: 10
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
3 3 2
You win: 12
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
0 5 1
Balance 10
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
6 3 6
Balance 8
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
4 2 6
Balance 6
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
2 4 1
Balance 4
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
6 1 6
Balance 2
Do you want to play again?
yes
You pulled the handle, and the slot machine gave you these 3 numbers:
3 2 5
Balance 0
Do you want to play again?
yes