Skip to the content.

Big Idea 1 Lesson 1.9

Big Idea 1 Lesson 1.9

Popcorn Hacks

Popcorn Hack 1

public class PopcornMax {
    // TODO: write int version of max
    // TODO: write double version of max
    public static int max(int a, int b) {
        if (a > b) {
            return a;
        } else {
            return b;
        }
    }

    public static double max(double a, double b) {
        if (a > b) {
            return a;
        } else {
            return b;
        }
    }

    public static void main(String[] args) {
        System.out.println(max(3, 9));      // expected: 9
        System.out.println(max(-2, -7));    // expected: -2
        System.out.println(max(3.5, 2.9));  // expected: 3.5
        System.out.println(max(2, 2.0));    // should call double version → 2.0
    }
}

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);          // int:42
        print("hello");     // str:hello
        print('A' + "!");   // str:A!
    }
}

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

Homework Hacks

Short Answer

int sum(int a, int b) and double sum(int a, int b) cannot both exist because they have the same method name and parameter list, and changing only the return type does not make a method different.

Parameters are the variables listed in a method’s definition, while arguments are the actual values you pass into the method when you call it.

Coding Tasks (1, 2, and 3)

// Task 1
int abs(int x) {
    return x < 0 ? -x : x;
}

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

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

// Task 2
public class ConcatExample {
    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;
    }

    public static void main(String[] args) {
        System.out.println(concat("Hello", "World")); // HelloWorld
        System.out.println(concat("Hi", 3));          // HiHiHi
    }
}
ConcatExample.main(null);
HelloWorld
HiHiHi
// Task 3
public class ShowExample {
    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);    // int
        show(7L);   // long
        show(7.0);  // double
    }
}
ShowExample.main(null);
int
long
double

FRQ Style Questions

// Question 1

public class IndexOfExample {

    // Assumptions & Constraints:
    // - s and target are non-null.
    // - Case-sensitive search.
    // - No use of built-in indexOf() or similar library methods.
    // - Returns -1 if target or substring is 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;
    }

    static int indexOf(String target, String s) {
        if (target.length() == 0 || target.length() > s.length()) {
            return -1;
        }

        for (int i = 0; i <= s.length() - target.length(); i++) {
            boolean match = true;
            for (int j = 0; j < target.length(); j++) {
                if (s.charAt(i + j) != target.charAt(j)) {
                    match = false;
                    break;
                }
            }
            if (match) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        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("apple", "banana")); // -1
    }
}
IndexOfExample.main(null);
1
-1
1
-1
// Question 2

public class ClampExample {

    // Assumptions & Constraints:
    // - Works for any int or double inputs.
    // - If low > high, values are swapped so the range is valid.

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

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

    public static void main(String[] args) {
        System.out.println(clamp(5, 1, 10));      // 5
        System.out.println(clamp(-3, 0, 7));      // 0
        System.out.println(clamp(12, 0, 10));     // 10
        System.out.println(clamp(8.5, 10.0, 5.0)); // 8.5 (swaps low/high)
    }
}
ClampExample.main(null);
5
0
10
8.5