in java import java.util.Scanner; import java.util.Random; import java.util.conc
ID: 3834037 • Letter: I
Question
in java
import java.util.Scanner;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class SlotMachine
{
/** Play a slot machine. The user is initially given 20 tokens and will see three
* numbers. If all three numbers match and are 7s, the user will win 750 points. If
* all three numbers match but are not 7s, the user will win 75 points. If two numbers
* are 7s, the user will get 20 points, if two numbers match and are not seven, the user
* will win 5 points. If no numbers match the user loses.
* @param args There are no command line arguments.
*
* @author Deborah A. Trytten
*/
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
final int INITIAL_TOKENS = 20;
int[] digits;
int userTokens = INITIAL_TOKENS;
String response= "Yes";
while (userTokens > 0 && response.equalsIgnoreCase("yes"))
{
// spin the wheel
// Call generateRandom Digits and show contents to user
// Call calculateWin
int result = 0 ; // this will hold the number of points won or lost
System.out.println("You earned " + result + " points.");
userTokens = userTokens + result;
System.out.println("You now have " + userTokens + " tokens.");
if (userTokens != 0)
{
System.out.println("Would you like to play again? Yes/No");
response = input.nextLine();
}
}
input.close();
}
/** Create an array that contains size random digits, each between low
* and high (inclusive).
*
* @param low The smallest random int that will be generated (inclusive)
* @param high The largest random int that will be generated (inclusive)
* @param size The number of random int values to be generated in the array
* @return A newly constructed array containing size random digits between
* low and high (inclusive)
*/
public static int[] generateRandomDigits(int low, int high, int size)
{
return null; // this is a stub
}
/** Find a random int uniformly distributed between low and high (inclusive).
* @param low The lower bound for the random number (inclusive)
* @param high The upper bound for the random number (inclusive)
* @return A random int uniformly distributed between low and high (inclusive).
*/
public static int findRandom(int low, int high)
{
return 0; // this is a stub
}
/** Determine how many points the user won. The result of logic are in the
* class preamble.
* @param x The digit on the first wheel.
* @param y The digit on the second wheel.
* @param z The digit on the third wheel.
* @return The score for this round.
*/
// This method uses pass by sharing
public static int calculateWin(int[] digits)
{
return 0; // This is a stub
}
}
complete this code to get this outcome
You have 20 tokens.
Your spin was 10 7 3
You earned -1 points.
You now have 19 tokens.
Would you like to play again? Yes/No
Yes
Your spin was 8 10 4
You earned -1 points.
You now have 18 tokens.
Would you like to play again? Yes/No
Yes
Explanation / Answer
Please find program with sample output:
import java.util.Scanner;
import java.util.Set;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class SlotMachine
{
/** Play a slot machine. The user is initially given 20 tokens and will see three
* numbers. If all three numbers match and are 7s, the user will win 750 points. If
* all three numbers match but are not 7s, the user will win 75 points. If two numbers
* are 7s, the user will get 20 points, if two numbers match and are not seven, the user
* will win 5 points. If no numbers match the user loses.
* @param args There are no command line arguments.
*
* @author Deborah A. Trytten
*/
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
final int INITIAL_TOKENS = 20;
int[] digits;
int userTokens = INITIAL_TOKENS;
String response= "Yes";
while (userTokens > 0 && response.equalsIgnoreCase("yes"))
{
System.out.println("You have "+userTokens+" tokens.");
// spin the wheel
// Call generateRandom Digits and show contents to user
digits = generateRandomDigits(0, 9, 3);
System.out.print("Your spin was ");
for(int digit: digits){
System.out.print(digit + " ");
}
System.out.println();
// Call calculateWin
int result = 0 ; // this will hold the number of points won or lost
result = calculateWin(digits);
System.out.println("You earned " + result + " points.");
userTokens = userTokens + result;
System.out.println("You now have " + userTokens + " tokens.");
if (userTokens != 0)
{
System.out.println("Would you like to play again? Yes/No");
response = input.nextLine();
}
}
input.close();
}
/** Create an array that contains size random digits, each between low
* and high (inclusive).
*
* @param low The smallest random int that will be generated (inclusive)
* @param high The largest random int that will be generated (inclusive)
* @param size The number of random int values to be generated in the array
* @return A newly constructed array containing size random digits between
* low and high (inclusive)
*/
public static int[] generateRandomDigits(int low, int high, int size)
{
int[] randomNumbers = new int[size];
for(int i =0; i<size; i++){
randomNumbers[i]=findRandom(low, high);
}
return randomNumbers; // this is a stub
}
/** Find a random int uniformly distributed between low and high (inclusive).
* @param low The lower bound for the random number (inclusive)
* @param high The upper bound for the random number (inclusive)
* @return A random int uniformly distributed between low and high (inclusive).
*/
public static int findRandom(int low, int high)
{
Random random = new Random();
return random.nextInt(high) + low; // this is a stub
}
/** Determine how many points the user won. The result of logic are in the
* class preamble.
* @param x The digit on the first wheel.
* @param y The digit on the second wheel.
* @param z The digit on the third wheel.
* @return The score for this round.
*/
// This method uses pass by sharing
public static int calculateWin(int[] digits)
{
/**
* Map to count number of times a number is repeated.
*/
HashMap<Integer, Integer> numberTimes = new HashMap<Integer, Integer>();
for(int digit: digits){
if(!numberTimes.containsKey(new Integer(digit))){
numberTimes.put(new Integer(digit), new Integer(1));
}else{
numberTimes.put(new Integer(digit), numberTimes.get(new Integer(digit))+1);
}
}
// If all the three numbers are 7
if(numberTimes.containsKey(new Integer(7))){
if(numberTimes.get(new Integer(7))==3){
return 750; // If all the three numbers are 7
}else if(numberTimes.get(new Integer(7))==2){
return 20; // If all the three numbers not 7 but same
}
}
Set<Integer> keySet = numberTimes.keySet();
for(Integer key :keySet){
if(key != new Integer(7) && numberTimes.get(key)==3){
return 75; // If two numbers are 7
}
}
keySet = numberTimes.keySet();
for(Integer key :keySet){
if(key != new Integer(7) && numberTimes.get(key)==2){
return 5; // If two numbers are equal but not 7
}
}
return -1; // In case you loss
}
}
Output:
---------
You have 20 tokens.
Your spin was 1 5 0
You earned -1 points.
You now have 19 tokens.
Would you like to play again? Yes/No
yes
You have 19 tokens.
Your spin was 0 1 8
You earned -1 points.
You now have 18 tokens.
Would you like to play again? Yes/No
yes
You have 18 tokens.
Your spin was 7 2 4
You earned -1 points.
You now have 17 tokens.
Would you like to play again? Yes/No
yes
You have 17 tokens.
Your spin was 1 3 3
You earned 5 points.
You now have 22 tokens.
Would you like to play again? Yes/No
yes
You have 22 tokens.
Your spin was 3 4 0
You earned -1 points.
You now have 21 tokens.
Would you like to play again? Yes/No
yes
You have 21 tokens.
Your spin was 2 0 2
You earned 5 points.
You now have 26 tokens.
Would you like to play again? Yes/No
yes
You have 26 tokens.
Your spin was 7 2 1
You earned -1 points.
You now have 25 tokens.
Would you like to play again? Yes/No
yes
You have 25 tokens.
Your spin was 3 3 1
You earned 5 points.
You now have 30 tokens.
Would you like to play again? Yes/No
yes
You have 30 tokens.
Your spin was 8 7 1
You earned -1 points.
You now have 29 tokens.
Would you like to play again? Yes/No
yes
You have 29 tokens.
Your spin was 5 1 4
You earned -1 points.
You now have 28 tokens.
Would you like to play again? Yes/No
yes
You have 28 tokens.
Your spin was 0 7 2
You earned -1 points.
You now have 27 tokens.
Would you like to play again? Yes/No
yes
You have 27 tokens.
Your spin was 3 6 8
You earned -1 points.
You now have 26 tokens.
Would you like to play again? Yes/No
no
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.