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

Description: Design and implement an application that simulates a simple slot ma

ID: 3730394 • Letter: D

Question

Description:

Design and implement an application that simulates a simple slot machine.

Requirements:

1. Write a slot machine that has the following functionalities (assuming all coins are the same):

a. Entering coins: adding coins to the slot machine;

b. Pulling the lever: generating three random numbers;

c. Paying out: depending the outcome of pulling the lever (the three numbers), pay the

player coins according to the following rules: (1) If all three numbers are equal to 1, the

player has hit a jackpot – pay the player the number of coins he/she has entered

multiplied by a random integer ranging from 1 to 10; (2) If all three numbers are equal,

but not 1, then pay the player the number of coins entered multiplied by 4; (3) If only

two out of the three are equal, then pay the player the number of coins entered

multiplied by 2; (4) if none of the numbers are equal, then pay nothing.

2. Write a driver program that uses the slot machine class above to simulate a game by:

a. Asking the player for the number of coins he/she has in hand; validating the player input

(must be positive);

b. Creating a slot machine;

c. Repeatedly doing the following:

i. Asking the player how many coins he/she would like to bet;

ii. Validating the player input – must be a number between 1 and the number of

coins the player has in hand at that moment;

iii. Entering the coin into the slot machine;

iv. Pulling the lever of the slot machine;

v. Calculating the payout amount and paying it to the player, increasing the

number of coins he/she has in hand; printing out the payout he/she gets this

time and the total number of the coins he/she now has in hand;

vi. Declaring the game is over when the player has no more coins left in hand;

vii. Asking whether the player would like to continue playing if he/she still has some

coins in hand; terminating the game when the player enters “n” or “N”;

continuing otherwise.

Marking:

Your submission will be marked on correctness, style (indentation, variable naming, etc.), and

comments. Code that does not compile can receive at most 20/100.

Sample Run 1:

How many coins do you have?

5

How many coins would you like to bet?

1

You get 2 coins. You now have 6 coins.

Continue? Enter 'n' to end the game, or anything else to continue.

y

How many coins would you like to bet?

2

You get 4 coins. You now have 8 coins.

Continue? Enter 'n' to end the game, or anything else to continue.

y

How many coins would you like to bet?

5

You get 0 coins. You now have 3 coins.

Continue? Enter 'n' to end the game, or anything else to continue.

y

How many coins would you like to bet?

2

You get 0 coins. You now have 1 coins.

Continue? Enter 'n' to end the game, or anything else to continue.

y

How many coins would you like to bet?

2

Invalid input. Please enter a number between 1 and 1

How many coins would you like to bet?

1

You get 0 coins. You now have 0 coins.

No more coins. Game over.

Sample Run 2:

How many coins do you have?

4

How many coins would you like to bet?

2

You get 0 coins. You now have 2 coins.

Continue? Enter 'n' to end the game, or anything else to continue.

n

(THE QUESTION SHOULD BE ANSWERED IN JAVA)

Explanation / Answer

// This program is based on the requirement of slot machine and I have taken 3 random numbers whose max limit is //10. If you want you can change the limit of random number to check. It is covering all cases here. I am modifying here with introducing Driver Class

#1 Driver Class which will call method of SlotMachine Class

import java.util.Scanner;

public class DriverClass {

public static void main(String[] args) {

Scanner sc =new Scanner(System.in);

System.out.println("Would like to play with slot machine? Please answer in y/n");

String answer=sc.nextLine();

SlotMachine.slotMachine(answer, sc);

}

}

#2 SlotMachine Class

import java.util.Random;

import java.util.Scanner;

public class SlotMachine {

public static void slotMachine(String answer,Scanner sc) {

//Scanner sc =new Scanner(System.in);

//System.out.println("Would like to play with slot machine? Please answer in y/n");

//String answer=sc.nextLine();

int remainCoin=0;

while(answer.equalsIgnoreCase("y")){

Random ram=new Random();

if(remainCoin>0){

System.out.println("Now you have "+remainCoin+" Coins");

}else{

System.out.println("How many coins do you have?");

remainCoin=sc.nextInt();

}

System.out.println("How many coins would like to bet?");

int betCoins=sc.nextInt();

if(remainCoin>=betCoins){

remainCoin=remainCoin-betCoins;

}else{

System.out.println("Please enter valid coin to bet which is less than or equal to your balanced coin");

continue;

}

System.out.println("After bet you have "+remainCoin);

int x=ram.nextInt(10);

int y=ram.nextInt(10);

int z=ram.nextInt(10);

if(x==y && y==z && z==x && x==1 && y==1 && z==1){

System.out.println("JACKPOT!!!");

remainCoin=betCoins*(ram.nextInt(10-1)+1);

System.out.println("Now you have "+remainCoin+" Coins");

}

else if(x==y && y==z && z==x && x!=1 && y!=1 && z!=1){

remainCoin=betCoins*4;

System.out.println("Cool you were very close to jackpot, all numbers are same. Try again for Jackpot");

System.out.println("Now you have "+remainCoin+" coins");

}

else if(x==y || y==z || x==z){

remainCoin=betCoins*2;

System.out.println("Nice try you were close to win. Try again for Jackpot");

System.out.println("Now you have "+remainCoin+" coins");

}

else if(x!=y && x!=z && y!=z){

System.out.println("Now you have "+remainCoin);

}

if(remainCoin==0){

System.out.println("Game is over !!! you have "+remainCoin+" coin");

}

if(remainCoin>0){

System.out.println("Would you like to continue play as you have "+remainCoin+" Coins Please press y to continue or n for exit the game");

Scanner sc1 =new Scanner(System.in);

answer=sc1.nextLine();

System.out.println();

}else{

System.out.println("Thanks for playing. Bye!!!");

break;

}

}

if(answer.equalsIgnoreCase("n"))

System.out.println("Thanks for playing. Bye!!!");

}

}