I have a problem with my code in java, replit. When I run my code in the console, the output for the csv file is always the random ID number, null, 0,0,0. I don't want null, 0,0,0. null,0,0,0 reflects name, totalgames, totalguesses, and bestgame. When I run my program it doesn't give the actual value of totalgames, totalguesses, and bestgame just 0,0,0 every time. Could you fix this? Also name is null because I haven't asked for the player's name, so I would like you to include that as well in this code.    Here is the code, each class in seperate files in replit:   import java.util.Scanner; import java.util.Random; 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();     } }                   import java.util.Scanner; import java.util.UUID; public class Player {   private UUID id;   private String name;   private int totalGames;   private int totalGuesses;   private int bestGame;   public Player() {       this.id = UUID.randomUUID();   }   public UUID getId() {       return id;   }   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;   }        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(); // consume invalid input             }         }         return guesses;     } }                                   import java.io.*; 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;     private static final String CSV_FILE_PATH = "player_results.csv";     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;         while (true) {             Player player = new Player();             int answer = 1 + random.nextInt(MAX);             int guesses = player.playGame(console, answer);             totalGames++;             totalGuesses += guesses;             bestGame = Math.min(bestGame, guesses);             // Save player information to CSV             savePlayerToCSV(player);             System.out.println("Play again y/n?");             String playAgain = console.next().toLowerCase();             if (!playAgain.startsWith("y")) {                 break;             }         }         printOverallStatistics(totalGames, totalGuesses, bestGame);     }     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);     }     private void savePlayerToCSV(Player player) {         try (FileWriter writer = new FileWriter(CSV_FILE_PATH, true);              BufferedWriter bw = new BufferedWriter(writer);              PrintWriter out = new PrintWriter(bw)) {             out.println(player.getId() + "," +                         player.getName() + "," +                         player.getTotalGames() + "," +                         player.getTotalGuesses() + "," +                         player.getBestGame());         } catch (IOException e) {             e.printStackTrace();         }     }     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();     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

I have a problem with my code in java, replit. When I run my code in the console, the output for the csv file is always the random ID number, null, 0,0,0. I don't want null, 0,0,0. null,0,0,0 reflects name, totalgames, totalguesses, and bestgame. When I run my program it doesn't give the actual value of totalgames, totalguesses, and bestgame just 0,0,0 every time. Could you fix this? Also name is null because I haven't asked for the player's name, so I would like you to include that as well in this code. 

 

Here is the code, each class in seperate files in replit:

 

import java.util.Scanner;
import java.util.Random;

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();
    }
}

 

 

 

 

 

 

 

 

 

import java.util.Scanner;
import java.util.UUID;

public class Player {
  private UUID id;
  private String name;
  private int totalGames;
  private int totalGuesses;
  private int bestGame;

  public Player() {
      this.id = UUID.randomUUID();
  }

  public UUID getId() {
      return id;
  }

  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;
  }
  
    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(); // consume invalid input
            }
        }
        return guesses;
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

import java.io.*;
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;
    private static final String CSV_FILE_PATH = "player_results.csv";

    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;

        while (true) {
            Player player = new Player();
            int answer = 1 + random.nextInt(MAX);
            int guesses = player.playGame(console, answer);
            totalGames++;
            totalGuesses += guesses;
            bestGame = Math.min(bestGame, guesses);

            // Save player information to CSV
            savePlayerToCSV(player);

            System.out.println("Play again y/n?");
            String playAgain = console.next().toLowerCase();
            if (!playAgain.startsWith("y")) {
                break;
            }
        }

        printOverallStatistics(totalGames, totalGuesses, bestGame);
    }

    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);
    }

    private void savePlayerToCSV(Player player) {
        try (FileWriter writer = new FileWriter(CSV_FILE_PATH, true);
             BufferedWriter bw = new BufferedWriter(writer);
             PrintWriter out = new PrintWriter(bw)) {

            out.println(player.getId() + "," +
                        player.getName() + "," +
                        player.getTotalGames() + "," +
                        player.getTotalGuesses() + "," +
                        player.getBestGame());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    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();
    }
}

Search
✓ Files Ⓒ
O.settings
.classpath
.project
04 - Guessing Game.pdf
Game.java
GameManager.java
Packager files
target
pom.xml
Main.java
player_results.csv
Player.java
✓ Tools
All
Recent
ⒸAI
04 - Guessing Game-ShriIyer ✓
Authentication
Chat
Q Code Search
> Console
8 natahace
⠀
Ctrl Shift F
4 Join Replit Core
88
+
Main.java X Player.java x Game.java X player_results.csv X
player_results.csv
1
2 463adfee-0734-4e92-8ed4-a9e55fbb4589,
3 0346a6be-fd1c-4f03-8cd1-be79ab4b1ddc,
4 1e7fa9c2-0166-48c7-970d-319200c96579,
5
X CAI (*) Comma Separated Values
▶ Run
null,0,0,0
null,0,0,0
null,0,0,0
Ln 2, Col 48 • Spaces: 2 History
> Console
V
Run
x
Your guess? 50
It's lower.
Play again y/n?
Yes
Your guess? 40
It's lower.
Your guess? 20
It's lower.
Your guess? 10
It's higher.
Your guess? 15
It's higher.
Your guess? 17
You got it right in 6 guesses!
Your guess? 50
It's lower.
I'm thinking of a number between 1 and 100...
Your guess? 10
It's higher.
Your guess? 20
It's higher.
Your guess? 40
It's lower.
Shell
Your guess? 30
It's higher.
Your guess? 35
It's higher.
+
Total games: 2
Total guesses: 14
Guesses/game: 7
Best game: 6
Your guess? 37
It's higher.
Your guess? 39
You got it right in 8 guesses!
Play again y/n?
No
o
U
Resubmit
A
? SH
36s on 17:20:50, 12/19 ✓
Transcribed Image Text:Search ✓ Files Ⓒ O.settings .classpath .project 04 - Guessing Game.pdf Game.java GameManager.java Packager files target pom.xml Main.java player_results.csv Player.java ✓ Tools All Recent ⒸAI 04 - Guessing Game-ShriIyer ✓ Authentication Chat Q Code Search > Console 8 natahace ⠀ Ctrl Shift F 4 Join Replit Core 88 + Main.java X Player.java x Game.java X player_results.csv X player_results.csv 1 2 463adfee-0734-4e92-8ed4-a9e55fbb4589, 3 0346a6be-fd1c-4f03-8cd1-be79ab4b1ddc, 4 1e7fa9c2-0166-48c7-970d-319200c96579, 5 X CAI (*) Comma Separated Values ▶ Run null,0,0,0 null,0,0,0 null,0,0,0 Ln 2, Col 48 • Spaces: 2 History > Console V Run x Your guess? 50 It's lower. Play again y/n? Yes Your guess? 40 It's lower. Your guess? 20 It's lower. Your guess? 10 It's higher. Your guess? 15 It's higher. Your guess? 17 You got it right in 6 guesses! Your guess? 50 It's lower. I'm thinking of a number between 1 and 100... Your guess? 10 It's higher. Your guess? 20 It's higher. Your guess? 40 It's lower. Shell Your guess? 30 It's higher. Your guess? 35 It's higher. + Total games: 2 Total guesses: 14 Guesses/game: 7 Best game: 6 Your guess? 37 It's higher. Your guess? 39 You got it right in 8 guesses! Play again y/n? No o U Resubmit A ? SH 36s on 17:20:50, 12/19 ✓
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education