Create a new class called Circle that extends Shape and implement the following
ID: 3843581 • Letter: C
Question
Create a new class called Circle that extends Shape and implement the following methods.
protected Circle(Point aCenter, double radius)
This constructor calls the superclass’s constructor and passes it the name "Circle". It then passes an array containing only the point aCenter to the setPoints method. Additionally, it stores radius in a private field. However, if radius is negative it will store 0.0 as the radius.
public double getRadius()
This returns the radius of the Circle.
Override the following methods.
public double getPerimeter()
This method calculates the perimeter of the Circle. The perimeter of a circle is given by the expression
2r
where r is the radius of the circle.
public double getArea()
This method calculates the area of the Circle. The area of a circle is given by the expression
r2
Exercise 4 Complete
where r is the radius of the circle.
here is my Shape code
import java.awt.Point;
public abstract class Shape {
private String name;
private Point[] points;
protected Shape(String aName) {
this.name = aName;
}
public final String getName() {
return this.name;
}
protected final void setPoints(Point[] thePoints) {
this.points = thePoints;
}
public final Point[] getPoints() {
return this.points;
}
public static double getDistance(Point p1, Point p2) {
double x1 = p1.getX();
double x2 = p2.getX();
double y1 = p1.getY();
double y2 = p2.getY();
double add1 = x1 - x2;
double sqr1 = add1 * add1;
double add2 = y1 - y2;
double sqr2 = add2 * add2;
double sqrt = sqr2 + sqr1;
return Math.sqrt(sqrt);
}
abstract double getPerimeter();// Just Remove Method body
}
Explanation / Answer
//Circle class extends Shape class and implements getPerimeter and getArea methods
class Circle extends Shape
{
private double radius; //private member of the class
protected Circle(Point []aCenter, double radius)
{
super("Circle"); // Calls Shape class constructor
super.setPoints(aCenter); //calls the method setPoints
if(radius<0) //if radius value is negative
{
this.radius = 0.0;
}
else
{
this.radius = radius;
}
}
public double getRadius() //return radius of the circle
{
return radius;
}
public double getPerimeter() //calculates and returns the perimeter of the circle
{
double perimeter = 2* 3.14 * radius;
return perimeter;
}
public double getArea() //calculates and returns the area of the circle
{
double area = 3.14 * radius * radius;
return area;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.