Skip to the content.

Lesson 1.9 - Rohan Bojja

public class PopcornMax {
    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
    }
}
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);          // expected: int:42
        print("hello");     // expected: str:hello
        print('A' + "!");   // char + String → String → expected: str:A!
    }
}

//Short Answer #1

//Because their parameter lists are identical, and return type alone doesn’t distinguish overloaded methods — the compiler can’t tell which to call.

//Short Answer #2

// Parameters are variables in the method definition, while arguments are the actual values passed to the method when it’s called.
// Coding Tasl #1

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

public class ConcatExample {
    public static String concat(String a, String b) {
        return a + b;
    }

    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("Yo", 3));        // YoYoYo
    }
}

// Coding Task #3

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

show(7);
show(7L);
show(7.0);

int
long
double
// FRQ #1

public class StringFinder {
    // index of a single character
    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;
    }

    // index of a substring
    public static int indexOf(String target, String s) {
        int tLen = target.length();
        for (int i = 0; i <= s.length() - tLen; i++) {
            if (s.substring(i, i + tLen).equals(target)) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(indexOf('a', "banana"));   // 1
        System.out.println(indexOf("ana", "banana")); // 1
    }
}
// FRQ #2

public class ClampExample {
    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;
    }

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

    public static void main(String[] args) {
        System.out.println(clamp(15, 0, 10));     // 10
        System.out.println(clamp(5.5, 0, 10));    // 5.5
        System.out.println(clamp(5, 10, 0));      // 5 (swaps bounds)
    }
}