In: Computer Science
Below is the code for all three classes implementing the Shape interface.
Note: I have put all the classes including Point2d in the package named "shapes".
1. Shape.java
package shapes;
interface Shape {
double area();
double perimeter();
Point2d center();
}
2. Rectangle.java
package shapes;
public class Rectangle implements Shape {
private double length;
private double breadth;
private Point2d p1;
private Point2d p2;
public Rectangle(Point2d p1, Point2d p2) {
this.p1 = p1;
this.p2 = p2;
this.length = this.p2.getX() - this.p1.getX();
this.breadth = this.p2.getY() - this.p1.getY();
}
public double area() {
return getLength() * getBreadth();
}
public double perimeter() {
return 2 * (getLength() + getBreadth());
}
public Point2d center() {
double xCenter = (this.p1.getX() + this.p2.getX()) / 2;
double yCenter = (this.p1.getY() + this.p2.getY()) / 2;
return new Point2d(xCenter, yCenter);
}
public double getLength() {
return length;
}
public double getBreadth() {
return breadth;
}
public void setLength(double length) {
this.length = length;
}
public void setBreadth(double breadth) {
this.breadth = breadth;
}
}
3. Circle.java
package shapes;
public class Circle implements Shape {
private Point2d center;
private double radius;
public Circle(Point2d c, double r) {
this.center = c;
this.radius = r;
}
public double area() {
return Math.PI * getRadius() * getRadius();
}
public double perimeter() {
return 2 * Math.PI * getRadius();
}
public Point2d center() {
return center;
}
public Point2d getCenter() {
return center;
}
public double getRadius() {
return radius;
}
public void setCenter(Point2d center) {
this.center = center;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
4. Polygon.java
package shapes;
public class Polygon implements Shape {
private Point2d[] points;
public Polygon(Point2d[] pts) {
this.points = new Point2d[pts.length];
System.arraycopy(pts, 0, this.points, 0, pts.length); // this method copies the pts array into the points field
// of the class
}
public double area() {
/*
* I have used the Shoelace Formula to calculate the area of the polygon
*/
double xCoordinates[] = new double[points.length];
double yCoordinates[] = new double[points.length];
double area = 0.0;
int j = points.length - 1;
for (int i = 0; i < points.length; i++) {
area += (xCoordinates[j] + xCoordinates[i]) * (yCoordinates[j] - yCoordinates[i]);
j = i;
}
return Math.abs(area / 2.0);
}
public double perimeter() {
double perimeter = 0.0;
for (Point2d pt : points) {
perimeter += pt.distance(pt);
}
return perimeter;
}
public Point2d center() {
double xAvg = 0.0, yAvg = 0.0;
for (Point2d pt : points) {
xAvg += pt.getX();
yAvg += pt.getY();
}
double xCenter = xAvg / points.length;
double yCenter = yAvg / points.length;
return new Point2d(xCenter, yCenter);
}
}
NOTE: It would be better if you could provide the test cases for
the program. If you can please provide the test cases in the
comment to this answer and I will update the answer by running your
test cases.
Try to run this program against your test cases and if there is any
problem, I will update the answer accordingly.