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

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

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...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company.
The purpose of this lab is to practice using inheritance and polymorphism.    We need a set of classes for an Insurance company.   Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc.    Insurance policies have a lot of data and behavior in common.   But they also have differences.   You want to build each insurance policy class so that you can reuse as much code as possible.   Avoid duplicating code.   You’ll save time, have less code to...
Using Classes This problem relates to the pre-loaded class Person. Using the Person class, write a...
Using Classes This problem relates to the pre-loaded class Person. Using the Person class, write a function print_friend_info(person) which accepts a single argument, of type Person, and: prints out their name prints out their age if the person has any friends, prints 'Friends with {name}' Write a function create_fry() which returns a Person instance representing Fry. Fry is 25 and his full name is 'Philip J. Fry' Write a function make_friends(person_one, person_two) which sets each argument as the friend of...
Chapter 9 Programming Project (Inheritance, Abstract Class/Methods, Overriding Methods) -For all classes, you need to provide...
Chapter 9 Programming Project (Inheritance, Abstract Class/Methods, Overriding Methods) -For all classes, you need to provide the accessor and mutator methods for all instance variables, and provide/override the toString methods. -Create a Customer class that has the attributes of name and age. Provide a method named importanceLevel. Based on the requirements below, I would make this method abstract. -Extend Customer to two subclasses: FlightCustomer, and RetailCustomer -FlightCustomer attributes: ticketPrice, seatNumber (The seat number should be a randomly generated number between...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give in the end of question) a method to return the botanical (Latin) name of the plant a method that describes how the plant is used by humans (as food, to build houses, etc) Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information: list 2 dishes (meals) that the vegetable can be used in...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public class DArray { private int array[]; public DArray() { } private void expandArray() { } private void shrinkArray() { } } --------------------------------------------------------------- public abstract class ArrayBP { protected int numElements; protected int numAllocations; public abstract void storeAt(int item, int index) { } public abstract getFrom(int index) { } public abstract int len() { } public abstract void remove();{ } public abstract void removeAt(int index)...
Write an AutoInsurance class to abstract auto insurance polices.
USE C#.Write an AutoInsurance class to abstract auto insurance polices. Each auto insurance policy contains policy holder's name, policy number, car model, monthly premium, inception date, and expire date. This class needs to provide two constructors and one method which is to calculate the yearly premium.
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT