Aunt Vera in Vegas - Java Program Program Specification: Vera takes a jar which
ID: 3870390 • Letter: A
Question
Aunt Vera in Vegas - Java Program Program Specification: Vera takes a jar which contains exactly 100 quarters to the casino with the intention to win big. She plays three machines slot machines in sequence. Unknown to her, the machines are entirely predictable. Each play costs one quarter. The following are constants: The first machine pays 25 quarters every 33th time it is played; the second machine pays 75 quarters every 99th time it is played; the third pays 7 quarters every 9th time it is played. So she will always lose - eventually. Notice that she plays the machines one at a time in sequence: the first one, then the second one, the third one, the first one, the second one… Your program will output the number of times Vera plays until she goes broke along with the result of each time she wins money. Besides “main” you must define and use at least 4 methods. Your program will input the number of quarters in Vera’s jar (it could be any number but for this assignment make it 100). Your program will output a statement EVERYTIME Vera wins money. The output will contain the following: 1. The Number of Machine (1, 2, or 3) that she just won on 2. The Amount she just won (formatted like dollars and cents) 3. The current number of quarters remaining in the jar immediately after the win and the amount of money in the jar (i.e. there are 47 quarters in the jar with equals $11.75. Once Vera is broke you will output put the total number of times she was able to play the machine. Your output will be saved inside of a text file. The text file should be saved in the src folder.
Explanation / Answer
package com.kanth.chegg;
import java.io.PrintWriter;
import java.util.Scanner;
public class JarsCasinoProblem {
public static void main(String[] args) {
JarsCasinoProblem obj = new JarsCasinoProblem();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of jars");
int jars = 100;
try {
jars = sc.nextInt();
} catch (Exception e) {
System.out.println("Please enter Integers ");
}
try {
obj.gamePlay(obj, jars);
System.out.println("file generated successfully");
if (sc != null) {
sc.close();
}
} catch (Exception e) {
System.out.println("Error in calcauting the game play");
if (sc != null) {
sc.close();
}
}
}
private void gamePlay(JarsCasinoProblem obj, int jars) throws Exception {
PrintWriter writer = new PrintWriter("src/com/kanth/chegg/result.txt", "UTF-8");
boolean first = true;
boolean two = false;
boolean three = false;
int totalPlay = 0;
int firstPlay = 1;
int secondPlay = 1;
int thirdPlay = 1;
for (int i = 1; i <= jars; i++) {
totalPlay++;
if (first) {
if (firstPlay % 33 == 0) {
jars = jars + 25;
printMachine1Data(obj, jars, writer, i);
}
first = false;
three = false;
two = true;
firstPlay++;
} else if (two) {
if (secondPlay % 99 == 0) {
jars = jars + 75;
printMachine2Data(obj, jars, writer, i);
}
two = false;
first = false;
three = true;
secondPlay++;
} else if (three) {
if (thirdPlay % 9 == 0) {
jars = jars + 7;
printMachine3Data(obj, jars, writer, i);
}
three = false;
two = false;
first = true;
thirdPlay++;
}
}
writer.println("");
writer.println("Total no of times she play's ==" + totalPlay);
writer.close();
}
private void printMachine3Data(JarsCasinoProblem obj, int jars, PrintWriter writer, int i) {
writer.println("Number of the machine won = 3");
writer.println("Amount won on this machine 3 is :" + obj.getAmount(7));
writer.println("Number of quarters remaining in the jar :" + (jars - i));
writer.println("Amount of money in the jars :" + obj.getAmount(jars));
writer.println("");
}
private void printMachine2Data(JarsCasinoProblem obj, int jars, PrintWriter writer, int i) {
writer.println("Number of the machine won = 2");
writer.println("Amount won on this machine 2 is :" + obj.getAmount(75));
writer.println("Number of quarters remaining in the jar :" + (jars - i));
writer.println("Amount of money in the jars :" + obj.getAmount(jars));
writer.println("");
}
private void printMachine1Data(JarsCasinoProblem obj, int jars, PrintWriter writer, int i) {
writer.println("Number of the machine won = 1");
writer.println("Amount won on this machine 1 is :" + obj.getAmount(25));
writer.println("Number of quarters remaining in the jar :" + (jars - i));
writer.println("Amount of money in the jars :" + obj.getAmount(jars));
writer.println("");
}
/**
* 11.74 = 47 quarters ,means 1 quarter=$0.25
*
* @param jars
* @return
*/
public double getAmount(int jars) {
return jars * 0.25;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.