In: Computer Science
Shapes2D Assignment
Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes.
Shape2D class
For this class, include just an abstract method name get2DArea() that returns a double.
Rectangle2D class
Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the width.
Circle2D class
Also make this class inherit from the Shape2D class. Have it store a radius as a field. Provide a constructor that takes a double argument and uses it to set the field. Note, the area of a circle is PI times it's radius times it's radius.
Shape2DDriver class
Have this class provide a method named displayName() that takes an object from just any of the above three classes (you can't use an Object type parameter). Have the method display the area of the object, rounded to one decimal place.
Note- please give me a thumbs up if this answer is helpful. Thank you.
Step 1
Objective: This program is designed with four classes, i.e. one abstract class along with three derive classes. All these shape classes will be used here to determine the area of the shapes Rectangle and Circle using the proper formula.
Programming Language: Java
Step 2
Shape2D.java:
Computer Science homework question answer, step 2, image 1
abstract class Shape2D
{
public abstract double get2DArea();
}
Step 3
Rectangle2D.java:
Computer Science homework question answer, step 3, image 1
class Rectangle2D extends Shape2D
{
protected double lengthOfRect;
protected double widthOfRect;
Rectangle2D(double lengthOfRect,double widthOfRect)
{
this.lengthOfRect=lengthOfRect;
this.widthOfRect=widthOfRect;
}
public double get2DArea()
{
return lengthOfRect * widthOfRect;
}
}
Step 4
Circle2D
class Circle2D extends Shape2D
{
protected double lengthOfRadius;
final double PI_const = 3.1415;
Circle2D(double lengthOfRadius)
{
this.lengthOfRadius=lengthOfRadius;
}
public double get2DArea()
{
return PI_const * lengthOfRadius * lengthOfRadius;
}
}
Step 5
Shape2DDriver.java:
Output