CS代考 CS1027 LAB 5 Computer Science Fundamentals II

CS1027 LAB 5 Computer Science Fundamentals II
Learning Outcomes
• Hand-trace a program to anticipate the flow of execution
• Analyze the effects of try-catch statements on the overall program execution

Copyright By PowCoder代写 加微信 powcoder

• Locate the source of errors using the messages displayed in the console
• Use the Debugger Tool in Eclipse to step through code and track variable values to
understand the reason for such errors
• Fix the errors that have been found in the code to make it run as expected
• Create a new Java project called Lab5
• Download the files: Exceptions.java, DebuggingExercise1.java,
DebuggingExercise2.java, DebuggingExercise3.java, MyObject.java, and
AnswersLab5.txt.
• Save the downloaded java files into the Lab5 src folder 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. Hand-trace the execution of the program and in file AnswersLab5 write down what you
think the program would output to the console after each invocation to method1() from
the main method.
4. Run the program to see the actual output.
5. How many of your answers were incorrect? Write this information in AnswersLab5.txt.
6. Now comment out Line 10 (P[x] = 5) and un-comment Line 11 (method2(P, x)).
7. Hand-trace the execution of the program and write in AnswersLab5 what you think the
program would output.
8. Run the program to see the actual output.
9. How many of your answers were incorrect? Write this information in AnswersLab5.txt.
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 the exception to be thrown? Write your answer in AnswersLab5.
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).

CS1027 LAB 5 Computer Science Fundamentals II
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, 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 doble-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 located in line 7. 5. When executing your program, the debugger will stop every time the line with the breakpoint is reached. Run your program in the debug mode by selecting on Run > Debug (alternatively, press the key F11).
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 window) 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 five 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 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 testArray[0][0] not change? Write your answer in AnswersLab5.
testArray[0][0]
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 answers in AnswersLab5. CS1027 LAB 5 Computer Science Fundamentals II 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 initialized so that the first row of testArray stores the values: 1, 2, 3, 4, 5, 6; the second row stores 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 the exception to be thrown? Write your answer in AnswersLab5. 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 it highlighted? Write your answer in AnswersLab5. 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; what is the line that it highlighted this time? Write your answer in AnswersLab5. 7. What is the difference between F5 and F6? Write your answer in AnswersLab5. 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 AnswersLab5. 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 load? Why does the program crash if you press

CS1027 LAB 5 Computer Science Fundamentals II
F6? Write your answer in AnswersLab5. (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”. 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. It has a compilation error in line 27.
2. Why does the compiler marks 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 AnswersLab5. (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 in AnswersLab5. Now try to determine the values of the variables immediately after method1(var1) is executed. Write down your answers in AsnwersLab5. 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; you will see now the value of name. Write the correct values of var1 and obj1 immediately after method2(obj) is executed.
7. 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 your answer in AnswersLab5.
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, not the actual address of the object but a string of the form the Java virtual machine will assign a different identifier to the object (5ccd43c2) than the debugger.
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.

CS1027 LAB 5 Computer Science Fundamentals II
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.
• 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
• DebuggingExercise1.java • DebuggingExercise2.java • DebuggingExercise3.java • AnswersLab5.txt

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com