Unit 1.9 - Method Overloading
Categories: homeworkJava homework assignment covering method overloading, parameter types, and method signatures
// Popcorn hack 1, max overloads
public class PopcornMax {
public static int max(int a, int b) {
return a >= b ? a : b;
}
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));
}
}
Expected Output:
9
-2
3.5
2.0
// Popcorn hack2, overload print for int and String
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);
print("hello");
print('A' + "!");
}
}
Expected Output:
int:42
str:hello
str:A!
// CT1, abs overloads
public class AbsOverloads {
public static int abs(int x) {
return x < 0 ? -x : x;
}
public static long abs(long x) {
return x < 0 ? -x : x;
}
public static double abs(double x) {
return x < 0.0 ? -x : x;
}
public static void main(String[] args) {
System.out.println(abs(-7));
System.out.println(abs(-7L));
System.out.println(abs(-7.5));
}
}
Expected Output:
7
7
7.5
// CT2, concat overloads
public class ConcatOverloads {
public static String concat(String a, String b) {
return a + b;
}
public static String concat(String a, int n) {
if (n <= 0) return "";
StringBuilder sb = new StringBuilder(a.length() * n);
for (int i = 0; i < n; i++) sb.append(a);
return sb.toString();
}
public static void main(String[] args) {
System.out.println(concat("cs", "a"));
System.out.println(concat("ha", 3));
System.out.println(concat("x", 0));
}
}
Expected Output:
csa
hahaha
// CT3, show method outputs
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);
Expected Output:
int
long
double
//FRQ 1, indexOf overloads (char and String)
public class MyIndexOf {
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) {
int n = s.length();
int m = target.length();
if (m == 0) return 0;
if (m > n) return -1;
for (int start = 0; start <= n - m; start++) {
int j = 0;
while (j < m && s.charAt(start + j) == target.charAt(j)) j++;
if (j == m) return start;
}
return -1;
}
public static void main(String[] args) {
System.out.println(indexOf('a', "banana"));
System.out.println(indexOf("ana", "banana"));
System.out.println(indexOf("xyz", "banana"));
System.out.println(indexOf("", "banana"));
}
}
Expected Output:
1
1
-1
0
// FRQ 2, clamp overloads for int and double
public class Clamp {
public static int clamp(int value, int low, int high) {
if (low > high) { int tmp = low; low = high; high = tmp; }
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 tmp = low; low = high; high = tmp; }
if (value < low) return low;
if (value > high) return high;
return value;
}
public static void main(String[] args) {
System.out.println(clamp(5, 0, 10));
System.out.println(clamp(-3, 0, 10));
System.out.println(clamp(12, 0, 10));
System.out.println(clamp(5.5, 7.0, 2.0));
}
}
Expected Output:
5
0
10
5.5