Shape Circle Rectangle Square Figure 1 Part 1: Implement the classes in Figure 1
ID: 3683192 • Letter: S
Question
Shape Circle Rectangle Square Figure 1 Part 1: Implement the classes in Figure 1. 1. Abstract class Shape has a string name and two abstract methods area() and perimeter); 2. A concrete class Circle that extends Shape with a data member radius, and non erO and tostringO. abstract methods area O, perimet 3. A concrete class Rectangle that extends Shape with a data member length and width, and non abstract methods areaO, perimeter) and tostringO 4. A concrete class Square that extends Rectangle. A square is a special case of Rectangle with the two sides equal. Square.java public class square extends Rectanglet public Square (double length) super(length, length); 5. Write the TestShapes.java: create an ArrayList of of type Shape and assign different shapes to it. Call functions areaO, perimeter) andExplanation / Answer
This question has multiple sub-parts. I have answered the first 5. Please post one more question for the same.
I have written the java codes for the same:
shape.java:
public abstract class Shape {
String name;
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() {
return width * length;
}
@Override
public double perimeter() {
return 2 * (width + length);
}
public void toString()
{
name="Rectangle";
System.out.println("Name is "+ name);
}
}
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() {
return pi * Math.pow(radius, 2);
}
public double perimeter() {
return 2 * pi * radius;
}
public void toString()
{
name="Circle";
System.out.println("Name is "+ name);
}
}
testshape.java
public class TestShape {
public static void main(String[] args) {
double width = 5, length = 7;
Shape rectangle = new Rectangle(width, length);
System.out.println("Rectangle width: " + width + " and length: " + length
+ " Resulting area: " + rectangle.area()
+ " Resulting perimeter: " + rectangle.perimeter() + " ");
double radius = 5;
Shape circle = new Circle(radius);
System.out.println("Circle radius: " + radius
+ " Resulting Area: " + circle.area()
+ " Resulting Perimeter: " + circle.perimeter() + " ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.