In: Computer Science
Shapes2D
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 displayArea() 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.
Also the code should Enforce an abstract get2DArea() method and the right parameter type for displayArea()
Executes the following code:
Shape2DDriver tester = new Shape2DDriver();
Rectangle2D r = new Rectangle2D( 2, 3 );
Circle2D c = new Circle2D( 4 );
tester.displayArea( r );
tester.displayArea( c );
I have implemented the Shape2D, Rectange2D, Circle2D, and Shape2DDriver Classes to demostrate polymorphism as per the given description.
Please find the following Code Screenshot, Output, and Code.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT :
2.OUTPUT :
3.CODE :
Shape2D.java
public abstract class Shape2D {
//include just an abstract method name get2DArea() that returns a double.
public abstract double get2DArea();
}
Rectangle2D.java
public class Rectangle2D extends Shape2D {
private double length,width;
/*
Provide a constructor that takes two double arguments
and uses them to set the fields
*/
public Rectangle2D(double length, double width) {
this.length = length;
this.width = width;
}
@Override
/*calculate and return the area of rectangle*/
public double get2DArea() {
return length*width;
}
}
Circle2D.java
public class Circle2D extends Shape2D {
// it store a radius as a field.
private double radius;
//constructor that takes a double argument and uses it to set the field.
public Circle2D(double radius) {
this.radius = radius;
}
@Override
public double get2DArea() {
//calculate and return area of the circle
return Math.PI*radius*radius;
}
}
Shape2DDriver.java
public class Shape2DDriver {
/*By the pricipal of polymorphism we can store the subclass
objects in parent class reference in the following signature */
public void displayArea(Shape2D s){
//display the name of the class and area
System.out.printf("The area of %s is %.1f\n",s.getClass(),s.get2DArea());
}
public static void main(String[] args) {
//create the object of the driver code
Shape2DDriver tester = new Shape2DDriver();
//create the obejcts of the Circle and Rectangle
Rectangle2D r = new Rectangle2D( 2, 3 );
Circle2D c = new Circle2D( 4 );
//display the result
tester.displayArea( r );
tester.displayArea( c );
}
}