Question

In: Computer Science

Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...

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 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.

Solutions

Expert Solution

package abstracts;

// Defines an abstract class Shapes2D
abstract class Shapes2D
{
   // Declares an abstract method to return area of 2D shape
   abstract double get2DArea();
}// End of abstract class Shapes2D

// Defines a sub class Rectangle2D extended from super class Shapes2D
class Rectangle2D extends Shapes2D
{
   // Instance variable to store length and width
   double length;
   double width;
  
   // Parameterized constructor to assign parameter values to instance variables
   Rectangle2D(double le, double wi)
   {
       length = le;
       width = wi;
   }
  
   // Overrides method to return area of rectangle
   public double get2DArea()
   {
       return length * width;
   }
}// End of class Rectangle2D

// Defines a sub class Circle2D extended from super class Shapes2D
class Circle2D extends Shapes2D
{
   // Instance variable to store radius
   double radius;
   double width;
  
   // Parameterized constructor to assign parameter value to instance variable
   Circle2D(double ra)
   {
       radius = ra;
   }
  
   // Overrides method to return area of circle
   public double get2DArea()
   {
       return 3.141 * radius * radius;
   }
}// End of class Circle2D

// Driver class definition
public class Shape2DDriver
{
   // Defines a method to receive Shapes2D class object
   static void displayName(Shapes2D object)
   {
       // Checks if parameter object is an instance of Rectangle2D class
       if(object instanceof Rectangle2D)
           // Calls the method to calculate and return the area of rectangle
           System.out.printf("\n Rectangle Area: %.2f", object.get2DArea());          
      
       // Otherwise checks if parameter object is an instance of Circle2D class
       else if(object instanceof Circle2D)
           // Calls the method to calculate and return the area of circle
           System.out.printf("\n Circle Area: %.2f", object.get2DArea());          
   }
  
   // main method definition
   public static void main(String []s)
   {
       Shapes2D object[] = new Shapes2D[2];
       object[0] = new Circle2D(12.2);
       object[1] = new Rectangle2D(5.0, 5.0);
              
       displayName(object[0]);
       displayName(object[1]);
   }
}// End of driver class

Sample Output:


Circle Area: 467.51
Rectangle Area: 25.00


Related Solutions

Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
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...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
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...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
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...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
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...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
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...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
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...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
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...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
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...
Shapes2D Assignment Write the following four classes to practice using an abstract class and polymorphism. Submit...
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...
This is in JAVA Shapes2D Write the following four classes to practice using an abstract class...
This is in JAVA 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT