Casting HW
For the applicators group. 1.9.
public class PopcornMax {
// int overload
public static int max(int a, int b) {
return a >= b ? a : b;
}
// double overload
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)); // 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)); // expected: 2.0
}
}
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!
}
}
public class AbsOverloads {
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;
}
public static void main(String[] args) {
System.out.println(abs(-5)); // 5
System.out.println(abs(-3.2)); // 3.2
System.out.println(abs(-100000L));// 100000
}
}
public class ConcatOverloads {
// Concatenate two strings
public static String concat(String a, String b) {
return a + b;
}
// Repeat string n times
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("Hello", "World")); // HelloWorld
System.out.println(concat("Hi", 3)); // HiHiHi
}
}
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);
show(7L);
show(7.0);
}
/**
* Finds the index of the first occurrence of a target character in a string.
* Returns -1 if not found.
*
* @param target the character to find
* @param s the string to search
* @return the index of the first occurrence or -1 if not found
*/
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;
}
/**
* Finds the index of the first occurrence of a substring in another string.
* Returns -1 if not found. Does not use built-in indexOf.
*
* @param target the substring to find
* @param s the string to search within
* @return the starting index of the first match, or -1 if not found
*/
public static int indexOf(String target, String s) {
for (int i = 0; i <= s.length() - target.length(); i++) {
if (s.substring(i, i + target.length()).equals(target)) {
return i;
}
}
return -1;
}
/**
* Constrains an int value within a given range.
* If low > high, swaps the bounds automatically.
*/
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;
}
/**
* Constrains a double value within a given range.
* If low > high, swaps the bounds automatically.
*/
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)); // → 5
System.out.println(clamp(-2, 0, 8)); // → 0
System.out.println(clamp(12.5, 0.0, 10)); // → 10.0
System.out.println(clamp(4, 10, 1)); // → 4 (bounds swapped)