Question: Create a class called Circle that implements Comparable interface. Write a generic class with a type parameter constrained to any type that implements Comparable. The
Create a class called Circle that implements Comparable interface.
Write a generic class with a type parameter constrained to any type that implements Comparable. The constructor should accept an Array of such Objects. The class should have methods that return highest and lowest values in the array. Demonstrate the class in an application using an array of Circles.
public class Circle implements Comparable{
private double radius;
public Circle() {
radius = 0;
}
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
public double getCircumference() {
return radius * Math.PI * 2;
}
@Override
public int compareTo(Object c) {
Circle t = (Circle)c;
if(t.getRadius() > radius)
return 1;
else
return -1;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
