Complete the following Java method that returns the maximum
Complete the following Java method that returns the maximum element of an array:
public class Exercise2{
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3};
System.out.println(max(numbers));
String[] words = {\"red\", \"green\", \"blue\"};
System.out.println(max(words));
Circle[] circles = {new Circle(3), new Circle(2.9), new Circle(5.9)};
System.out.println(max(circles));
}
static class Circle implements Comparable<Circle> {
double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public int compareTo(Circle c) {
if (radius < c.radius)
return -1;
else if (radius == c.radius)
return 0;
else
return 1;
}
@Override
public String toString() {
return \"Circle radius: \" + radius;
}
}
public static <E extends Comparable<E>> E max(E[] list) {
// Your code here!
}
}
Solution
HI Friend, Please find my implementation.
Please let me know in case of any issue.
public class Exercise2{
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3};
System.out.println(max(numbers));
String[] words = {\"red\", \"green\", \"blue\"};
System.out.println(max(words));
Circle[] circles = {new Circle(3), new Circle(2.9), new Circle(5.9)};
System.out.println(max(circles));
}
static class Circle implements Comparable<Circle> {
double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public int compareTo(Circle c) {
if (radius < c.radius)
return -1;
else if (radius == c.radius)
return 0;
else
return 1;
}
@Override
public String toString() {
return \"Circle radius: \" + radius;
}
}
public static <E extends Comparable<E>> E max(E[] list) {
if(list == null)
return null;
E max = list[0]; // initializing with first element
for(int i=1; i<list.length; i++){
if(max.compareTo(list[i]) < 0) // if current element from list is greater than max
max = list[i];
}
return max;
}
}
/*
Sample run:
3
red
Circle radius: 5.9
*/
![Complete the following Java method that returns the maximum element of an array: public class Exercise2{ public static void main(String[] args) { Integer[] numb Complete the following Java method that returns the maximum element of an array: public class Exercise2{ public static void main(String[] args) { Integer[] numb](/WebImages/35/complete-the-following-java-method-that-returns-the-maximum-1104831-1761584525-0.webp)
![Complete the following Java method that returns the maximum element of an array: public class Exercise2{ public static void main(String[] args) { Integer[] numb Complete the following Java method that returns the maximum element of an array: public class Exercise2{ public static void main(String[] args) { Integer[] numb](/WebImages/35/complete-the-following-java-method-that-returns-the-maximum-1104831-1761584525-1.webp)
![Complete the following Java method that returns the maximum element of an array: public class Exercise2{ public static void main(String[] args) { Integer[] numb Complete the following Java method that returns the maximum element of an array: public class Exercise2{ public static void main(String[] args) { Integer[] numb](/WebImages/35/complete-the-following-java-method-that-returns-the-maximum-1104831-1761584525-2.webp)