MIT-Curricular/OOP/Java/Lab/Week12/LargestOfThree.java

31 lines
696 B
Java
Raw Normal View History

2024-10-26 02:14:18 +05:30
public class LargestOfThree {
public static <T extends Comparable<T>> T findLargest(T a, T b, T c) {
T max = a;
if (b.compareTo(max) > 0) {
max = b;
}
if (c.compareTo(max) > 0) {
max = c;
}
return max;
}
public static void main(String[] args) {
Integer a = 10;
Integer b = 5;
Integer c = 15;
System.out.println("The largest number is: " + findLargest(a, b, c));
String str1 = "apple";
String str2 = "orange";
String str3 = "banana";
System.out.println(
"The largest string is: " + findLargest(str1, str2, str3)
);
}
}