Shape.java
public abstract class Shape {
public abstract
double area();
public abstract
double perimeter();
}
Rectangle.java
public class Rectangle extends Shape {
private final
double width, length; //sides
public Rectangle()
{
this(1,1);
}
public
Rectangle(double width, double length) {
this.width =
width;
this.length =
length;
}
@Override
public double
area() {
// A = w * l
return width *
length;
}
@Override
public double
perimeter() {
// P = 2(w +
l)
return 2 *
(width + length);
}
public String toString(){
return ("Rectangle width: " + width + " and
length: " + length
+
"\nResulting area: " + area()
+
"\nResulting perimeter: " + perimeter() + "\n");
}
}
Circle.java
public class Circle extends Shape {
private final
double radius;
final double pi =
Math.PI;
public Circle() {
this(1);
}
public
Circle(double radius) {
this.radius =
radius;
}
@Override
public double
area() {
// A = π r^2
return pi *
Math.pow(radius, 2);
}
public double
perimeter() {
// P = 2πr
return 2 * pi
* radius;
}
public String toString(){
return ("Circle radius: " + radius
+ "\nResulting Area: " + area()
+ "\nResulting Perimeter: " + perimeter() +
"\n");
}
}
Triangle.java
public class Triangle extends Shape {
private final
double a, b, c; // sides
public Triangle()
{
this(1,1,1);
}
public
Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double
area() {
// Heron's
formula:
// A =
SquareRoot(s * (s - a) * (s - b) * (s - c))
// where s =
(a + b + c) / 2, or 1/2 of the perimeter of the triangle
double s = (a
+ b + c) / 2;
return Math.sqrt(s
* (s - a) * (s - b) * (s - c));
}
@Override
public double
perimeter() {
// P = a + b +
c
return a + b +
c;
}
}
Cylinder.java
public class Cylinder extends Circle {
// private instance
variable
private final
double height;
// Constructors
public Cylinder() {
super(); // invoke superclass' constructor Circle()
this.height=1;
System.out.println("Constructed a Cylinder with
Cylinder()");
}
public
Cylinder(double height) {
super(); // invoke superclass' constructor Circle()
this.height =
height;
System.out.println("Constructed a Cylinder with
Cylinder(height)");
}
public
Cylinder(double height, double radius) {
super(radius); // invoke
superclass' constructor Circle(radius)
this.height =
height;
System.out.println("Constructed a Cylinder with Cylinder(height,
radius)");
}
/** Returns the
volume of this Cylinder */
public double
getVolume() {
return
area()*height; // Use Circle's
getArea()
}
public String
toString() {
return
("Height: " + height +
"\nArea of
Circle: " + area() + "This is a Cylinder volume:" + getVolume()
+ "\n");
}
}
Cone.java
public class Cone extends Circle{
private final double height;
public Cone() {
super(); // invoke superclass' constructor Cone()
this.height=1;
System.out.println("Constructed a Cone with Cone()");
}
public Cone(double height) {
super(); // invoke superclass' constructor Cone()
this.height =
height;
System.out.println("Constructed a Cone with
Cone(height)");
}
public Cone(double height, double radius) {
super(radius); // invoke
superclass' constructor Circle(radius)
this.height =
height;
System.out.println("Constructed a Cone with Cone(height,
radius)");
}
public double
getVolume() {
return area()*height/(3*7); // Use Circle's getArea()
}
public String toString() {
return
("Height: " + height +
"\nArea of
Circle: " + area() + "This is a Cone volume:" + getVolume() +
"\n");
}
}
TestApplication.java
public class TestApplication {
public static void
main(String[] args) {
// Rectangle
test
double width = 5, length = 7;
Rectangle rectangle = new Rectangle(width, length);
System.out.println( rectangle.toString());
// Circle test
double radius = 5;
Circle circle = new Circle(radius);
System.out.println( circle.toString());
// Triangle
test
double a = 5, b = 3, c = 4;
Triangle triangle = new Triangle(a,b,c);
System.out.println("Triangle sides lengths: " + a
+ ", " + b + ", " + c
+ "\nResulting Area: " + triangle.area()
+ "\nResulting Perimeter: " + triangle.perimeter()
+ "\n");
// Cylinder test
double height = 5;
Cylinder cylinder = new Cylinder(height,radius);
System.out.println( cylinder.toString());
// Cone test
Cone cone = new Cone(height,radius);
System.out.println( cone.toString());
}
}
Output
0 Comments