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

JAVA In this exercise, you’ll improve the Number Guessing Game. Modify the loop

ID: 3726711 • Letter: J

Question

JAVA

In this exercise, you’ll improve the Number Guessing Game.

Modify the loop statement

1. Open the project named ch08_ex3_GuessingGame in the extra_ex_starts directory.

2. Open the Main class. Modify the loop that prompts the user for a number so that it’s an

infinite while loop. When you do that, you only need to code the two statements that

get input from the user at the top of the loop.

3. Modify the if/else statement so it uses a break statement to jump out of the

loop if the user guesses the correct number.

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

Add some if/else statements

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

6. After the loop, add an if/else statement that displays a message that depends on the

user’s number of guesses. For example:

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

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

Add a try/catch statement to get a valid integer

8. In the Main class, add a try/catch statement so it catches the NumberFormatException

that’s thrown by the parseInt method. 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.

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

Add an if/else statement to make sure the integer is within a range

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

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

Here is the source code that needs to be modified:

& Main.java x S NumberGame.java x Source History G - - Q 5 2 1 2 2 2 package murach.games; 3D import java.util.Scanner; public class Main { 7 RI public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner (System.in); Number Game game = new Number Game (0: System.out.println("I have selected a number between 0 and " + game.getUpperLimit(0); System.out.println(); " " no 0 0 2 2 2 2 2 2 2 8 N N & N G G - H H M M M M System.out.print("Enter your guess: "); int guess = Integer.parseInt(sc.nextLine()); while (guess != game.getNumber (0) { if (guess game.getNumber (0) { 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!");

Explanation / Answer

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// TODO Auto-generated method stub

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.println("Enter your guess ");

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

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

while (true) { // infinite loop added

System.out.println("Enter your guess ");

try {

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

if (guess <= 0) {

System.out.println(" Number should be greater than 0 ");

game.incrementGuessCount();

continue; // it will transfer the control to the top of the

// loop again

} else if (guess >= game.getUpperLimit()) {

System.out.println(" Number should be less than " + game.getUpperLimit());

game.incrementGuessCount();

continue;

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

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

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

if ((guess - game.getNumber()) > 10) {

System.out.println("Way too high!");

} else

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

} else if (guess == game.getNumber()) { // added to get out of

// loop if matches

break; // break to jump out of loop if guess matches the

// random number

}

} catch (NumberFormatException numberFormatException) {

System.out.println(" Please enter a valid number ");

}

game.incrementGuessCount();

}

int guessCount = game.getGuessCount();

System.out.println("Correct ");

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

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

System.out.println("You guessed the correct number in " + guessCount + " guesses. ");

if (guessCount <= 3)

System.out.println("Great work! you are a mathematical wizard");

else if (guessCount > 3 && guessCount <= 7)

System.out.println("Not too bad! you have got some potential");

else if (guessCount > 7)

System.out.println("What took you so long! may be you should take some session");

System.out.println("Bye");

}

}

// The sample run

Welcome to the number guessing game

I have selected a number between 0 and 50

Enter your guess
a
Please enter a valid number
Enter your guess
30
Your guess is too low

Enter your guess
49
Your guess is too high

Enter your guess
45
Your guess is too high

Enter your guess
40
Your guess is too low

Enter your guess
52
Number should be less than 50
Enter your guess
41
Your guess is too low

Enter your guess
0
Number should be greater than 0
Enter your guess
45
Your guess is too high

Enter your guess
46
Your guess is too high

Enter your guess
43
Your guess is too low

Enter your guess
44
Correct
You guessed the correct number in 12 guesses.

What took you so long! may be you should take some session
Bye