Question

Question 1 - Debugging Java Problem Description: Commonly attributed to Grace Hopper in the 1940s (when...

Question 1 - Debugging Java

Problem Description:
Commonly attributed to Grace Hopper in the 1940s (when a moth found in a computer relay was fouling outputs), debugging is the practice of removing errors from code. It is a systematic procedure of examining the output, drawing a hypothesis for the cause of the error, then either implementing a correction or otherwise validating or falsifying the original error hypothesis. We are hunting and correcting errors in a controlled, systematic fashion.

Most IDEs provide a debug tool called a 'breakpoint', which pauses execution at a specified line of code and allows the programmer to examine the state of variables at those points in time.

Another common tool is the 'step-through', which (after a halt is encountered), allows the programmer to move execution forward one line of code at a time (again, allowing you to examine how your variable states are changing).

It is worth noting that, should you lack an IDE, similar functionality may be achieved by the judicious use of console outputs.

When a function call produces an error, it is often instructive to ascertain the following:
- What are the argument states when the function starts?
- What are the argument states while the function performs its work?

  - What is the state of the return prior to the close of the function?

This practice is particularly useful within loops.

Task:

Based on the following code:

public class Main {

  public static void main( String[] args ) {

    int iterations = 0;

    int n = 0;

    

    while( n != 1 ) {

      n = (int)(Math.random() * 20);

      iterations++;

    } // Closing while

    System.out.println( "We escaped!");

    System.out.print( "" );

  } // Closing main()

} // Closing class main()

Set an appropriate breakpoint that allows you to examine the state of both 'n' and 'iterations' as the loop iterates and as it exits. Paste a screen capture of these variables below:

<TODO>

Homework Answers

Answer #1

Set the first break point at: while( n != 1 )

Set the second break point at: Closing parenthesis of while loop
n: = 2

iterations: = 1

n: = 14

iterations: = 2

n: = 4

iterations: = 3

n: = 19

iterations: = 4

n: = 6

iterations: = 5

n: = 14

iterations: = 6

n: = 11

iterations: = 7

n: = 1

iterations: = 8
We escaped!

Note: Change the code below in while loop to check of your own

while( n != 1 )
{
   n = (int)(Math.random() * 20);
   System.out.println("\n n: = " + n);
   iterations++;
   System.out.println("\n iterations: = " + iterations);
} // Closing while

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...