Debugging Exercise 3
Goal:
-
debug the
Functions.java
code. -
Use the step over
↷
button. See the Important Tips section for more information.
Setup
From Java Bootcamp Resources
-> Module 2
-> 8. Exception Handling
-> Debugging Workbooks
open the folder, exercise-three
.
If you can't find it, please download the updated resources from Github.
Debugging
The file contains TWO functions:
-
The first function should return a random number between 1 and 15.
-
The second function should check if that number is prime or composite.
-
A prime number is not divisible by anything except itself and one (17, 29, 41...).
-
Common mistake: 1 is not a prime number.
-
If you run the starter code, it will crash.
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Functions.isPrime(Functions.java:15)
at Functions.main(Functions.java:5)
Use breakpoints to visualize the runtime and debug the program line by line. Recall, you can use the step into ↓
button to step into a function.
Important Tips
-
Problem:
isPrime()
is called insideprintln()
. So, how do you step insideisPrime()
?- Solution: step into
↓
println()
. Then, step out↑
of it. The next step into↓
will step insideisPrime()
.
- Solution: step into
-
Problem: When inside a method, how do you avoid stepping into other methods?
- Solution: Use step over
↷
to execute the method without stepping in it.
- Solution: Use step over