java
Consider the following class definition:
public class Circle { private double radius; public Circle (double r) {
radius = r ; }
public double getArea(){ return Math.PI * radius * radius;
} public double getRadius(){
return radius; }
}
a) Write a toString method for this class. The method should return a string containing the radius and area of the circle; For example “The area of a circle with radius 2.0 is 12.1.”
b) Writeaequalsmethodforthisclass.ThemethodshouldacceptaCircleobjectasan argument. It should return true if the argument object contains the same data as the calling object, or false otherwise.
c) WriteagreaterThanmethodforthisclass.ThemethodshouldacceptaCircleobject as an argument. It should return true if the argument object has an area that is greater than the area of the calling object, or false otherwise.
public class Circle { private double radius; public Circle(double r) { radius = r; } public double getArea() { return Math.PI * radius * radius; } public double getRadius() { return radius; } // a) @Override public String toString() { return "The area of a circle with radius " + radius + " is " + getArea() + "."; } // b) @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Circle circle = (Circle) o; return Double.compare(circle.radius, radius) == 0; } // c) public boolean greaterThan(Circle circle) { return getArea() > circle.getArea(); } }
Get Answers For Free
Most questions answered within 1 hours.