Skip to the content.

Method Signatures

Student Teaching Lesson

Popcorn Hack 1

public class PopcornMax {
    // int overload
    public static int max(int a, int b) {
        return a >= b ? a : b;
    }

    // double overload
    public static double max(double a, double b) {
        return a >= b ? a : b;
    }

    public static void main(String[] args) {
        System.out.println(max(3, 9));      // 9
        System.out.println(max(-2, -7));    // -2
        System.out.println(max(3.5, 2.9));  // 3.5
        System.out.println(max(2, 2.0));    // 2.0  (int 2 promoted to double → calls max(double,double))
    }
}
PopcornMax.main(null);
9
-2
3.5
2.0

Popcorn Hack 2

public class PopcornPrint {
    static void print(int n) {
        System.out.println("int:" + n);
    }

    static void print(String s) {
        System.out.println("str:" + s);
    }

    public static void main(String[] args) {
        print(42);          // expected: int:42
        print("hello");     // expected: str:hello
        print('A' + "!");   // expected: str:A!
    }
}
PopcornPrint.main(null);
int:42
str:hello
str:A!

FRQ

public class FrqTemplates {
    // FRQ 1
    public static int sumRange(int start, int end) {
        if (start > end) return 0;
        int sum = 0;
        for (int n = start; n <= end; n++) {
            sum += n;
        }
        return sum;
    }

    // FRQ 2
    public static double area(double radius) {
        return Math.PI * radius * radius;
    }
    public static int area(int width, int height) {
        return width * height;
    }

    // FRQ 3
    public static String formatScore(int earned, int total) {
        return earned + "/" + total;
    }
    public static String formatScore(double percent) {
        return String.format("%.1f%%", percent);
    }

    public static void main(String[] args) {
        System.out.println(sumRange(1, 5));      // expected: 15
        System.out.println(area(3.0));           // expected: ~28.274333882308138
        System.out.println(area(3, 4));          // expected: 12
        System.out.println(formatScore(45, 50)); // expected: 45/50
        System.out.println(formatScore(92.35));  // expected: 92.4%
    }
}
FrqTemplates.main(null);
15
28.274333882308138
12
45/50
92.4%


HOMEWORK: mcq

Q1

// A
class A {
    int f(int a) { return a; }
    int f(int x) { return x; }  
}
|       int f(int x) { return x; }  

method f(int) is already defined in class A
// B
class B {
    int f(int a) { return a; }
    double f(int a) { return a; } 
}
|       double f(int a) { return a; } 

