In this homework, you will be writing a program, which repeatedly asks the user
ID: 3859249 • Letter: I
Question
In this homework, you will be writing a program, which repeatedly asks the user if the next roll will be higher or lower than the last dice roll. You will continually play until the user types quit on a line by itself. The game will remember how many times you were successful and how many times you tried and report the ratio of success to tries after the user quits. In addition, the game will maintain persistence: remembering you and how many times you've played even after exiting (by maintaining a file). You will need to ask the user for their user name prior to playing and use that to record their results in a file. If the user has played before, the number of successful tries and the number of tries is in a file named after the user with a .txt extension. If not, we start at zero for that user. Once the user quits, you'll need to save the new results into the correct file. (10%) Your dialog should be clear each time you ask for something what you are asking for and output the result. (20%) Logic reading/determining the hi/lo/quit 3. (20%) Logic providing the dice rolls (30%) Logic providing the persistence which includes: Determine if the file exists Opening the file, reading the number correct and the number of tries (each on their own line) Once finished, rewriting the file so that it contains current data (20%) The logic providing the rest of the result (managing the play, keeping score, calculations) Name your program: DiceHILO.javaExplanation / Answer
Hi buddy, please find the below java program. You may want to change the path in the constructor File file = new File(YOURPATH) in the given program
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class DiceHILO.{
//Object for generating random number
private static final Random rand = new Random();
//Method to calculate GCD for printing ratio
private static int gcd(int a, int b){
if(b==0)return a;
return gcd(b,a%b);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Prompting and reading username
System.out.println("Please enter your name");
String name = br.readLine().trim();
String choice;
int prev = rand.nextInt(6)+1;
System.out.println("Present dice rolled "+prev);
System.out.println("Please enter your choice");
int success = 0;
int failure = 0;
//This loop continues until user enters quit.
while(!(choice = br.readLine()).equals("quit")){
int present = rand.nextInt(6)+1;
System.out.println("Dice rolled "+present);
if(choice.equals("hi")){
if(present>prev){
success++;
}
else{
failure++;
}
}
else{
if(present<prev){
success++;
}
else{
failure++;
}
}
prev = present;
}
int g = gcd(success,failure);
//Printing number of successes, falures and ratio for this game
System.out.println("Success : "+success+" Failure : "+failure+" Ratio : "+(success/g)+":"+(failure/g));
//File for writing the results
File file = new File("D:/"+name+".txt");
int preSuccess = 0;
int preFail = 0;
//Checking for existence of the file. If this file exists, fetch number of successes and failures so far
if(file.exists()){
BufferedReader fr = new BufferedReader(new FileReader(file));
preSuccess = Integer.parseInt(fr.readLine());
preFail = Integer.parseInt(fr.readLine());
fr.close();
}
//Increment number of successes and failures
preSuccess += success;
preFail += failure;
//Read the results to the file
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(preSuccess+" ");
bw.write(preFail+"");
bw.flush();
bw.close();
}
}
OUTPUT :
Please enter your name
alex
Present dice rolled 5
Please enter your choice
lo
Dice rolled 2
hi
Dice rolled 6
hi
Dice rolled 6
hi
Dice rolled 3
hi
Dice rolled 1
lo
Dice rolled 1
quit
Success : 2 Failure : 4 Ratio : 1:2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.