COMP 1510 Lab 7
COMP 1510 Lab 7
1. Open Eclipse. Remember to keep all your projects in the same workspace. Think of a
workspace as a shelf, and each project is a binder of stuff on the shelf. If you need some
help organizing your workspace, we’d be happy to help!
2. Create a new Non Modular Java project. Call it Comp1510Lab07LastNameFirstInitial
3. Create a Microsoft Word document Comp1510Lab07LastNameFirstInitial.docx to hold
your answers to the questions below (wherever the lab below wants a screenshot or
answer to a question).
4. Complete the following tasks. Remember you can right-click the src file in your lab 7
project to quickly create a new package and Java class.
5. When you have completed the exercises, show them to your lab instructor. Be
prepared to answer some questions about your code and the choices you made.
1 of 12
COMP 1510 Bruce Link, Carly Orr, Ife Agboola, Yue Wang, Chris Thompson
No source code files are created for you by default. Also, the IDE gives to access to a variety of
libraries that are represented by .jar package files.
2 of 12
COMP 1510 Bruce Link, Carly Orr, Ife Agboola, Yue Wang, Chris Thompson
b. Create Class file content. Replace the entire main() method with the following
code:
c. Add additional Classes, and an Interface Definition. Add the following code at the
very bottom end of the file, outside of the class definition.
3 of 12
COMP 1510 Bruce Link, Carly Orr, Ife Agboola, Yue Wang, Chris Thompson
interface Operation {
int perform(int a, int b);
}
class Add implements Operation {
@Override
public int perform(final int a, final int b)
{
return (a + b);
}
}
class Subtract implements Operation {
@Override
public int perform(final int a, final int b) {
return (a - b);
}
}
class Factorial {
int perform(final int n) {
int ret;
ret = 1;
for(int i = 1; i < n; i++) {
ret *= i;
}
return (ret);
}
}
3. Run Program.
Run the program and look at the console output. The above program essentially does: 6 + 7
then 6 – 7 then 6! (6 factorial). This program is convoluted to allow us to highlight some
debugger features. We can use the debugger to help us understand what the program is doing.
First, make sure that line numbers are displayed in your sources files. In Eclipse, go to
WindowàPreferencesàGeneralàText Editorà check box
for line number.
The most basic thing you will do in a debugger is use a
breakpoint. A breakpoint is a line (point) in your program that
you want the debugger to pause (break) at. There are a few
ways to set a breakpoint, one of them is to right-click at the
line you want to set the breakpoint on (in this case the very
first line in the main method) and select the Toggle
Breakpoint from the sub menu.
4 of 12
COMP 1510 Bruce Link, Carly Orr, Ife Agboola, Yue Wang, Chris Thompson
You can disable the break point by right-clicking on the dot and toggling the breakpoint. You
will see that the breakpoint disappears. Click the same spot again and you will see that the
breakpoint comes back. You can also toggle a breakpoint by hitting Ctrl-Shift-B on the keyboard
(or CMD-Shift-B on SX).
5 of 12
COMP 1510 Bruce Link, Carly Orr, Ife Agboola, Yue Wang, Chris Thompson
The most common activity you will probably use in the debugger is to step through the code.
There are several options, such as
6 of 12
COMP 1510 Bruce Link, Carly Orr, Ife Agboola, Yue Wang, Chris Thompson
You can even change the values if you want to (don’t do that for this tutorial though).
“Step Over (F6)” is used to execute individual lines of code. If you “Step Over” a method
call then the method is run and the debugger continues on after the method call.
As you step through the code the green line updates as does any variable values. This is a great
way to see how your code works.
Just keep stepping through the code and get comfortable with that. Keep going until the
program ends.
To return to your source code, click on the “Java” tab in the top right-hand
corner of your screen.
7 of 12
COMP 1510 Bruce Link, Carly Orr, Ife Agboola, Yue Wang, Chris Thompson
Notice that the Variables update as you step through the “for loop”. Make sure you stop
stepping on the return statement.
Use the Call Stack window to see what method calls were made. When you select a line on the
Call Stack window you will be taken to the method call.
Terminate and repeat the process to debug again. However this time, use “Step-Return”
before the “for” loop is over. What happens?
You can set a condition for when the debugger will break. There are several ways to set a
condition. You can set the “Hit Count” to be 3. After your matched setting as shown, click
on the OK button.
Now, set a “conditional” breakpoint so that it stops at the condition i==5 instead.
Run the program until it stops at the conditional breakpoint. Show a screen capture of
the Variables Windows from the Debug Perspective.
8 of 12
COMP 1510 Bruce Link, Carly Orr, Ife Agboola, Yue Wang, Chris Thompson
TABLE ONE
Question: What is the Eclipse keyboard shortcut for toggling a breakpoint?
Answer:
Question: What is the difference between “Step-Over”, and “Step-Into”, and “Step-Return”?
Answer:
It is ok if you don’t understand all of the java code; but you should be able to trace the order in which statements
are executed.
Based on your best understanding of the program, provide a list of methods that are called when the program
executes (from start to end, in order of being called). You can skip library methods (like println, for example).
HINT: Use a combination of “Step-Into” “Step-Over” and “Step-Return”. Use the “Stack Trace” window.
List of Methods (in order of call) below. Please use the fully qualified name, eg.
“DebugStar.run(String, int, int). Use the stack view to help you.
9 of 12
COMP 1510 Bruce Link, Carly Orr, Ife Agboola, Yue Wang, Chris Thompson
2. Provide an error log table (such as the one below) indicating error details (line number,
type of error, and explain error and show correction).
TABLE TWO
Line Type of error Description Correction
Number (compile-time,
run-time, or
logical)
… … … …
… … … …
3. Provide screen capture of fixed code (with line numbers), and sample run, using n=10:
10 of 12
COMP 1510 Bruce Link, Carly Orr, Ife Agboola, Yue Wang, Chris Thompson
2. Now click on Add Folder, then click on Create New Folder and enter test in the Folder
name field that pops up. The result should look as follows:
3. Now click on the OK button, then the Apply and Close button on the Build Path
Window. You should see a test folder in your project that looks like The
symbol on the folder indicates you have set up a new source folder (if no folders have
this symbol, then eclipse will not compile any of your code resulting in massive
confusion).
4. From now on, you should put unit test code under a test folder and your regular code
under the src folder. Add a package ca.bcit.comp1510.lab07 to the test folder.
5. From the learning hub, down load the file TestThis.java and copy it into your src folder
in the ca.bcit.comp1510.lab07 package. This will be what we are testing. There are two
methods, both finding the largest value of integers.
6. Right click on the package in the test folder and chose New > other and type JUnit in the
text field as follow:
Then select Junit Test Case, and in the New Junit Test Case wizard, select New Junit Jupiter
Test and enter the test class name (TestThisTest) and the class under test (TestThis) and
click Next.
7. On the next screen, under TestThis, click the check boxes in front of the two methods
(largest() with different parameters) and click Finish.
8. If you have not already added Junit to your build path, click OK on the message that
pops up (or add Junit 5 to the library manually).
11 of 12
COMP 1510 Bruce Link, Carly Orr, Ife Agboola, Yue Wang, Chris Thompson
9. Now it is a matter of adding test methods. Each method should call one of the methods
of the TestThis class and use an assert method (such as assertEquals) to check the
correct result. Each method should have a name indicating what is being tested. You
can cut and paste the ones provided by eclipse and add numbers at the end.
10. Start with methods named, for example (you can come up with better names):
a. testLargestIntIntInt1
b. testLargestIntIntInt2
c. testLargestIntIntInt3
d. testLargestListOfInteger1
e. testLargestListOfInteger2
f. testLargestListOfInteger1
g. and so on.
11. Fix any errors your tests find.
12. When you think you have done enough testing, run the Coverage tool to see if you need
more. You should be able to get complete coverage, and all tests green!
12 of 12