Untitled
unknown
java
3 years ago
3.4 kB
6
Indexable
public class Quadrilateral extends Shape {
XYCoord b, c, d;
public Quadrilateral(XYCoord a, XYCoord b, XYCoord c, XYCoord d) {
super(a);
this.b = b;
this.c = c;
this.d = d;
}
public double sideLength(XYCoord p1, XYCoord p2) {
return p1.distance(p2);
}
public double perimeter() {
return sideLength(a, b) + sideLength(b, c) + sideLength(c, d) + sideLength(d, a);
}
public double area() {
double s1 = (sideLength(a, b) + sideLength(b, c) + sideLength(c, a)) / 2;
double s2 = (sideLength(a, d) + sideLength(d, c) + sideLength(c, a)) / 2;
return Math.sqrt(s1 * (s1 - sideLength(a, b)) * (s1 - sideLength(b, c)) * (s1 - sideLength(c, a))) +
Math.sqrt(s2 * (s2 - sideLength(a, d)) * (s2 - sideLength(d, c)) * (s2 - sideLength(c, a)));
}
}
public class Square extends Quadrilateral {
private double length;
public Square(XYCoord anchor, double length) {
super(anchor, new XYCoord(anchor.getX() + length, anchor.getY()),
new XYCoord(anchor.getX() + length, anchor.getY() + length),
new XYCoord(anchor.getX(), anchor.getY() + length));
this.length = length;
}
public void setLength(double newLength) {
this.length = newLength;
XYCoord b = new XYCoord(this.a.getX() + newLength, this.a.getY());
XYCoord c = new XYCoord(this.a.getX() + newLength, this.a.getY() + newLength);
XYCoord d = new XYCoord(this.a.getX(), this.a.getY() + newLength);
this.b = b;
this.c = c;
this.d = d;
}
public double getLength() {
return this.length;
}
public double area() {
return length * length;
}
public double perimeter() {
return 4 * length;
}
}
public class Circle extends Shape {
private double radius;
public Circle(XYCoord center, double radius) {
super(center);
this.radius = radius;
}
/** Returns the radius of this circle. */
public double getRadius() {
return this.radius;
}
/** Changes the radius of this circle. */
public void setRadius(double newRadius) {
this.radius = newRadius;
}
/** Computes and returns the area of this circle. */
public double area() {
return Math.PI * this.radius * this.radius;
}
/** Computes and returns the perimeter of this circle. */
public double perimeter() {
return 2 * Math.PI * this.radius;
}
@Override
public String toString() {
return String.format("Circle with center at %s and radius %.3f", this.getAnchor(), this.radius);
}
}
public class Triangle extends Shape {
private XYCoord b, c;
public Triangle(XYCoord a, XYCoord b, XYCoord c) {
super(a);
this.b = b;
this.c = c;
}
public XYCoord getB() {
return b;
}
public XYCoord getC() {
return c;
}
public void setBC(XYCoord newB, XYCoord newC) {
this.b = newB;
this.c = newC;
}
@Override
public double area() {
double ab = a.distance(b);
double ac = a.distance(c);
double bc = b.distance(c);
double s = (ab + ac + bc) / 2.0;
return Math.sqrt(s * (s - ab) * (s - ac) * (s - bc));
}
@Override
public double perimeter() {
return a.distance(b) + a.distance(c) + b.distance(c);
}
}
Editor is loading...