Download the file DebuggingExercise.java and create a new project Click on the D
ID: 3915812 • Letter: D
Question
Download the file DebuggingExercise.java and create a new projectClick on the Debug button to change to the Debug view.
In the window displaying your code, you will now add a breakpoint to the line for (int j=1; j<=6; j++).
When executing your program Now you can use F5 to execute your program one instruction at a time (called single-stepping) or F8 to continue with the execution until the next breakpoint is reached (in a loop the next breakpoint might be the same one again).
While you are doing this, expand some of the rows and columns of the array testArray in the Variables window, to see how the variables are changing. Fix the error when you find it. 1 import java.io.*; 4 public class DebuggingExercise public static void main (String[] args) intIlll testArray new int [51 [61; 9 for(int i-0;i
Explanation / Answer
Initially i = 0,
testArray[0][1] = ( 0 + 1 ) * 1
= 1
testArray[0][2] = ( 0 + 1 ) * 2
= 2
testArray[0][1] = ( 0 + 1 ) * 3
= 3
testArray[0][1] = ( 0 + 1 ) * 4
= 4
testArray[0][1] = ( 0 + 1 ) * 5
= 5
It causes a IndexOutOfBounds Exception as the size of testArray is 5x6. So, each column goes from 0 to 5. But we try to access with column 6, which causes exception.
The correct code is
import java.io.*;
public class DebuggingExercise{
public static void main(String[] args)
{
int[][] testArray = new int[5][6];
for( int i = 0 ; i < 5 ; i++ )
{
// j goes from 0 to 5
for( int j = 0 ; j < 6 ; j++ )
testArray[i][j] = ( i + 1 ) * j;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.