record Point(int x, int y) {}
interface IShape {
double PI = 3.1415;
double area();
double perimeter();
}
abstract class Shape implements IShape {
protected final Point[] points;
Shape(int edges) {
this.points = new Point[edges];
}
Shape(Point... points) {
this.points = points;
}
public int getEdges() {
return this.points.length;
}
}
final class Circle extends Shape implements Comparable {
private final int radius;
Circle() {
super(1);
this.radius = 1;
}
Circle(Point center, int radius) {
super(center);
this.radius = radius;
}
public int getRadius() {
return radius;
}
public Point getCenter() {
return points[0];
}
@Override
public double area() {
return Math.PI * (radius * radius);
}
@Override
public double perimeter() {
return Math.PI * 2*radius;
}
@Override
public int compareTo(Object o) {
if (!(o instanceof Circle c)) {
throw new ClassCastException("must be an instance of Circle");
}
if (getRadius() > c.getRadius()) {
return 1;
} else if (getRadius() < c.getRadius()) {
return -1;
} else {
return 0;
}
}
}
class Rectangle extends Shape {
private int width, height;
Rectangle(int width, int height) {
super(1);
this.width = width;
this.height = height;
}
Rectangle(Point upperLeft, int width, int height) {
super(upperLeft);
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
@Override
public double area() {
return width * height;
}
@Override
public double perimeter() {
return 2*width + 2*height;
}
}
class Triangle extends Shape {
Triangle(Point... points) {
super(points);
}
public Point[] getPoints() {
return points;
}
// see https://tinyurl.com/msfdtjme
private double length(Point p1, Point p2) {
return Math.sqrt((p2.x() - p1.x()) * (p2.x() - p1.x()) + (p2.y() - p1.y()) * (p2.y() - p1.y()));
}
// see https://www.ypologismos.gr/emvadon-trigonou-xerontas-3-pleyres-typos-hrona/
@Override
public double area() {
double s = perimeter()/2;
double a = length(points[0], points[1]);
double b = length(points[1], points[2]);
double c = length(points[2], points[0]);
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
// https://www.ypologismos.gr/emvadon-trigonou-xerontas-3-pleyres-typos-hrona/
@Override
public double perimeter() {
return length(points[0], points[1]) + length(points[1], points[2]) + length(points[2], points[0]);
}
}
public class Main {
public static void main(String[] args) {
Shape c = new Circle(new Point(10, 10), 10);
System.out.println(c.perimeter());
System.out.println(c.area());
Shape r = new Rectangle(new Point(0, 0), 10, 10);
System.out.println(r.perimeter());
System.out.println(r.area());
Shape t = new Triangle(new Point(0, 0), new Point(10, 10), new Point(-10, -10));
System.out.println(t.perimeter());
System.out.println(t.area());
}
}