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

The following points can be followed to create the mentioned four classes:

  • The abstract keyword will be used for making the class Shape2D as abstract. The get2DArea() method will be kept empty.
  • Classes Rectangle2D and Circle2D will be created. These classes will extend the Shape2D abstract class.
  • Define the constructor and get2DArea() for each subclass based on the data members.
  • Define a method displayName() in a class Shape2DDriver.
  • In this class, create objects from the subclasses.
  • The method displayName() will display the area of the passed object.

The java program:

import java.lang.Math.*;
import java.text.DecimalFormat;
//abstract class Shape2D
abstract class Shape2D{
    abstract double get2DArea();
}
//class Retangle2D
class Retangle2D extends Shape2D{
    
    //data members
    private double length;
    private double width;
    
    //constructor
    Retangle2D(double l,double w){
        length = l;
        width = w;
    }
    
    //method to get area
    public double get2DArea(){
        return length * width;
    }
}
//class Circle2D
class Circle2D extends Shape2D{
    
    //date member
    private double radius;
    
    //constructor
    Circle2D(double r){
        radius = r;
    }
    
    //method to get area
    public double get2DArea(){
        return Math.PI * radius * radius;
    }
}
public class Shape2DDriver
{
 public static void main(String[] args) {
 
 //Object of Circle2D
 Circle2D c = new Circle2D(6);
 
 //Object of Retangle2D
 Retangle2D r = new Retangle2D(11,8);
 
 //area of Circle
 displayName(c);
 
 //area of Retangle
 displayName(r);
 
 //using abstract class
 Shape2D s = new Retangle2D(7,5);
 
 displayName(s);
 
 }
 
 //method displayName
 static void displayName(Shape2D obj){
     
     //format to round upto one decimal
     DecimalFormat df = new DecimalFormat("#.#");
        
        //print the Area of the object
     System.out.println("Area :" + df.format(obj.get2DArea()) );
 }
}

Sample output:


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...
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.
CS Using the following UML outline, create the following abstract parent class along with the 4...
CS Using the following UML outline, create the following abstract parent class along with the 4 subclasses and Driver class. Implement the Classes, listed attributes and methods along with any additional things that you may need. Add a driver class that utilizes the methods/attributes in the classes to output the following based on the vehicle type (therefore, you must create an instance for all 4 subclasses in your driver): 1. Vehicle Type (i.e., Car, Airplane, etc.) 2. Transportation method (wheels,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT