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 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 );

Solutions

Expert Solution

In below code the an abstract class Shape2D is there which is extended by two classes Rectangle2D and Circle2D ..These two classes having instance variables and parameterised contructor and both class implementing or can say overriding the get2DArea( ) method according to its own requirements as desrcibed in code ..Also Shape2D driver class is there which has method displayArea( ) which takes an object as parameter and displays the area ....

Code snippet

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

/**
 *
 * @author vijay
 */
abstract class Shape2D // defining the abstract class Shape2D 
{
     abstract double get2DArea();// the abstract method which will be implemented by class extending this class 
    
}
class Rectangle2D extends Shape2D //Rectangle2D class extending the Shape2D class
{
   double length,width; //declaring the length and width
 
   Rectangle2D(double length,double width) //parameterised constructor to initialise the constructor
   {
       this.length=length; //assigning the length 
       this.width=width; //assigning the width
   }
 
    @Override
    double get2DArea()  //implementing the method declared as abstract in abstract class  
    {
        double area= length*width; //calculate the area
        return area; //return the area
    
    }
    
    
}

class Circle2D extends Shape2D //Circle2D class extending the Shape2D class
{
    double radius; //declaring the radius variable
    Circle2D(double radius) //parameterised constructor to initialise the constructor
    {
        this.radius=radius; //assigning the radius
    }

    @Override
    double get2DArea() 
    {
       
        double area=Math.PI*radius*radius; //calculate the area of circle
         return area; //return the area 
    
    }
       
    
}
class Shape2DDriver  //defining the Shape2DDriver class 
{
    
    void displayArea(Shape2D figure) //function to display the area of the passed object
    {
       double area=figure.get2DArea();//get area by calling get2DArea() function
      //simple math logic to round to 1 decimal place
      //multiply the number by 10 and then convert it to int and then divide by 10 coverting into double
       area=area*10; // multiplied by 10
     int area_round=(int)area;//coverted to int
        area=((double)area_round)/10; //divide by 10 and again assigned to double variable
    System.out.println("The area of figure is: "+area); //print the area 
    }
    
}


public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    Shape2DDriver tester = new Shape2DDriver();//make object Shape2DDriver class
 
Rectangle2D r = new Rectangle2D( 2, 3 ); //Rectangle2D object
 
Circle2D c = new Circle2D( 4 ); //Circle2D object

tester.displayArea( r ); //calling the function display area from tester object by passing Rectangle2D object

tester.displayArea( c ); //calling the function display area from tester object by passing Circle2D object
    
    }
    
}

Screenshot


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