Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Start Eclipse and import the project named MidTerm_GuessingGame. Review the exis

ID: 3603408 • Letter: S

Question

Start Eclipse and import the project named MidTerm_GuessingGame.

Review the existing code. Note the use of the Random class. This code generates a random integer between 0 and upperLimit. For the purposes of this exercise, you don’t need to understand how it works.

Write the code that prompts the user for an upper limit for the guess. This code should set the upperLimit variable which is already defined.

Write the code that prompts the user for their first guess.

Write the while loop that allows the user to guess again if their guess was wrong. This loop should inform the user whether their guess was too high or too low. Then, it should prompt the user for a new guess. The loop should exit when the user’s guess is equal to the random number the computer generated.

After the while loop, write code that tells the user they guessed correctly.

Run the application and make sure it works correctly.

Before the while loop, add a variable to keep track of the number of guesses the user has made. Remember to initialize this variable to 1 instead of 0 since the user will need at least one guess to get the correct number.

Inside the while loop, increment the variable by one each time the user guesses incorrectly.

After the while loop, add a message that tells the user how many guesses they took.

Modify the application so it uses some object-oriented features by creating a new class named NumberGame.

In this class, create one instance variable for storing the upper limit of the number, a second for storing the number, and a third for the number of guesses the user has made.

Add a constructor to the class that takes an integer value for the upper limit and uses it to set the upper limit instance variable. In this constructor generate the number that the user should try to guess and set that instance variable. Also initialize the instance variable for the number of guesses to 1.

Add get and set methods for all three instance variables.

Add a method named incrementGuessCount that adds 1 to the instance variable for the number of guesses.

In the Main class modify the code so it uses the new object.

Remove any unnecessary code from the main method that is now being accomplished by the new object.

Run the project again and makes sure it still works correctly.

In the NumberGame class, add a constructor to the class that takes no arguments. The code for this constructor can call the other constructor in this class and pass it a value of 50 for the upper limit.

In the Main class, modify the code so it uses the zero-argument constructor. Then, comment out the statements that get the upper limit from the user. These statements are no longer necessary since the constructor automatically sets the upper limit to 50.

Run the project again and makes sure it works correctly. It should now set an upper limit of 50 by default.

Use Eclipse to create a new package named murach.games.guessing. Then, move the Main class into it. Review chapter 5 if necessary.

Use Eclipse to create a new package named murach.games.guessing.business. Then, move the NumberGame class into it.

Review the code in the NumberGame class and note that Eclipse has automatically updated the package statement for you.

Review the code for the Main class and note that Eclipse has automatically updated the package statement. Also, note that Eclipse has automatically added an import statement for the NumberGame class.

Run the project to make sure it works correctly.

Modify the loop in the Main class so that it prompts the user for a number. (Comment out unnecessary code after the modification, if there is any.)

Modify the loop so that it’s an infinite while loop. (Comment out unnecessary code after the modification, if there is any.)

Modify the if/else statement so it uses a break statement to jump out of the loop if the user guesses the correct number. (Comment out unnecessary code after the modification, if there is any.)

Run the application to make sure it works correctly.

Within the loop, modify the if/else statement so the application says, "Way too high!" if the user’s guess is more than 10 higher than the random number. Otherwise, the application should just say, “Your guess is too high.”

After the loop, add an if/else statement that displays a message that depends on the user’s number of guesses based on the following:

Number of guesses      Message

=================     =======

<=3                                   Great work! You are a mathematical wizard.

>3 and <=7                       Not too bad! You've got some potential.

>7                                     What took you so long? Maybe you should take some lessons

Run the application to make sure it works correctly.

In the Main class, add a try/catch statement so it catches the NumberFormatException that’s thrown by the parseInt method. (Review chapter 8 if necessary.) If this exception is thrown, display a message to the console that says, “Invalid number” and jump to the top of the loop. This should prompt the user to enter the number again.

Run the application to make sure it works correctly.

Add an if/else statement to make sure the user enters a value between the minimum and maximum values. If the user enters a value that’s less than or equal to 0, display a user-friendly error message and jump to the top of the loop. Conversely, if the user enters a value that’s greater than or equal to the game’s upper limit, display a user-friendly error message and jump to the top of the loop.

Run the application to make sure it works correctly.

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:                                                 You guessed the correct number in 20 seconds.

Add javadoc comments to all the classes in the project. Make sure these comments include @param and @return tags every where these tags are appropriate.

This documentation must include descriptions for all of the constructors and methods as well as information about all parameters and return values.

Generate the documentation for the entire project.

View the documentation.

CODE:

Main.java

package murach.games;

import java.util.Random;
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();
  
// TODO: Replace next line with code that prompts user for upper limit.
int upperLimit = 10;
System.out.print("Enter the upper limit for the number :");
upperLimit = sc.nextInt();

// Generate a random number between 0 and the upperLimit variable
Random random = new Random();
int number = random.nextInt(upperLimit - 1) + 1;
  
System.out.println("number: " + number); // remove this from final app
}
}

Explanation / Answer

NumberGame.java

package murach.games.guessing.business;

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 setUpperLimit(int upperLimit) {

this.upperLimit = upperLimit;

}

public void setNumber(int number) {

this.number = number;

}

public void setGuessCount(int guessCount) {

this.guessCount = guessCount;

}

public void incrementGuessCount() {

guessCount = guessCount + 1;

}

/*

public LocalDateTime getStart() {

  

return start;

}

public void setStart(LocalDateTime start) {

this.start = start;

}

public LocalDateTime getEnd() {

return end;

}

public void setEnd(LocalDateTime end) {

this.end = end;

}

public long calculateTime()

{

return end-start;

}

}

______________

Main.java

package murach.games.guessing;

import java.util.Scanner;

import murach.games.guessing.business.NumberGame;

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

  

int noOfGuesses=1;

// long startSeconds = startTime.toInstant(ZoneOffset.UTC).getEpochSecond();

  

System.out.print("Enter your guess: ");

int guess = Integer.parseInt(sc.nextLine());

while (guess != game.getNumber()) {

if (guess < game.getNumber()) {

noOfGuesses++;

System.out.println("Your guess is too low. ");

} else if (guess > game.getNumber()) {

noOfGuesses++;

System.out.println("Your guess is too high. ");

}

game.incrementGuessCount();

System.out.print("Enter your guess: ");

guess = Integer.parseInt(sc.nextLine());

}

//long endSeconds = startTime.toInstant(ZoneOffset.UTC).getEpochSecond();

  

System.out.println("Correct! ");

System.out.println("You guessed the correct number in "

+ game.getGuessCount() + " guesses. ");

System.out.println("Bye!");

}

}

________________

Output:

Welcome to the Number Guessing Game

I have selected a number between 0 and 50

Enter your guess: 25
Your guess is too low.

Enter your guess: 40
Your guess is too low.

Enter your guess: 45
Your guess is too high.

Enter your guess: 43
Your guess is too high.

Enter your guess: 42
Correct!

You guessed the correct number in 5 guesses.

Bye!

________________Thank YOu

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote