Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Exceptions { public static void method1 (int[] P, int x) { System.out.print(A); try { System.out.print(B); P[x] = 5; //method2(P, x); System.out.print(C); } catch

public class Exceptions { public static void method1 (int[] P, int x) { System.out.print("A"); try { System.out.print("B"); P[x] = 5; //method2(P, x); System.out.print("C"); } catch (ArrayIndexOutOfBoundsException e) { System.out.print("D"); } catch (Exception e) { System.out.print("E"); } finally { System.out.print("F"); } System.out.print("G"); } public static void method2 (int[] P, int x) { try { P[x] = 5; } catch (Exception e) { System.out.print("X"); } } public static void main(String[] args) { // i) method1(new int[7], 9); System.out.println(); // ii) method1(null, 5); System.out.println(); // iii) method1(new int[7], 3); } }

public class DebuggingExercise1 { public static void main(String[] args) { int[][] testArray = new int[5][6]; for (int i = 0; i < 5; i++) for (int j = 1; j <= 6; j++) testArray[i][j] = (i + 1) * j; printArray(testArray); } private static void printArray(int[][] array) { for (int i = 0; i < array.length; ++i) for (int j = 0; j < array[0].length; ++j) { System.out.print(array[i][j]); if (j < array[0].length - 1) System.out.print(", "); else System.out.print(" "); } } }

public class DebuggingExercise2 { private static int load = 1; public static void main(String[] args) { int i; int total; i = process(2); total = (i * 100) / load; if (load == 0) System.out.println("The value of total is infinity"); else System.out.println("The value of total is "+total); } private static int process(int step) { int i = 1; int result = step; for (; i <= 3; ++i) { result = result * i * step + step; if (result > 100) { load = 0; step = step - result; } else { load = load * result; ++step; } } // What are the values of i, step, result, and load? return result; } }

public class DebuggingExercise3 { private static int var1; private static MyObject obj1; private static void method1 (int var1) { var1 = 10; } private static void method2 (MyObject obj2) { MyObject obj1 = new MyObject("joe"); if (obj1 == obj2) var1 = 20; else var1 = -20; obj2 = new MyObject("john"); } public static void main(String[] args) { var1 = 2; obj1 = new MyObject("joe"); for (int i = 1; i < 2; ++i) { int var1 = i; } i = 8; method2(obj1); // What are the values of var1 and obj1.name? method1(var1); // What are the values of var1 and obj1.name? } }

public class MyObject { private String name; public MyObject(String theName) { name = theName; } public void setName(String newName) { name = newName; } public String getName() { return name; } }

Exercise 1 Follow the exception handling execution

1. Open the Exceptions.java file in Eclipse. Do not run it yet. 2. Examine the code to think about the flow of execution. 3. Jot down what you think the program would output to the console from each of the 3 cases (the calls to method1() from the main method). 4. Once you have finished hand-tracing the program, run it to see the actual output. 5. Were you correct? If not, read through the code again to understand why it executed this way. 6. Now comment out Line 10 (P[x] = 5) and un-comment Line 11 (method2(P, x)). 7. Read the code within method2() and hand-trace the execution on paper with this small change in the code. 8. Once you have thought about the expected output, run the program to see the actual output. 9. Were you correct? If not, read through the code again to understand why it executed this way.

Exercise 2 Using the Debugging Tool

1. Open DebuggingExercise1.java and run it. You will see that it compiles but it crashes when you run it. Read the error message and try to understand it. It will give you some information that may help you find the source of the error. 2. Check that you have a Debug button in the top right corner of your Eclipse window (a button with a little green bug in it), beside the Java button. If you don't, select Window > Perspective > Open Perspective > Debug. 3. Click on the Debug button to change to the Debug view. You can, at any time, switch back to the Java view by clicking on the Java button in the top right corner (the button with the blue letter J located to the left of the debug button). 4. In the window displaying your code, add a breakpoint to the line for (int j=1; j Debug (alternatively, press the key F11). 6. You will see that the line for (int j=1; j" symbol to its left, the debugger shows 5 new variables ([0], [1], [2], [3], and [4]), each one of them references an array of length 6; these are the rows of the 2-dimensional array. If you click on the ">" symbol to the left of [0], six new variables will be shown; these are the columns of the first row of the two dimensional matrix. 7. Now you can use F5 to execute your program one instruction at a time (this is called single-stepping). Press F5 once. Note how the variables i and j, and the values stored in the 2-dimensional array are changing in the Variables window. Why did testArray[0][0] not change? 8. Keep pressing F5 and stop when the value of testArray[0][5] changes value from 0 to 5 and the statement for (int j=1; j<=6; j++) is highlighted in green. Push key F5 one more time. What are the values of i and j? Is it correct that the program tries to store the value (i+1)*j in testArray[i][j]? 9. After answering the previous questions, you should know what is wrong with the program. Fix the error. Run the program and make it sure that it does not crash; it should print 30 values. 10. Modify the code so all entries of testArray are properly initialized. The first row of testArray should store the values: 1, 2, 3, 4, 5, 6; the second row should store the values: 2, 4, 6, 8, 10, 12; the third row: 3, 6, 9, 12, 15, 18; the fourth row: 4, 8, 12, 16, 20, 24, and the last row: 5, 10, 15, 20, 25, 30.

Exercise 3 Using the Debugging Tool

1. For this and the following exercises you CANNOT add any print or println statements to the code that you are testing. 2. Open DebuggingExercise2.java and run it. It will also crash. 3. Add a breakpoint to the line i = process(2); 4. Run the program in Debug mode. 5. Keep pressing F5 until you reach the statement return result; (do not execute this statement yet). 6. Using the debugger, find out the values of the variables: i, step, result, and load. 7. Since load is a static variable, to see its value in the Variables window you need to click on the little inverted triangle in the upper right corner of the Variables window; this will bring two options: "Layout" and "Java". Click on Java and then select "Show static variables" (see a screenshot of this option). 8. Terminate the program by selecting Run > Terminate. 9. Run this program again and stop execution at statement i = process(2). 10. This time press the key F6 instead of the key F5. You will see that the debugger now does not go inside method process(), but it goes directly to statement total = (i * 100) / load (key F5 tells the debugger to "step into" a method, while key F6 makes the debugger "step over" a method). What is the value of load? Why does the program crash if you keep pressing F6? (Do not press F5 or the debugger will try to step into the code of the Java's virtual machine). 11. Fix the program so that it does not crash and it prints the message "The value of total is infinity". Change the statement i = process(2) to i = process(1) and run the program again; this time it must print "The value of total is 5". (hint: check if load is 0 before computing total = (i * 100) / load).

Exercise 4 Using the Debugging Tool

1. Open DebuggingExercise3.java and run it. It will not compile. 2. Which line causes the compilation error? Why does the compiler complain about this line even though i has been declared in the statement: for (int i = 1; i < 2; ++i) (hint: what is the scope of i?) 3. Fix the code by commenting out the line with the compilation error. 4. Run the program; it should not produce any output. 5. Study the code for this class and try to determine without running the program what the values for the variables var1 and obj1 are immediately after method2(obj1) is executed. Write down your answers. Now try to determine the values of the variables immediately after method1(var1) is executed. Write down your answers again.6. Now use the debugger to determine the actual values of the above variables. To see the string stored in obj1, in the Variables window click on the ">" symbol to the left of obj1; you will see now the value of name. 7. Were you correct? If not, study the code and try to understand why the variables have the values shown by the debugger. 8. Add a breakpoint at the line if (obj1 == obj2). Press F11 to run the program in debugging mode and stop at this statement. 9. Press F5. Why is the result of the comparison false even though both obj1 and obj2 contain the same information, namely "joe"? 10. Look at the value of obj1 in the Variables window of the debugger. Since obj1 is a nonprimitive variable its value is the address of an object of the class MyObject. Note that the debugger does not show the actual address of the object referenced by obj1; instead it displays the address in a user-friendly format: MyObject (id = 19) you might get a different value for id. This means that the address stored in obj1 is the address of an object of class MyObject and the debugger assigns it a unique internal identifier (in the above example the identifier is 19). If you were to print the value of obj1 with System.out.println(obj1) you would get a similar output, not the actual address of the object but a string of the form MyObject@5ccd43c2; the Java virtual machine will assign a different identifier to the object (5ccd43c2) than the debugger.

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

Step: 3

blur-text-image

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 Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago