Popcorn Hack 1: Write and Test max Overloads

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
    }
}

PopcornMax.main(null);
9
-2
3.5
2.0

Popcorn Hack 2: Overload print for int and String

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

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

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

PopcornPrint.main(null);
int:42
str:hello
str:A!

MCQs

Short Answer

Q1: int sum(int a, int b) and double sum(int a, int b) cannot both exist because Java cannot differentiate methods by return type alone — parameter lists must differ.

Q2:

  • Parameters are variables defined in the method declaration.

  • Arguments are the actual values passed to those parameters when calling the method.

Coding Tasks

public class AbsOverloads {
    public static int abs(int x) {
        return x < 0 ? -x : x;
    }

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

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

    public static void main(String[] args) {
        System.out.println(abs(-5));      // 5
        System.out.println(abs(-3.2));    // 3.2
        System.out.println(abs(-9L));     // 9
    }
}

AbsOverloads.main(null);

5
3.2
9
public class ConcatOverloads {
    // Concatenates two strings
    public static String concat(String a, String b) {
        return a + b;
    }

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

    public static void main(String[] args) {
        System.out.println(concat("Hi", "There"));   // HiThere
        System.out.println(concat("Go", 3));         // GoGoGo
    }
}

ConcatOverloads.main(null);
HiThere
GoGoGo
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");
}

public static void main(String[] args) {
    show(7);
    show(7L);
    show(7.0);
}

show(7);    // int
show(7L);   // long
show(7.0);  // double

int
long
double

FRQ 1

/**
 * Finds the index of the first occurrence of a character in a string.
 *
 * @param target the character to search for
 * @param s the string to search in
 * @return the index of the first occurrence of target, or -1 if not found
 */
public static int indexOf(char target, String s) {
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == target) {
            return i;
        }
    }
    return -1;
}

/**
 * Finds the index of the first occurrence of a substring in a string.
 *
 * @param target the substring to search for
 * @param s the string to search in
 * @return the starting index of the first occurrence, or -1 if not found
 */
public static int indexOf(String target, String s) {
    for (int i = 0; i <= s.length() - target.length(); i++) {
        if (s.substring(i, i + target.length()).equals(target)) {
            return i;
        }
    }
    return -1;
}

FRQ 2

/**
 * Restricts an integer value within a given range.
 *
 * @param value the number to clamp
 * @param low the lower bound
 * @param high the upper bound
 * @return the clamped value
 */
public static int clamp(int value, int low, int high) {
    if (low > high) {
        int temp = low;
        low = high;
        high = temp;
    }
    if (value < low) return low;
    if (value > high) return high;
    return value;
}

/**
 * Restricts a double value within a given range.
 *
 * @param value the number to clamp
 * @param low the lower bound
 * @param high the upper bound
 * @return the clamped value
 */
public static double clamp(double value, double low, double high) {
    if (low > high) {
        double temp = low;
        low = high;
        high = temp;
    }
    if (value < low) return low;
    if (value > high) return high;
    return value;
}