Question: Exercise 15-3 Add a start time and end time to the Number Guessing Game In this exercise, youll modify the Number Guessing Game so it
Exercise 15-3 Add a start time and end time to the Number Guessing Game
In this exercise, youll 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
1. Open the project named ch15_ex3_GuessingGame in the extra_ex_starts directory.
2. Open the classes and review their code. Note that they dont record the start and end time of the game.
3. Run the application to make sure it works correctly.
Store the start and end time of the game
4. 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.
5. 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.
6. 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!.
7. 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!.
8. 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:
You guessed the correct number in 20 seconds.
THE FOLLOWING CODE HAS ALREADY BEEN GIVEN
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!"); } }
AND
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; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
