Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

There is a similar question on Chegg but the answer is not complete please please do not copy and paste that solution. Exercise 15-3 Add

There is a similar question on Chegg but the answer is not complete please please do not copy and paste that solution.

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.

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. Also display in the final message the date the game was played. Use the LONG format style.

9. 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:

OUTPUT: You guessed the correct number in 20 seconds on January 1st 2015.

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

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions