Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS1027

Computer Science Fundamentals II

LAB 4

Important Note

Please do complete all the exercises in the labs as they contain important information that helps you understand the material taught in class and they will help you prepare for the midterm and final exams. The labs might look long, but it is because they contain     detailed instructions to make sure you get the whole benefit of the exercises.

Learning Outcomes

•    Hand-trace a program to follow its execution

•    Locate and fix compilation and run-time errors

•    Use the debugger tool in Eclipse to step through code and track variable values

•    Fix the errors that have been found in the code to make it run as expected

Pre-Lab

•    Create a new Java project called Lab4

•    Download the files: DebuggingExercise1.java, DebuggingExercise2.java,     DebuggingExercise3.java, Errors.java, MyObject.java, and AnswersLab4.txt.

•    Save these downloaded files into the Lab4 src folder

Exercise 1  Fix Errors and Follow the Execution of a Program

1.   Open the Errors.java file in Eclipse. You will see that it has several compilation errors.

2.   Fix the compilation error in line 12. If you need help, read the message that Eclipse displays when you hover the mouse over the little red x on the left side of the line; you can also click on the red x to see the suggestion made by Eclipse to fix the error.

3.  The errors in lines 16- 19 are not due to something wrong in these lines, but rather the  problem is in lines 3 and 4. If you click on the red x to the left of line 16 you will see      some suggestions that Eclipse makes to fix the errors. Only one of them makes sense. Which one? Write your answer in AnswersLab4.txt.

4.   Fix the errors by changing the declaration of variables arr and i in lines 3 and 4.

5.  There is still an error. Hover the mouse over the red x to the left of line 21. You will probably not understand the error messages that Eclipse prints, because Eclipse thinks that at this line you are trying to declare another method. What is the real error in this    line? Write your answer in AnswersLab4.txt.

6.   Fix the error. Do not read the hint, unless you really have no idea of what causes the error.

7.   Now that you have fixed all compilation errors, you can run the program. Run it. It will crash. Read the error message printed by the Java virtual machine. What line caused the error and what is the error? Write your answers in AnswersLab4.txt.

8.   Fix the code by changing line 18, but do not run the program yet. Hand-trace the execution of the program and write in AnswersLab4.txt what you think the program is going to print. Notice that the for loop in line 16 has the comparison

i < Errors.i

Recall that every Java class has a single static object of the same name as the class.  So, class Errors has a static object called Errors. If you performed step 3 correctly and   changed the declarations of variables i and arr to fix the compilation errors, then these   variables are stored inside the static object Errors and so the value of instance variable i is accessed through Errors.i.

9.   Run the program and check if your predictions of what the program was going to print were correct.

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. Which line of the program    causes it to crash? Write your answer in AnswersLab4.txt.

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). Click on the Debug button to change to the Debug view. If you do not see the debug button, select Window > Perspective > Open          Perspective > Debug.

3.  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 little 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<=6; j++). You do this by right-clicking in this line at the very left, on the blue bar, and then clicking Toggle Breakpoint. You can remove a breakpoint at any time in the exact same way.

You can also add a breakpoint by double-clicking on the blue bar to the left of the line   numbers. A breakpoint is indicated by a small blue circle. Make sure that the breakpoint is in line 7.

5.  When executing your program, the debugger will stop every time this line is reached.     Run your program in the debug mode by selecting on Run > Debug (alternatively, press the key F11, or click on the little bug symbol at the tools bar).

6.  You will see that the line for (int j=1; j<=6; j++) is highlighted in green. The   execution of the program has stopped at this point. In the Variables window (upper right corner) you will see the variables of the program: args, testArray, and i and their values.

Since testArray is a variable referencing a 2-dimensional array, or an array in which every entry is an array of integers, when you click on the ">" 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 or F6 (or click on the “Step Into” or “Step Over” icons in the tool bar) to execute your program one instruction at a time (this is called single-stepping). Press F6 once. Note how the variables i and j and the values stored in the 2-dimensional array are changing in the Variables window. Press F6 again. Why did the value of testArray[0][0] not change? Write your answer in AnswersLab4.txt.

 

