Question

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.

Written in java

Homework Answers

Answer #1

HERE IS THE CODE AS PER YOUR REQUIREMENT

CODE:

// creating abstract class
public abstract class Shape2D{
   // creating abstract method
   public abstract double get2DArea();
}
// creating rectangle2D class that inherits shape2D
class Rectangle2D extends Shape2D{
   double length;
   double width;
   // constructor that takes double inputs
   Rectangle2D(double length, double width){
       this.length = length;
       this.width = width;
   }
   // returns the rectangle length
   public double get2DArea(){
       return length*width;
   }
}
// creating the circle2D class that inherits Shape2D
class Circle2D extends Shape2D{
   double radius;
   // constructor that takes double input
   Circle2D(double radius){
       this.radius = radius;
   }
   // returns circle length
   public double get2DArea(){
       return Math.PI*radius*radius;
   }
}
// Driver class to test and execute
class Shape2DDriver{
   // displayname method to display the area of the objects we created
   public static void displayName(Shape2D nShape){
       System.out.println("Area : "+Math.round(nShape.get2DArea()));
   }
   public static void main(String[] args) {
       Shape2D rectangle = new Rectangle2D(5.5,2.5);
       Shape2D circle = new Circle2D(2.5);
       displayName(rectangle);
       displayName(circle);
   }
}

SCREENSHOTS:

OUTPUT:

NOTE: Be sure save java file name with Shape2D.java

IF YOU HAVE ANY QUERIES FEEL FREE TO ASK AT COMMENTS

PLEASE DO LIKE :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an...
a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class...
a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class named PhoneCall that includes a String field for a phone number and a double field for the price of the call. Also include a constructor that requires a phone number parameter and that sets the price to 0.0. Include a set method for the price. Also include three abstract get methods - one that returns the phone number, another that returns the price of...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields and appropriate types and permissions diceType numSides sideUp The class should have A 0 argument (default) constructor default values are diceType: d6, numSides: 6, sideUp: randomValue 1 argument constructor for the number of sides default values are diceType: d{numSides}, sideUp: randomValue 2 argument constructor for the number of sides and the diceType appropriate accessors and mutators *theoretical question: can you change the number of...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
Lab Objectives This lab was designed to inforce the following programming concepts: • Using classes to...
Lab Objectives This lab was designed to inforce the following programming concepts: • Using classes to create a data type SimpleCalculator capable of performing arithmetic operations. • Creating const member functions to enforce the principle of least privilege. The follow-up questions and activities also will give you practice: • Using constructors to specify initial values for data members of a programmer-defined class. Description of the Problem Write a SimpleCalculator class that has public methods for adding, subtracting, multiplying and dividing...
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance:...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance: ⦁   A base class called Pet ⦁   A mix-in class called Jumper ⦁   A Dog class and a Cat class that each inherit from Pet and jumper ⦁   Two classes that inherit from Dog: BigDog and SmallDog ⦁   One classes that inherit from Cat: HouseCat The general skeleton of the Pet, Dog, and BigDog classes will be given to you (see below, "Skeleton", but...
Class VacationPackage java.lang.Object triptypes.VacationPackage Constructor Summary Constructors Constructor and Description VacationPackage(java.lang.String name, int numDays) Initializes a...
Class VacationPackage java.lang.Object triptypes.VacationPackage Constructor Summary Constructors Constructor and Description VacationPackage(java.lang.String name, int numDays) Initializes a VacationPackage with provided values. Method Summary All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description boolean equals(java.lang.Object other) Provides a logical equality comparison for VacationPackages and any other object type. double getAmountDue() This method provides the remaining amount due to the travel agent for this trip less any deposit made upfront. abstract double getDepositAmount() This method provides the required...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...