CSA Unit 1.9 — Assignment Statements and Input

Popcorn Hack #1

public class PopcornMax {

    // int version of max
    public static int max(int a, int b) {
        if (a > b) {
            return a;
        } else {
            return b;
        }
    }

    // double version of max
    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
    }
}

9


-2
3.5
2

Popcorn Hack #2

public class PopcornPrint {

    // method for int
    public static void print(int n) {
        System.out.println("int:" + n);
    }

    // method for String
    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!
    }
}

int:42


str:hello
str:A!

Short Answer

Q1. Why can’t int sum(int a, int b) and double sum(int a, int b) both exist?

They cannot both exist because overloading is based only on parameter lists, not return types — these two methods have identical parameter types, causing a duplicate method error.

Q2. Distinguish between parameters and arguments.

Parameters are variables defined in a method’s declaration, while arguments are the actual values passed to those parameters when the method is called.

Coding Tasks

// 1) Three overloads of abs
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;
}

System.out.println(abs(-5));     // 5
System.out.println(abs(-3.14));  // 3.14
System.out.println(abs(-123L));  // 123
// Explanation: Each abs version returns the positive value of its argument.


// 2) Two overloads of concat
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;
}

System.out.println(concat("hi", "bye")); // hibye
System.out.println(concat("ha", 3));     // hahaha
// Explanation: One concat joins two strings; the other repeats a string n times.


// 3) Determine output
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);

System.out.println("int");
System.out.println("long");
System.out.println("double");

// Explanation :  Each literal’s type (int, long, double) decides which show() runs.







5
3.14
123
hibye
hahaha
int
long
double
int
long
double

FQ1



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

public static int indexOf(String target, String s) {
    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;
}

System.out.println(indexOf('e', "hello"));    
System.out.println(indexOf('x', "hello"));    
System.out.println(indexOf("ell", "hello")); 
System.out.println(indexOf("zz", "pizza"));   

// Explanation: The first method finds the index of a single character and the second finds the index of a substring without using built-in indexOf.



1
-1
1
2

FQ2



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

System.out.println(clamp(5, 1, 10)); 
System.out.println(clamp(-2, 0, 8));     
System.out.println(clamp(12.5, 3.5, 10)); 
System.out.println(clamp(8, 10, 5));      

5
0
10.0
8