Need some help as soon as possible today please. Thanks so much people of Chegg.
ID: 3721996 • Letter: N
Question
Need some help as soon as possible today please. Thanks so much people of Chegg.
In this exercise, you’ll modify the Number Guessing Game so it stores a start time and an end time and calculates the number of seconds that it took the user to guess the number.
Review the application
Open the project named ch15_ex3_GuessingGame in the extra_ex_starts directory.
Open the classes and review their code. Note that they don’t record the start and end time of the game.
Run the application to make sure it works correctly.
Store the start and end time of the game
In the NumberGame class, add instance variables of the LocalDateTime type for the start and end time of the game. Then, add get and set methods for these instance variables.
In the NumberGame class, add a method that returns the number of seconds between the start and end time. To do that, you can use code like the following code to get the number of seconds that have elapsed since January 1, 1970:
long startSeconds = startTime.toInstant(ZoneOffset.UTC).getEpochSecond();
Then, you can use subtraction to determine the number of seconds between the start and end time.
In the Main class, store the start time in the NumberGame object just before the code prompts the user for the first number. Then, store the end time in the NumberGame object just after the code that displays the message that says, “Correct!”.
In the Main class, add code that displays a message that contains the number of seconds just before the code that displays the message that says, “Bye!”.
Run the application to make sure it works correctly. If it takes the user 20 seconds to guess the number, the console should display a message something like this:
Code:
Main.java
package murach.games;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
System.out.println("Welcome to the Number Guessing Game");
System.out.println();
Scanner sc = new Scanner(System.in);
NumberGame game = new NumberGame();
System.out.println("I have selected a number between 0 and " +
game.getUpperLimit());
System.out.println();
System.out.print("Enter your guess: ");
int guess = Integer.parseInt(sc.nextLine());
while (guess != game.getNumber()) {
if (guess < game.getNumber()) {
System.out.println("Your guess is too low. ");
} else if (guess > game.getNumber()) {
System.out.println("Your guess is too high. ");
}
game.incrementGuessCount();
System.out.print("Enter your guess: ");
guess = Integer.parseInt(sc.nextLine());
}
System.out.println("Correct! ");
System.out.println("You guessed the correct number in " +
game.getGuessCount() + " guesses. ");
System.out.println("Bye!");
}
}
NumberGame.Java
package murach.games;
import java.util.Random;
public class NumberGame {
private int upperLimit;
private int number;
private int guessCount;
public NumberGame() {
this(50);
}
public NumberGame(int upperLimit) {
this.upperLimit = upperLimit;
Random random = new Random();
number = random.nextInt(upperLimit - 1) + 1;
guessCount = 1;
}
public int getNumber() {
return number;
}
public int getGuessCount() {
return guessCount;
}
public int getUpperLimit() {
return upperLimit;
}
public void incrementGuessCount() {
guessCount = guessCount + 1;
}
}
Please as soon as possible.
You guessed the correct number in 20 secondsExplanation / Answer
/*********************************NumberGame.java***********************************/
package murach.games;
import java.time.LocalDateTime;
import java.util.Random;
public class NumberGame {
private int upperLimit;
private int number;
private int guessCount;
private LocalDateTime startTime;
private LocalDateTime endTime;
public NumberGame() {
this(50);
}
public NumberGame(int upperLimit) {
this.upperLimit = upperLimit;
Random random = new Random();
number = random.nextInt(upperLimit - 1) + 1;
guessCount = 1;
}
public int getNumber() {
return number;
}
public int getGuessCount() {
return guessCount;
}
public int getUpperLimit() {
return upperLimit;
}
public LocalDateTime getStartTime() {
return startTime;
}
public void setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
}
public LocalDateTime getEndTime() {
return endTime;
}
public void setEndTime(LocalDateTime endTime) {
this.endTime = endTime;
}
public void incrementGuessCount() {
guessCount = guessCount + 1;
}
}
/*************************************Main.java*************************************/
package murach.games;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String args[]) {
System.out.println("Welcome to the Number Guessing Game");
System.out.println();
Scanner sc = new Scanner(System.in);
NumberGame game = new NumberGame();
System.out.println("I have selected a number between 0 and " + game.getUpperLimit());
System.out.println();
Date date = new Date();
game.setStartTime(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
System.out.print("Enter your guess: ");
int guess = Integer.parseInt(sc.nextLine());
while (guess != game.getNumber()) {
if (guess < game.getNumber()) {
System.out.println("Your guess is too low. ");
} else if (guess > game.getNumber()) {
System.out.println("Your guess is too high. ");
}
game.incrementGuessCount();
System.out.print("Enter your guess: ");
guess = Integer.parseInt(sc.nextLine());
}
date = new Date();
game.setEndTime(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
System.out.println("Correct! ");
System.out.println("You guessed the correct number in " + game.getGuessCount() + " guesses. ");
// Output is in nano second converting it to seconds
System.out.println("You guessed the correct number in " + getDifferences(game.getStartTime(), game.getEndTime())
+ " second(s). ");
System.out.println("Bye!");
}
private static long getDifferences(LocalDateTime startTime, LocalDateTime endTime) {
return TimeUnit.MILLISECONDS.toSeconds(getEpochMilli(endTime) - getEpochMilli(startTime));
}
private static long getEpochMilli(LocalDateTime time) {
return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
}
/***********************************************OUTPUT******************************
Welcome to the Number Guessing Game
I have selected a number between 0 and 50
Enter your guess: 1
Your guess is too low.
Enter your guess: 33
Your guess is too high.
Enter your guess: 33
Your guess is too high.
Enter your guess: 22
Your guess is too high.
Enter your guess: 11
Your guess is too low.
Enter your guess: 15
Your guess is too low.
Enter your guess: 17
Your guess is too high.
Enter your guess: 19
Your guess is too high.
Enter your guess: 20
Your guess is too high.
Enter your guess: 23
Your guess is too high.
Enter your guess: 34
Your guess is too high.
Enter your guess: 23
Your guess is too high.
Enter your guess: 22
Your guess is too high.
Enter your guess: 21
Your guess is too high.
Enter your guess: 20
Your guess is too high.
Enter your guess: 19
Your guess is too high.
Enter your guess: 28
Your guess is too high.
Enter your guess: 18
Your guess is too high.
Enter your guess: 17
Your guess is too high.
Enter your guess: 16
Correct!
You guessed the correct number in 20 guesses.
You guessed the correct number in 35 second(s).
Bye!
************************************************************************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.