Lab 12 10 pts Week 5 Due Apr 29

Lab 12: Math Utilities

Static methods with parameters, return values, and overloading

methods parameters return-values overloading
Clone from GitHub
3 checkpoints 5 autograder 2 timeliness
Prerequisites: lesson-1-5 lesson-1-11

Learning Objectives

After completing this lab, you will be able to:

  • Write static methods that accept parameters and return values
  • Overload methods by changing parameter types while keeping the same name
  • Implement common math algorithms (absolute value, primality testing, factorial)
  • Distinguish between methods that return values and methods that print

What You’re Building

A MathUtils utility class containing static methods that mirror built-in math operations. You will implement max, min, and abs for int parameters, then overload them for double. Finally, you will add isPrime and factorial. All methods are pure functions: they take input, compute a result, and return it without printing.


Concepts and Common Misconceptions

Concept What students get wrong
Return values Writing System.out.println(result) inside the method instead of return result. The caller decides what to do with the value.
Overloading Thinking you can overload by changing only the return type. Java resolves overloads by parameter types, not return type.
isPrime edge cases Forgetting that 1 is not prime, 2 is prime, and negative numbers are not prime.
Factorial overflow int overflows at 13!. Use long to support up to 20!.
abs for Integer.MIN_VALUE Math.abs(Integer.MIN_VALUE) is negative due to overflow. For this lab, you do not need to handle that edge case, but know it exists.

Checkpoint 1: max, min, abs (1 pt)

Write three static methods that operate on int parameters:

  • max(int a, int b) – return the larger value
  • min(int a, int b) – return the smaller value
  • abs(int n) – return the absolute value

Do not use Math.max, Math.min, or Math.abs. Implement the logic with if statements or the ternary operator.

Debugging tip: Test with negative numbers and zero. max(-3, -7) should return -3, not -7. If your abs returns negative numbers, check that you are negating the input when it is negative: return (n < 0) ? -n : n;.


Checkpoint 2: Overloaded double Versions (1 pt)

Add overloaded versions of all three methods that accept double parameters:

  • max(double a, double b)
  • min(double a, double b)
  • abs(double n)

The logic is identical, but the parameter and return types are double. Java selects the correct version at compile time based on the argument types.

Test that max(3, 5) calls the int version and max(3.0, 5.0) calls the double version.

Debugging tip: If Java calls the wrong overload, check your argument types. max(3, 5.0) will call the double version because Java widens the int to double.


Checkpoint 3: isPrime and factorial (1 pt)

Add two more static methods:

  • isPrime(int n) – return true if n is prime, false otherwise. Check divisors from 2 up to Math.sqrt(n). Return false for values less than 2.
  • factorial(long n) – return n! as a long. Return 1 for n == 0.
isPrime(7)   // true
isPrime(1)   // false
isPrime(2)   // true
factorial(5) // 120
factorial(0) // 1

Debugging tip: If isPrime(2) returns false, your loop starts checking at 2 and immediately finds that 2 % 2 == 0. The fix: only flag a number as non-prime if the divisor is less than n itself.


How to Debug

  1. Test each method independently. Call max, min, abs with a variety of inputs from main before moving to overloading.
  2. Print intermediate values in isPrime. Log each divisor you test: System.out.println("checking " + i); to verify the loop range.
  3. Compare with Math.max/Math.min. Temporarily print both your result and the built-in result side by side.
  4. Watch for int vs long. If factorial(20) returns a negative number, you are using int instead of long.

Scoring Breakdown

Component Points
Checkpoint 1: int max/min/abs 1
Checkpoint 2: Overloaded double versions 1
Checkpoint 3: isPrime and factorial 1
Autograder correctness 5
Timeliness (on-time submission) 2
Total 10