Need help with a Java assignment if possible. At the time we got this assignment
ID: 3589413 • Letter: N
Question
Need help with a Java assignment if possible. At the time we got this assignment we had not started learning about OOP yet, so we are supposed to write the program without creating classes with instance variables:
Before you start this program, you should be comfortable with the decision statement and loops.
Program Specification:
Ver 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
Given below is the program for the question. It will prompt for the no. of quarters and also the output filename. You will need to refresh the project to see the output file listed there after running the program. Hope the answer helped. If it did, please don't forget to rate it. Thank you.
To indent code in eclipse, select code by pressing Ctrl+A and then press Ctrl+i
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class CasinoPlay {
private static int getMoneyInput(Scanner keybd)
{
System.out.print("Enter the number of quarters in Vera's jar: ");
int quarters = keybd.nextInt();
return quarters;
}
private static String getOutputFilename(Scanner keybd)
{
String filename;
System.out.print("Enter output filename: ");
filename = keybd.next();
return filename;
}
//returns the pay made by a machine depending on how many times the game has been played on it
private static int playGame(int machineNo, int m1Games, int m2Games, int m3Games, int m4Games)
{
if(machineNo == 1 && m1Games % 33 == 0)
return 25;
else if(machineNo == 2 && m2Games % 99 == 0)
return 75;
else if(machineNo == 3 && m3Games % 9 == 0)
return 7;
else if(machineNo == 4 && m4Games % 3 == 0)
return 1;
else
return 0;
}
private static double convert(int quarters)
{
double totalCents = quarters * 25 ;
double dollars = totalCents / 100;
return dollars;
}
private static void writeToFile(PrintWriter outfile, int machine, int pay, int qtrsLeft)
{
outfile.println("Machine: " + machine);
outfile.println("Winning amount: $" + convert(pay));
outfile.println("No. of quarters left: " + qtrsLeft + "( $" + convert(qtrsLeft) + " ) " );
}
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
int noQtrs = getMoneyInput(keybd);
String filename = getOutputFilename(keybd);
PrintWriter outFile = null;
try {
//create an object for output file
outFile = new PrintWriter(new File(filename));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
return;
}
//variables to represent how may games have been played on each of the machines
int m1Games = 0, m2Games = 0, m3Games = 0, m4Games = 0;
int game = 0;
int machine = 1;
while(noQtrs > 0 )
{
game++;
noQtrs --; //reduce a quarter from jar to play the game
if(machine == 1)
m1Games++;
else if(machine == 2)
m2Games++;
else if(machine == 3)
m3Games++;
else
m4Games++;
//System.out.println(machine +" " + m1Games + " " + m2Games + " " + m3Games + " " + m4Games);
int pay = playGame(machine, m1Games, m2Games, m3Games, m4Games);
if(pay != 0)
{
noQtrs += pay;
writeToFile(outFile, machine, pay, noQtrs);
}
machine++;
if(machine == 5) //come back in sequence to 1 if machine is 5
machine = 1;
}
outFile.println("Total no. of games played : " + game);
outFile.close();
System.out.println("Output written to file " + filename);
}
}
output
Enter the number of quarters in Vera's jar: 100
Enter output filename: casino.txt
Output written to file casino.txt
output file: casino.txt
Machine: 4
Winning amount: $0.25
No. of quarters left: 89( $22.25 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 78( $19.5 )
Machine: 3
Winning amount: $1.75
No. of quarters left: 74( $18.5 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 74( $18.5 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 63( $15.75 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 52( $13.0 )
Machine: 3
Winning amount: $1.75
No. of quarters left: 48( $12.0 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 48( $12.0 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 37( $9.25 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 26( $6.5 )
Machine: 3
Winning amount: $1.75
No. of quarters left: 22( $5.5 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 22( $5.5 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 11( $2.75 )
Machine: 1
Winning amount: $6.25
No. of quarters left: 27( $6.75 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 25( $6.25 )
Machine: 3
Winning amount: $1.75
No. of quarters left: 21( $5.25 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 21( $5.25 )
Machine: 4
Winning amount: $0.25
No. of quarters left: 10( $2.5 )
Total no. of games played : 166
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.