Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Open the classes and review their code. Note that they dont record the start and end time of the game. 2. Run the application

1. Open the classes and review their code. Note that they dont record the start and end time of the game.

2. Run the application to make sure it works correctly.

Store the start and end time of the game

3. 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.

4. 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.

5. 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!.

6. 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!.

7. Also display in the final message the date the game was played. Use the LONG format style.

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.

//------------------main.java

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

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

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

1. Is this a between- or within-participants design?

Answered: 1 week ago