Having problems with debugging Exercise 6-2: In this exercise, you’ll get more p
ID: 3747588 • Letter: H
Question
Having problems with debugging Exercise 6-2:
In this exercise, you’ll get more practice working NetBeans debugging capabilities by debugging a version of the Number Guessing Game that has bugs in it.
Find and fix the compile-time errors
1. Attempt to run the project. This should display an error message that contains links to the errors.
2. 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 actual error.
3. Find and fix 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 runtime error
4. Run the application to test it. Note that it contains a runtime error. Specifically, the game almost always tells you that your guess is 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.
5. Use the NetBeans debugger to find and fix the bug that is causing this problem.
The existing code is:
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;
}
}
Explanation / Answer
NumberGame.java
import java.util.Random;
public class NumberGame {
private int upperLimit;
private int number;
private int guessCount;
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 incrementGuessCount() {
guessCount = guessCount + 1;
}
}
______________
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);
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 = sc.nextInt();
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 = sc.nextInt();
}
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
Enter upper limit for guess: 50
Enter your guess: 20
Your guess is too low.
Enter your guess: 30
Your guess is too low.
Enter your guess: 40
Your guess is too high.
Enter your guess: 35
Your guess is too low.
Enter your guess: 37
Your guess is too low.
Enter your guess: 38
Correct!
You guessed the correct number in 6 guesses.
Bye!
_______________Could you plz rate me well.Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.