8.   Keep pressing F6 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 F6 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]? Write your answer in AnswersLab4.txt.

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. Which line of the program causes it to crash? Write your answer in AnswersLab4.txt.

3.  Add a breakpoint to the line i = process(2);

4.   Run the program in Debug mode.

5.   Press F6; what is the line that is highlighted? Write your answer in AnswersLab4.txt.

6.   Stop the program by clicking on the small red square near the upper left corner of Eclipse. Run the program in Debug mode again. This time press F5 instead of F6. What is the line that is highlighted this time. Write your answer in AnswersLab4.txt.

7.  What it the difference between F6 (“Step over”) and F5 (“Step into”)? Write your answer in AnswersLab4.txt.

8.   Keep pressing F6 until you reach the statement return result; (do not execute this statement yet).

9.   Using the debugger, find out the values of the variables: i, step, result, and load. Write your answers in AnswersLab4.txt.

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 (in some  versions of Eclipse instead of a triangle you might see 3 white dots); this will bring two   options: "Layout" and "Java". Click on Java and then select "Show static variables" .

 

10. Terminate the program by selecting Run > Terminate.

11. Run this program again and stop execution at statement i = process(2).

12. 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. What is the value of variable load? Why does the program crash if    you press F6? Write your answer in AnswersLab4.txt. (Do not press F5 or the debugger will try to step into the code of the Java's virtual machine).

13. Fix the program so that it does not crash, and it prints the message "The value of total is infinity". [Hint. To do this move the statement total = (i * 100) / load to inside the if statement below it (where in the if statement?).]

14. 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".

Exercise 4  Using the Debugging Tool

1.   Open DebuggingExercise3.java and run it. It has a compilation error.

2.  Why does the compiler mark an error in this line even though i has been declared in the statement: for (int i = 1; i < 2; ++i). Write your answer in AnswersLab4.txt. (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 the String stored in obj1 are immediately after method2(obj1) is executed. Write down your answers in AnswersLab4.txt.   Now try to determine without running the program the values of these variables immediately after method1(var1) is executed. Write down your answers in   AnswersLab4.txt.

6.   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; now you will see the value of instance variable name. Write the correct values of var1  and obj1 immediately after method2(obj) is executed.

7.   Stop the program. Add a breakpoint at the line if (obj1 == obj2). Press F11 to run the program in debugging mode and stop at this statement.

8.   Press F5. Why is the result of the comparison false even though both obj1 and obj2 contain the same information, namely "joe"? Write down your answers in AnswersLab4.txt.

9.   Look at the value of obj1 in the Variables window of the debugger. Since obj1 is a non-  primitive 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 of the form

MyObject@5ccd43c2, where 5ccd43c2 is the representation of the address of the object.

Submission

When you have completed the lab, navigate to the weekly module page on OWL and click the   Lab link (where you found this document). Make sure you are in the page for the correct lab.      Upload the files listed below and remember to hit Save and Submit. Check that your submission went through and look for an automatic OWL email to verify that it was submitted successfully.

Rules

•    Please only submit the files specified below. Do not attach other files even if they were part of the lab.

•    Do not ZIP or use any other form of compressed file for your files. Attach them individually.

•    Submit the lab on time. Late submissions will receive a penalty.

•    Forgetting to hit "Submit" is not a valid excuse for submitting late.

•    Submitting the files in an incorrect submission page will receive a penalty.

•   You may re-submit code if your previous submission was not complete or correct, however, re-submissions after the regular lab deadline will receive a penalty.

Files to submit

•   AnswersLab4.txt

•    Errors.java

•    DebuggingExercise1.java

•    DebuggingExercise2.java

•    DebuggingExercise3.java