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

Java Before you start this program, you should be comfortable with the decision

ID: 3592655 • Letter: J

Question

Java

Before you start this program, you should be comfortable with the decision statement and loops.

Program Specification:

Vera takes a jar which contains exactly 100 quarters to the casino with the intention to win big. She plays four 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 machine pays 7 quarters every 9th time it is played;

the fourth machine pays 1 quarter every 3rd time it is played.

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, and the fourth 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. The methods can be placed below main or in a separate class.


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:

The Number of Machine (1, 2, 3 or 4) that she just won on

The Amount she just won (formatted like dollars and cents)

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.

IN THIS ASSIGNMENT, your output will be saved inside of a text file. The text file should be saved in the src folder. You may need to research how to save output to a text file.

Deliverables:

A Good Design Tool

Well Documented Source Code

You CANNOT DO:

You cannot create an instance of a class that you have created.

You cannot create a class with instance variables (not yet)

Explanation / Answer

Solution:

code:

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;

}

}

Please, please upvote and ask your doubts in the comments.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote