//Lab 10//// public abstract class GeometricObject { public abstract double getA
ID: 3687269 • Letter: #
Question
//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 toString() 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. Part 3) 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
Answer for Question:
1. Created circle class with extends the GeometricObject with getArea and getPerimeter methods.
2. Created reactangle class with extends the GeometricObject with getArea and getPerimeter methods.
1. Created eqilateraltriangle class with extends the GeometricObject with getArea and getPerimeter methods.
class abstract class GeometricObject
{
public abstract double getArea();
public abstract double getPerimeter();
public String toString()
{
return "GeometricObject : Area = ?, Perimeter = ?";
}
}
// circle is extends the GeometricObject
// with getArea and getPerimeter methods overridden
class circle extends GeometricObject
{
public double radius ;
circle(double radius)
{
this.radius = radius;
}
public double getArea()
{
return (r * r * 3.14159);
}
public double getPerimeter()
{
return (2 * 3.14159 * radius);
}
public String toString()
{
return "Circle Object :Area = "+getArea()+" .Perimeter =+"+getPerimeter();
}
}
// Rectangle is extends the GeometricObject
// with getArea and getPerimeter methods overridden
class Rectangle extends GeometricObject
{
public double length;
public double width;
Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
public double getArea()
{
return (width * length);
}
public double getPerimeter()
{
return ( 2 *( length + width));
}
public String toString()
{
return "Rectangle Object :Area = "+getArea()+" .Perimeter =+"+getPerimeter();
}
}
// EqilateralTriangle is extends the GeometricObject
// with getArea and getPerimeter methods overidden
class EqilateralTriangle extends GeometricObject
{
public double side ;
EqilateralTriangle(double side)
{
this.side = side;
}
public double getArea()
{
return ((1/2) * side * side );
}
public double getPerimeter()
{
return (side + side + side);
}
public String toString()
{
return "EqilateralTriangle Object :Area = "+getArea()+" .Perimeter =+"+getPerimeter();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.