Shape Circle Square Triangle Use the following as a guide: The Shape class shoul
ID: 3607494 • Letter: S
Question
Shape Circle Square Triangle Use the following as a guide: The Shape class should be abstract and contain two abstract methods: o public double getArea-that returns the area. 0 public double ge t perimeter-that returns the perimeter. and one concrete method: o public. String getName- that returns the shapes name. - The circle class's constructor should take a double representing its diamete The Square class's constructor should take a double representing the length c its sides. The Triangle class's constructor should take a double representing the lengt of its sides. . Create a test class called zest shapes that does the following: 1. Create an array of size three, that uses Shape references. Fill the array with one of each concrete class in the hierarchy. a. Display the shape's name. c. Display the shape's perimeter. 2. Loop through the array of shapes and doing the following: b. Display the shape's area Zip all the source code and submit the zip file to Blackboard. AIT S0O. Proaramimina Assianment (Michael BoehmenExplanation / Answer
Please find my implementation.
//Circle Class:
class Circle extends Shape{
private double diameter;
public Circle(String name, double diameter) {
super(name);
this.diameter = diameter;
}
@Override
public double getArea() {
return Math.PI*diameter*diameter/4;
}
@Override
public double getPerimeter() {
return Math.PI*diameter;
}
}
//Shape class:
abstract class Shape{
private String name;
abstract public double getArea();
abstract public double getPerimeter();
public Shape(String name){
this.name = name;
}
public String getName() {
return name;
}
}
//Square class:
class Square extends Shape{
private double side;
public Square(String name, double size) {
super(name);
this.side = size;
}
@Override
public double getArea() {
return side*side;
}
@Override
public double getPerimeter() {
return 4*side;
}
}
//Triangle class:
class Triangle extends Shape{
private double side1, side2, side3;
public Triangle(String name, double side1, double side2, double side3) {
super(name);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public double getArea() {
double p = getPerimeter()/2;
double area = Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
return area;
}
@Override
public double getPerimeter() {
double perimeter = side1 + side2 + side3;
return perimeter;
}
}
#########
public class ShapeTest {
public static void main(String[] args) {
Shape[] shapes = {
new Circle("Circle", 6.5),
new Triangle("Triangle", 3, 4, 5),
new Square("Square", 4)
};
for(Shape shape : shapes) {
System.out.println(shape.getName()+", "+shape.getArea()+", "+shape.getPerimeter());
}
}
}
/*
Sample run:
Circle, 33.18307240354219, 20.420352248333657
Triangle, 6.0, 12.0
Square, 16.0, 16.0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.