Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help Debugging the a number guessing game. I fixed the compile-time errors, but I'm having trouble figuring out the runtime error. Help as soon

Need help Debugging the a number guessing game. I fixed the compile-time errors, but I'm having trouble figuring out the runtime error. Help as soon as possible thanks!

In this exercise, youll get more practice working NetBeans debugging capabilities by debugging a version of the Number Guessing Game that has bugs in it.

Review the project

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

2. Review the code and note that NetBeans displays error icons for the compile time errors.

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.

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 actual error.

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

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

7. Use the NetBeans 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.

image text in transcribed

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 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!"); } } }

image text in transcribed

package murach.games;

import java.util.Random;

public class NumberGame { private int 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; } }

package murach.games: 31E import java.util. Scanner; public class Main public static void main (String args[) i System.out.println("Welcome to the Number Guessing Game") System.out.println ); Scanner sc = new scanner (System.in); 12 13 System.out.print ("Enter upper 1imit for guess:") int upperLimit Integer.parseInt (sc.nextLine )) NumberGame game new NumberGame (upperLimit); System.out.println ) 16 System.out.print ("Enter your guess: "); int guess = Integer.parseInt(sc.nextLine()); while (guess != game . getNumber()) 21 if (guessgame.getNumber System.out.println ("Your guess is too low.In") else if (guess> game.getNumber ) System.out.println ("Your guess is too high.In") game.incrementGuessCount ) System.out.print ("Enter your guess:") Integer.parseInt (sc.nextLine )) 27 System.out.println ("Correct!n") System.out.println ("You guessed the correct number in"+ 31 32 game.getGuessCount ) "guesses. " System.out.println("Bye!"): 36

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_2

Step: 3

blur-text-image_3

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

Database Systems For Advanced Applications 18th International Conference Dasfaa 2013 Wuhan China April 22 25 2013 Proceedings Part 2 Lncs 7826

Authors: Weiyi Meng ,Ling Feng ,Stephane Bressan ,Werner Winiwarter ,Wei Song

2013th Edition

3642374492, 978-3642374494

More Books

Students also viewed these Databases questions

Question

1. Discuss the four components of language.

Answered: 1 week ago

Question

f. What stereotypes were reinforced in the commercials?

Answered: 1 week ago