method f(int) is already defined in class B
// C
class C {
    int f(int a) { System.out.println("C.f(int)"); return a; }
   
Incomplete input:

|   // C

|   class C {

|       int f(int a) { System.out.println("C.f(int)"); return a; }
// D
class D {
    int f(int a, int b) { return a+b; }
    int f(int a, int b) { return a-b; }  
}
|       int f(int a, int b) { return a-b; }  

method f(int,int) is already defined in class D

Answer: C


Q2

class SignatureTest {
    int max(int a, int b) { return a; }
    int max(int x, int y) { return b; } 
    int max(int a, int b) { return a; }
    double max(int a, double b) { return b; } 
}
|       int max(int x, int y) { return b; } 

method max(int,int) is already defined in class SignatureTest



|       int max(int a, int b) { return a; }

method max(int,int) is already defined in class SignatureTest



|       int max(int x, int y) { return b; } 

cannot find symbol

  symbol:   variable b

Answer: B


Q3

class OverloadTest {
    static void p(int x) {
        System.out.println("int");
    }
    static void p(double x) {
        System.out.println("double");
    }
}

OverloadTest.p(5);
OverloadTest.p(5.0);
int
double

Answer: A


Q4

class AmbiguousTest {
    static void h(long x) { System.out.println("long"); }
    static void h(float x) { System.out.println("float"); }
}

AmbiguousTest.h(5.0);    
AmbiguousTest.h(5f);  

|   AmbiguousTest.h(5.0);

no suitable method found for h(double)

    method AmbiguousTest.h(long) is not applicable

      (argument mismatch; possible lossy conversion from double to long)

    method AmbiguousTest.h(float) is not applicable

      (argument mismatch; possible lossy conversion from double to float)

Answer: A


Q5

answer: D

Overloading requires different parameter lists, this is exactly how Java distinguishes between overloaded methods.



HOMEWORK: coding task

public class OverloadTasks {

    // 1️⃣ Three overloads of abs
    static int abs(int x) {
        return (x < 0) ? -x : x;
    }

    static double abs(double x) {
        return (x < 0) ? -x : x;
    }

    static long abs(long x) {
        return (x < 0) ? -x : x;
    }

    // 2️⃣ Two overloads of concat
    static String concat(String a, String b) {
        return a + b;
    }

    static String concat(String a, int n) {
        String result = "";
        for (int i = 0; i < n; i++) {
            result += a;
        }
        return result;
    }

    // 3️⃣ show() overloads
    static void show(int x) {
        System.out.println("int");
    }

    static void show(double x) {
        System.out.println("double");
    }

    static void show(long x) {
        System.out.println("long");
    }

    // 🧪 Main test
    public static void main(String[] args) {
        // Test abs
        System.out.println("abs(-5): " + abs(-5));
        System.out.println("abs(-5.5): " + abs(-5.5));
        System.out.println("abs(-5000000000L): " + abs(-5000000000L));

        System.out.println();

        // Test concat
        System.out.println("concat(\"Hi\", \"There\"): " + concat("Hi", "There"));
        System.out.println("concat(\"Go\", 3): " + concat("Go", 3));

        System.out.println();

        // Test show outputs
        show(7);     // int literal
        show(7L);    // long literal
        show(7.0);   // double literal
    }
}

// Run it 👇
OverloadTasks.main(null);
abs(-5): 5
abs(-5.5): 5.5
abs(-5000000000L): 5000000000

concat("Hi", "There"): HiThere
concat("Go", 3): GoGoGo

int
long
double

Explanations

abs(-5) → uses int version because the argument is an int literal.

abs(-5.5) → uses double version because the argument is a double literal.

abs(-5000000000L) → uses long version because the argument is a long literal.

concat(“Hi”, “There”) → joins two strings end-to-end.

concat(“Go”, 3) → repeats “Go” three times.

show(7) → prints “int” because 7 is an int literal.

show(7L) → prints “long” because 7L is a long literal.

show(7.0) → prints “double” because 7.0 is a double literal.



HOMEWORK: frq

public class FRQOverloads {

    // FRQ 1: indexOf (char version)
    // Assumptions:
    // - s is not null
    // - search is case sensitive
    // - returns -1 if target not found
    static int indexOf(char target, String s) {
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == target) {
                return i;
            }
        }
        return -1;
    }

    // FRQ 1: indexOf (substring version)
    // Assumptions:
    // - s and target are not null
    // - target is non-empty
    // - returns -1 if substring not found
    static int indexOf(String target, String s) {
        int tLen = target.length();
        int sLen = s.length();

        // loop through s, checking if target starts at each index
        for (int i = 0; i <= sLen - tLen; i++) {
            boolean match = true;
            for (int j = 0; j < tLen; j++) {
                if (s.charAt(i + j) != target.charAt(j)) {
                    match = false;
                    break;
                }
            }
            if (match) return i;
        }
        return -1;
    }

    // FRQ 2: clamp (int version)
    // Assumptions:
    // - low and high can be in any order; swap if low > high
    static int clamp(int value, int low, int high) {
        if (low > high) { // swap
            int temp = low;
            low = high;
            high = temp;
        }
        if (value < low) return low;
        if (value > high) return high;
        return value;
    }

    // FRQ 2: clamp (double version)
    static double clamp(double value, double low, double high) {
        if (low > high) { // swap
            double temp = low;
            low = high;
            high = temp;
        }
        if (value < low) return low;
        if (value > high) return high;
        return value;
    }

    // Main test
    public static void main(String[] args) {
        // FRQ 1 tests
        System.out.println(indexOf('a', "banana"));     // 1
        System.out.println(indexOf('z', "banana"));     // -1
        System.out.println(indexOf("ana", "banana"));   // 1
        System.out.println(indexOf("nana", "banana"));  // 2
        System.out.println(indexOf("zzz", "banana"));   // -1

        System.out.println();

        // FRQ 2 tests
        System.out.println(clamp(5, 1, 10));       // 5
        System.out.println(clamp(-5, 1, 10));      // 1
        System.out.println(clamp(15, 1, 10));      // 10
        System.out.println(clamp(5, 10, 1));       // 5 (swap handled)

        System.out.println(clamp(3.5, 2.0, 5.0));  // 3.5
        System.out.println(clamp(1.0, 2.0, 5.0));  // 2.0
        System.out.println(clamp(8.0, 2.0, 5.0));  // 5.0
        System.out.println(clamp(4.0, 7.0, 2.0));  // 4.0 (swap handled)
    }
}

// Run in Jupyter:
FRQOverloads.main(null);

1
-1
1
2
-1

5
1
10
5
3.5
2.0
5.0
4.0