I\'m in java one and need help with this code. please follow the directions give
ID: 3684294 • Letter: I
Question
I'm in java one and need help with this code. please follow the directions given and test your program to make sure it works as it is required. I will apreciate your help. thank you.please use the given code.
Programming Concepts
1. Random Numbers
2. Methods
3. Passing Arrays
Assignment Description
-- Write a program that will emulate a slot machine. The machine will continue to run until the user wishes to stop or runs out of money. The machine generates 3 random characters.
-- Every time the user wants to play again, subtract $1 from their total
--You will copy a template of the program from this directory into yours by issuing the following command:
cp ~lkoperski/1404_Lab/Assignments/SlotMachine.java .
here is the template. please use it as given and fill the missing information to make the program work.
Required Methods
• public static void genRandSlots ( char[] slots )
o I have created a char array for you, called symbols, with an initializer list of the heart, spade,club, and diamond symbols
o For each element of the slots array, you need to randomly pick one of the symbols and store it into the slots array
public static int getWinnings( char[] slots )
o Based on the number of matching characters in the slots array, calculate the amount of money that the user wins, and return it.
o If three characters match >? The user wins $10
o If only two characters match > ? The user wins $5
o If none of the characters match > ? The user wins $0
Methods Already Written For You
public static boolean playAgain () o Asks the user if they want to play again
o Returns true if they say do
o Returns false if they say don't
public static void printSlots () o Prints out the slots
Output Example 1 – Getting two matching symbols
user@loki:~$ java SlotMachine
You have $3
???????
You won $0
Would you like to pay $1 to play again? ( Y / N ): y
====================================================
You have $2
???????
You won $5
Would you like to pay $1 to play again? ( Y / N ): y
====================================================
You have $6
???????
You won $0
Would you like to pay $1 to play again? ( Y / N ): n
====================================================
user@loki:~$
Output Example 2 – Getting three matching symbols
user@loki:~$ java SlotMachine
You have $3
???????
You won $10
Would you like to pay $1 to play again? ( Y / N ): y
====================================================
You have $12
???????
You won $0
Would you like to pay $1 to play again? ( Y / N ): n
====================================================
user@loki:~$
Output Example 3 – Running out of money
user@loki:~$ java SlotMachine
You have $3
???????
You won $0
Would you like to pay $1 to play again? ( Y / N ): y
====================================================
You have $2
???????
You won $0
Would you like to pay $1 to play again? ( Y / N ): y
====================================================
You have $1
???????
You won $0
Would you like to pay $1 to play again? ( Y / N ): y
====================================================
You failed at gambling. Call the helpline at 1-800-IAMBAD
user@loki:~$
Explanation / Answer
package com.he.capillary.chegg1;
import java.util.Random;
import java.util.Scanner;
public class SlotMachine {
public static void main(String[] args) {
char[] slots = new char[3];
int total = 3;
while (true) {
if (total < 3) {
System.out.println("No Money");
break;
} else {
System.out.println(" You have $" + total);
getRandSlots(slots);
printSlots(slots);
getWinnings(slots);
System.out.println(" You won $" + getWinnings(slots));
total += getWinnings(slots);
if (playAgain()) {
total--;
continue;
} else {
break;
}
}
}
}
public static void getRandSlots(char[] slots) {
Random r = new Random();
char[] symbols = { 'u2665', 'u2660', 'u2663', 'u2666' };
for (int i = 0; i < slots.length; i++) {
int dice = new Random().nextInt((4 - 1) + 1) + 1;
slots[i] = symbols[dice % symbols.length];
}
}
public static int getWinnings(char[] slots) {
if (slots[0] == slots[1] && slots[1] == slots[2]) {
return 10;
} else if (slots[0] == slots[1] || slots[1] == slots[2] || slots[0] == slots[2]) {
return 5;
}
return 0;
}
public static boolean playAgain() {
Scanner sc = new Scanner(System.in);
System.out.println(" Would you like to pay $1 to play again?");
char choice = sc.next().charAt(0);
if (choice == 'y') {
return true;
} else {
return false;
}
}
public static void printSlots(char[] slots) {
char a = 'u250c';
char b = 'u2510';
char c = 'u2514';
char d = 'u2518';
char e = 'u252c';
char f = 'u2534';
char g = 'u2500';
char h = 'u2502';
String i = g + "" + "" + g + "" + g;
System.out.printf(" %c%s%c%s%c%s%c %c%c%c%c%c%c%c %c%s%c%s%c%s%c ", a, i, e, i, e, i, b,
h,slots[0], h, slots[1], h, slots[2],h
, c, i, f, i, f, i, d);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.