Create a new class called Quadrilateral that extends Shape and implement the fol
ID: 3843247 • Letter: C
Question
Create a new class called Quadrilateral that extends Shape and implement the following constructor.
• protected Quadrilateral(Point[] aPoints)
This constructor calls the superclass’s constructor and passes it the name "Quadrilateral". It then passes a copy of the first four entries in the aPoints array to the setPoints method.
Override the following methods.
4
1. public double getPerimeter()
This method calculates the perimeter of the Quadrilateral. The perimeter of a quadrilateral is the sum of the lengths its sides (to get the length of a side, calculate the distance between the two of its vertices).
2. public double getArea()
This method calculates the area of the Quadrilateral. If the vertices of a quadrilateral are A = (ax,ay), B = (bx,by), C = (cx,cy), and D = (dx,dy), then the area of the quadrilateral is given by the expression
[(axby aybx)+(bxcy bycx)+(cxdy cydx)+(dxay dyax)] / 2
Here is my code for shape
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
Quadrilateral Class
import java.awt.Point;
public class Quadrilateral extends Shape{
private Point[] points;
protected Quadrilateral(Point[] aPoints)
{
super("Quadrilateral");
for(int i=0;i<4;i++)
{
points[i]=aPoints[i];
}
this.setPoints(points);
}
@Override
double getPerimeter() {
double perimeter=0;
Point[] points=this.getPoints();
perimeter+=getDistance(points[0],points[1]);
perimeter+=getDistance(points[1],points[2]);
perimeter+=getDistance(points[2],points[3]);
perimeter+=getDistance(points[3],points[0]);
return perimeter;
}
public double getArea()
{
Point[] points=this.getPoints();
double area=0;
Point A=points[0];
Point B=points[1];
Point C=points[2];
Point D=points[3];
area=((A.getX()*B.getY()-A.getY()*B.getX())+(B.getX()*C.getY()-B.getY()*C.getX())+(C.getX()*D.getY()-C.getY()*D.getX())+(D.getX()*A.getY()-D.getY()*A.getX()))/2;
return area;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.