Java Bootcamp – Workbooks and Challenges
)}

Debugging Exercise 3

Goal:

  1. debug the Functions.java code.

  2. 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:

  1. The first function should return a random number between 1 and 15.

  2. 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


  1. Problem: isPrime() is called inside println(). So, how do you step inside isPrime()?

    • Solution: step into println(). Then, step out of it. The next step into will step inside isPrime().
  2. 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.

Good Luck!


Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.