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

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 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.
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)...
(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.
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
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,...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private data for name, selection, wins, and losses. It must have a non-default constructor that requires name. It may not contain a default constructor. Create overloaded functions for the ++ and - - operator. The overloaded ++operator will add to the number of wins, while the - - operator will add to the losses. You will create two different child classes of player, Human and...
Write two classes Class A and Class B in class A you should read from the...
Write two classes Class A and Class B in class A you should read from the keyboard a number and you should call a public method of the class B that will print on the screen whether the number that was read from the keyboard in class A is multiple of 7 and has the last digit 5.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT