1.9 Lesson Homework
Lesson 1.9 Homework
Popcorn Hack 1
public class PopcornMax {
// int version of max
public static int max(int a, int b) {
return (a > b) ? a : b;
}
// double version of max
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));
System.out.println(max(-2, -7));
System.out.println(max(3.5, 2.9));
System.out.println(max(2, 2.0));
}
}
PopcornMax.main(null);
9
-2
3.5
2.0
Popcorn Hack 2
public class PopcornPrint {
// int version of print
public static void print(int n) {
System.out.println("int:" + n);
}
// String version of print
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!
}
}
PopcornPrint.main(null);
int:42
str:hello
str:A!
Homework
Short answer
- Explain why int sum(int a, int b) and double sum(int a, int b) cannot both exist.
- In one sentence, distinguish parameters vs arguments.
Answer
- They can’t both exist because they have identical parameters, and Java doesn’t allow overloading by return type alone.
- Parameters are method input variables, while arguments are the actual values passed to them.
Coding Tasks (write Java in code blocks; pseudo-Java acceptable)
Overloads of abs
// Absolute value overloads
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;
}
System.out.println("abs(-5) = " + abs(-5)); // 5
System.out.println("abs(-3.14) = " + abs(-3.14)); // 3.14
System.out.println("abs(-100L) = " + abs(-100L)); // 100
abs(-5) = 5
abs(-3.14) = 3.14
abs(-100L) = 100
Concat overloads
// Concatenation overloads
String concat(String a, String b) {
return a + b;
}
String concat(String a, int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(a);
}
return sb.toString();
}
System.out.println(concat("Hello", "World"));
System.out.println(concat("Hi", 3));
HelloWorld
HiHiHi
Show overloads
// Show method overloads
void show(int x) {
System.out.println("int");
}
void show(double x) {
System.out.println("double");
}
void show(long x) {
System.out.println("long");
}
show(7);
show(7L);
show(7.0);
int
long
double
FRQ-style
FRQ 1
public class Frq1_IndexOf {
// Assumptions & constraints:
// - s and target are non-null
// - For char version, return first occurrence or -1 if not found
// - For String version, return index of first occurrence of substring or -1
// - Do not use built-in String.indexOf for substring
// Find first occurrence of a 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;
}
// Find first occurrence of a substring
public static int indexOf(String target, String s) {
if (target.length() == 0) return 0; // empty string at start
for (int i = 0; i <= s.length() - target.length(); i++) {
boolean found = true;
for (int j = 0; j < target.length(); j++) {
if (s.charAt(i + j) != target.charAt(j)) {
found = false;
break;
}
}
if (found) return i;
}
return -1;
}
public static void main(String[] args) {
System.out.println(indexOf('a', "banana")); // expected: 1
System.out.println(indexOf('z', "banana")); // expected: -1
System.out.println(indexOf("ana", "banana")); // expected: 1
System.out.println(indexOf("xyz", "banana")); // expected: -1
}
}
Frq1_IndexOf.main(null);
1
-1
1
-1
FRQ 2
public class Frq2_Clamp {
// Clamp int value to [low, high], swap if low > high
public 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;
}
// Clamp double value to [low, high], swap if low > high
public 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;
}
public static void main(String[] args) {
System.out.println(clamp(5, 1, 10)); // expected: 5
System.out.println(clamp(-2, 0, 10)); // expected: 0
System.out.println(clamp(15, 0, 10)); // expected: 10
System.out.println(clamp(5.5, 1.0, 5.0)); // swap → expected: 5.0
System.out.println(clamp(3.2, 5.0, 1.0)); // swap → expected: 3.2
}
}
Frq2_Clamp.main(null);
5
0
10
5.0
3.2