Related questions
I have the following code in java, replit:
//First class
public class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Random random = new Random();
Game game = new Game(console, random);
game.startGame();
console.close();
}
}
//Second class
import java.util.Scanner;
public class Player {
private String name;
private int totalGames;
private int totalGuesses;
private int bestGame;
public Player() {
this.name = null;
this.totalGames = 0;
this.totalGuesses = 0;
this.bestGame = Integer.MAX_VALUE;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTotalGames() {
return totalGames;
}
public void setTotalGames(int totalGames) {
this.totalGames = totalGames;
}
public int getTotalGuesses() {
return totalGuesses;
}
public void setTotalGuesses(int totalGuesses) {
this.totalGuesses = totalGuesses;
}
public int getBestGame() {
return bestGame;
}
public void setBestGame(int bestGame) {
this.bestGame = bestGame;
}
}
//Third class
import java.util.Scanner;
import java.util.Random;
public class Game {
public static final int MAX = 100;
private final Scanner console;
private final Random random;
public Game(Scanner console, Random random) {
this.console = console;
this.random = random;
}
public void startGame() {
int totalGames = 0;
int totalGuesses = 0;
int bestGame = Integer.MAX_VALUE;
Player player = new Player();
while (true) {
int answer = 1 + random.nextInt(MAX);
int guesses = playGame(console, answer);
totalGames++;
totalGuesses += guesses;
bestGame = Math.min(bestGame, guesses);
player.setTotalGames(totalGames);
player.setTotalGuesses(totalGuesses);
player.setBestGame(bestGame);
System.out.println("Play again y/n?");
String playAgain = console.next().toLowerCase();
if (!playAgain.startsWith("y")) {
break;
}
}
printOverallStatistics(player.getTotalGames(), player.getTotalGuesses(), player.getBestGame());
}
public int playGame(Scanner console, int answer) {
System.out.println("I'm thinking of a number between 1 and " + Game.MAX + "...");
int guesses = 0;
while (true) {
System.out.print("Your guess? ");
if (console.hasNextInt()) {
int guess = console.nextInt();
guesses++;
if (guess == answer) {
System.out.println("You got it right in " + guesses + " guess" + (guesses == 1 ? "!" : "es!"));
break;
} else if (guess < answer) {
System.out.println("It's higher.");
} else {
System.out.println("It's lower.");
}
} else {
System.out.println("Invalid input. Please enter a valid integer.");
console.next();
}
}
return guesses;
}
private void printOverallStatistics(int totalGames, int totalGuesses, int bestGame) {
System.out.println("Total games: " + totalGames);
System.out.println("Total guesses: " + totalGuesses);
System.out.println("Guesses/game: " + (totalGames == 0 ? 0 : totalGuesses / totalGames));
System.out.println("Best game: " + bestGame);
}
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Random random = new Random();
Game game = new Game(console, random);
game.startGame();
console.close();
}
}
Now I've created a player_results.csv file. I want help with adding code to my existing class so that it reads from the csv file(which can be updated by just editing it, example in attachment below). Then, convert those strings into a player object you can use to resume a series of games in the console. In other words, update the players statistics as though they were resuming from the state they last left the guessing game. It should work for all players you add in the file. You must use these:
- java.io.File
- java.io.FileWriter
- java.util.Scanner
- java.util.Random
- java.util.UUID
Must use FileWriter not bufferedfilewriter or anything else, and Arrays.
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps
- T/F 1. Java methods can only return primitive typesarrow_forwardSee ERROR in Java file 2: Java file 1: import java.util.Random;import java.util.Scanner; public class Account {// Declare the variablesprivate String customerName;private String accountNumber;private double balance;private int type;private static int numObjects;// constructorpublic Account(String customerName, double balance, int type) { this.customerName = customerName;this.balance = balance;this.type = type;setaccountNumber(); // set account number functionnumObjects += 1;}private void setaccountNumber() // definition of set account number{Random rand = new Random();accountNumber = customerName.substring(0, 3).toUpperCase();accountNumber += type;accountNumber += "#";accountNumber += (rand.nextInt(100) + 100);}// function to makedepositvoid makeDeposit(double amount){if (amount > 0){balance += amount;}}// function for withdrawboolean makeWithdrawal(double amount) {if (amount < balance)balance -= amount;elsereturn false;return true;}public String toString(){return accountNumber +...arrow_forwardJAVA programming languagearrow_forward
- Question 2 Analyze the following code. // Program 1 public class Test{ public static void main(String[] args) { Object al = new A0); Object a 2 = new A0); System.out.println(((A)a 1).equals((A)a 2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } // Program 2 public class Test{ public static void main(String[] args) { A a1 = new A(); A a2 = new A0); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } Program 1 displays true and Program 2 displays true Program 1 displays false and Program 2 displays true O Program 1 displays true and Program 2 displays false Program 1 displays false and Program 2 displays falsearrow_forward1 public class Main { 1234567 7 8 9 10 11 ENERGET 12 13 14 15 16 17 18} public static void main(String[] args) { String present = ""; double r = Math.random(); // between 0 and 1 if (r>>>arrow_forward1. Insert the missing part of the code below to output the string. public class MyClass { public static void main(String[] args) { ("Hello, I am a Computer Engineer");arrow_forward
- Fix all errors to make the code compile and complete. //MainValidatorA3 public class MainA3 { public static void main(String[] args) { System.out.println("Welcome to the Validation Tester application"); // Int Test System.out.println("Int Test"); ValidatorNumeric intValidator = new ValidatorNumeric("Enter an integer between -100 and 100: ", -100, 100); int num = intValidator.getIntWithinRange(); System.out.println("You entered: " + num + "\n"); // Double Test System.out.println("Double Test"); ValidatorNumeric doubleValidator = new ValidatorNumeric("Enter a double value: "); double dbl = doubleValidator.getDoubleWithinRange(); System.out.println("You entered: " + dbl + "\n"); // Required String Test System.out.println("Required String Test:"); ValidatorString stringValidator = new ValidatorString("Enter a required string: "); String requiredString =...arrow_forwardHow do I fix the errors? Code: import java.util.Scanner;public class ReceiptMaker { public static final String SENTINEL = "checkout";public final int MAX_NUM_ITEMS;public final double TAX_RATE;private String[] itemNames;private double[] itemPrices;private int numItemsPurchased; public ReceiptMaker() {MAX_NUM_ITEMS = 10;TAX_RATE = 0.0875;itemNames = new String[MAX_NUM_ITEMS];itemPrices = new double[MAX_NUM_ITEMS];numItemsPurchased = 0;}public ReceiptMaker(int maxNumItems, double taxRate) {MAX_NUM_ITEMS = maxNumItems;TAX_RATE = taxRate;itemNames = new String[MAX_NUM_ITEMS];itemPrices = new double[MAX_NUM_ITEMS];numItemsPurchased = 0;} public void greetUser() {System.out.println("Welcome to the " + MAX_NUM_ITEMS + " items or less checkout line");}public void promptUserForProductEntry() {System.out.println("Enter item #" + (numItemsPurchased + 1) + "'s name and price separated by a space, or enter \"checkout\" to end transaction early");}public void addNextPurchaseItemFromUser(String...arrow_forwardcomplete the missing code. public class Exercise09_04Extra { public static void main(String[] args) { SimpleTime time = new SimpleTime(); time.hour = 2; time.minute = 3; time.second = 4; System.out.println("Hour: " + time.hour + " Minute: " + time.minute + " Second: " + time.second); } } class SimpleTime { // Complete the code to declare the data fields hour, // minute, and second of the int type }arrow_forward
- public class LabProgram { public static void main(String args[]) { Course course = new Course(); String first; // first name String last; // last name double gpa; // grade point average first = "Henry"; last = "Cabot"; gpa = 3.5; course.addStudent(new Student(first, last, gpa)); // Add 1st student first = "Brenda"; last = "Stern"; gpa = 2.0; course.addStudent(new Student(first, last, gpa)); // Add 2nd student first = "Jane"; last = "Flynn"; gpa = 3.9; course.addStudent(new Student(first, last, gpa)); // Add 3rd student first = "Lynda"; last = "Robison"; gpa = 3.2; course.addStudent(new Student(first, last, gpa)); // Add 4th student course.printRoster(); } } // Class representing a student public class Student { private String first; // first name private String last; // last name private double gpa; // grade point average...arrow_forwardpublic class Course{String name;String dept;int number;public Course (String n, String d, int n){name = n;dept = d;number = n;}public String toString(){return "Course " + name + " in " + dept + " #: " + number;}public static void main (String [] args){Scanner in = new Scanner(System.in);add 10 lines using the scanner and saving them as strings The input for each line will be entered a String, a space, another String, and an int. Parse the input string into those three items and make an instance of Course. Add the new Course instance to a generic ArrayList of Course. print out each Course in the ArrayList.arrow_forwardpublic class Test { } public static void main(String[] args){ int a = 10; System.out.println(a*a--); }arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education