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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT