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

Be sure save java file name with Shape2D.java

// 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:


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 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...
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...
This assignment tests your understanding of abstract classes, inheritance, and requirements modeling using class-based methods. The...
This assignment tests your understanding of abstract classes, inheritance, and requirements modeling using class-based methods. The assignment scenario involves an abstract vehicle class and concrete subclasses Jeep and Ford. The vehicle class has the following variables (manufacturer, language), and methods (getManufacturer( ), and getLanguage( )). The manufacturer builds a specific vehicle (a Jeep and a Ford). The manufacturer will notify the consumer class that a Jeep and a Ford are available for purchase. The consumer class purchases the Jeep. Your...
Submit your complete implementation of our abstract list type using an array, ArrayList.java. public class ArrayList...
Submit your complete implementation of our abstract list type using an array, ArrayList.java. public class ArrayList { public static void main(String[] args) { ArrayList list = new ArrayList(); } private String[]a = new String[1000]; private int end = -1; // Throw an IndexOutOfBoundsException if the index is invalid public String get(int index); // Return the first index in the list with the given value, or -1 if it's not found public int find(String val); // Return true if the element...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT