Follow the directions below //Lab 10//// public abstract class GeometricObject {
ID: 3687259 • Letter: F
Question
Follow the directions below
//Lab 10//// public abstract class GeometricObject { public abstract double getArea(); public abstract double getPerimeter(); public String toString() { return "GeometricObject: Area=?, Perimeter=?";}} Part 2) Write a subclass of GeometricObject called Circle. Save it in the file Circle.java. Include a member variable for the circle's radius. Include a constructor that takes the radius of the circle as a parameter. Override the getArea(), getPerimeter() and toStringO methods. The toString() method should print out the circle's radius, and the area and perimeter of the circle. Include a block comment at the top of your class (like the one for GeometriObject above) that gives your name and section number. Write another subclass of GeometricObject called Rectangle. Save it in the file Rectangle.java. Include member variables for the width and height of the rectangle. Make a constructor that takes the width and height as parameters. Finally, override the methods from GeometricObject, similar to what you did with Circle. Include an appropriate block comment at the top of your class. Part 4) Write a third subclass of GeometricObject called EquilateralTriangle. Save it in the file EquilateralTriangle.java. Include a member variable for the side length of the triangle, and a constructor that takes the side length as a parameter. Override the methods from GeometricObject. Note that you may wish to use Heron's formula to compute the area of the triangle: http://en.wikipedia.org/wiki/Heron's_formula. Include a block comment at the top of your class.Explanation / Answer
Circle.java
/*
* Circle Area formula : PI * R * R
* Circle Perimeter formula: 2 * PI * R
*
* */
public class Circle extends GeometricObject{
private double radius;
private final double PI = 3.14;
public Circle(double radius){
this.radius = radius;
}
public double getArea(){
return PI * (radius * radius);
}
public double getPerimeter(){
return 2 * PI * radius;
}
public String toString(){
return String.format("Circle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
}
}
Rectangle.java
/*
* Rectangle Area formula : width * height
* Rectangle Perimeter formula: 2 * (height + width)
*
* */
public class Rectangle extends GeometricObject{
private double width;
private double height;
private final double PI = 3.14;
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
public double getArea(){
return width * height;
}
public double getPerimeter(){
return 2 * (height + width);
}
public String toString(){
return String.format("Rectangle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
}
}
EquilateralTriangle.java
/*
* EquilateralTriangle Area formula : (Math.sqrt(3) / 4) * a * a
* EquilateralTriangle Perimeter formula: 3 * a
*
* */
public class EquilateralTriangle extends GeometricObject {
private double sideLength;
private final double PI = 3.14;
public EquilateralTriangle(double sideLength){
this.sideLength = sideLength;
}
public double getArea(){
return (Math.sqrt(3) / 4) * sideLength * sideLength ;
}
public double getPerimeter(){
return 3 * sideLength;
}
public String toString(){
return String.format("EquilateralTriangle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
}
}
GeometricObject.java
/*
* GeometricObject abstract class
*
* */
public abstract class GeometricObject {
public abstract double getArea();
public abstract double getPerimeter();
public String toString(){
return "GeometricObject: Area=? , Perimeter=?";
}
}
TestObject.java
/*
* Driver class which has Main method. Run this java program for execution.
*/
public class TestObject {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle c = new Circle(2.4);
System.out.println(c.toString());
Rectangle r = new Rectangle(2.4, 5.4);
System.out.println(r.toString());
EquilateralTriangle e = new EquilateralTriangle(3.3);
System.out.println(e.toString());
}
}
Output:
Circle: Area = 18.09 , Perimeter = 15.07
Rectangle: Area = 12.96 , Perimeter = 15.60
EquilateralTriangle: Area = 4.72 , Perimeter = 9.90