Question: Find and fix the compile-time errors 3. Attempt to run the project. This should display an error message that contains links to the errors. Copy

Find and fix the compile-time errors

3. Attempt to run the project. This should display an error message that contains links to the errors. Copy the output to the Word Document.

4. Find and fix these errors by clicking on the links to jump to the code for the error. Sometimes the error marker in the left margin may not be on the same line as the code causing the error. Insert a comment above the line of code causing each error which describes the error, and stating how each error was fixed.

5. Find, fix, and comment on any remaining compile-time errors. To do that, you can use the error icons to locate the compile-time errors. Then, you can fix them.

Find and fix the logic error

6. Run the application to test it. Note that it contains a logic error. Specifically, the game almost always tells you that your guess is either too high or too low, no matter which number you pick. For example, even if you chose 0, it might tell you that your guess is too high.

7. Use a debugger to find and fix the bug that is causing this problem.

Hint: As you step through the code, keep an eye on the value of the guess variable. Predict what the line of code will do and determine if that line of code did what is expected.

Add packages to the project and move the classes (review chapter 5)

8. create a new package named murach.games.guessing. Then, move the Main class into it.

9. create a new package named murach.games.business. Then, move the NumberGame class into it.

10. Run the project to make sure it still works correctly.

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

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); System.out.print("Enter upper limit for guess: "); int upperLimit = Integer.parseInt(sc.nextLine()); NumberGame game = new NumberGame(upperLimit); 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: "); 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-------------------------------------------------------------------

package murach.games;

import java.util.Random;

public class NumberGame { private iny number; private int guessCount; public NumberGame(int upperLimit) { Random random = new Random(); number = random.nextInt(upperLimit + 1); guessCount = 1; }

public int getNumber() { return number }

public int getGuessCount() { return guessCount; } public void incrementGuessCount() { guessCount = guessCount + 1; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!