Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this lab we will explore how polymorphism works in Java programs. We will also learn about abstract classes and methods. Finally, we will learn

In this lab we will explore how polymorphism works in Java programs. We will also learn about abstract classes and methods. Finally, we will learn about "protected" - a new access modifier. There are no starting files for this lab.
We will use polymorphism to help us write a program dealing with several different shapes. We will work with triangles, rectangles, and ellipses. Each of these shapes has similar properties. They each have a height, width, and area. What is different about each shape, however, is the process for calculating the area.
Part 1- Shape Class
First, let's write a Shape class to represent the common aspects of all shapes. The Triangle, Rectangle, and Ellipse classes will all inherit from the Shape class. We will make the Shape class abstract, which means we will not be able to instantiate it. When we want to make a shape, we will need to instantiate one of the concrete classes: Triangle, Rectangle, or Ellipse.
Write a Shape class and declare it abstract. The class header should look like this:
public abstract class Shape
Include the following protected instance variables:
height (double)
width (double)
area (double)
Note: usually we make instance variables private, but in this case, we want to allow descendants of the Shape class (classes that inherit from Shape) to have direct access to the instance variables. We can do this by declaring them "protected" instead of "private". Example:
protected double height;
Include at least one constructor, and include get and set methods for the height and the width. The area data member also needs a get method.
Write a toString method that returns a String containing the shape's height, width, and area (formatted nicely).
Finally, declare an abstract method called calculateArea. This method will not take in any parameters or return anything. Its job is to calculate the area of the shape and store that value in the area data member. However, we cannot write the code for this method because we do not know what type of shape we are dealing with. We will declare the method "abstract". This means that any concrete class that inherits from Shape will have to implement this method. The method header should look like this:
public abstract void calculateArea();
Part 2- Concrete Classes
Next let's work on our concrete classes. These are the Triangle, Rectangle, and Ellipse classes, which will represent specific types of shapes. Create these classes in your project. They should each inherit from (extend) the Shape class. In each class, write a calculateArea method. The calculateArea method should contain the code necessary to calculate the area of the particular shape and store the value in the area instance variable. Here are the various area formulas:
Triangle Area =(1)/(2)* height * width
Rectangle Area = height * width
Ellipse Area = pi *((1)/(2) height)*((1)/(2) width)
Remember you can use Math.PI to get the most accurate value of pi in your program. You do not need anything other than the calculateArea method in these three classes.
Part 3- Main Class
Now that you've created your abstract class and three concrete classes, let's test them and see how they work. Create a main class and write code that does the following:
Create an ArrayList to store Shape objects.
Ask the user what kind of shape they want to create.
Prompt the user for the height and width of the shape.
Create the appropriate Shape object, set the height and width, and store the object in the ArrayList.
Allow the user to continue creating shapes until they are finished.
Write a loop that iterates through the ArrayList and calculates the area of each shape (by calling each object's calculateArea method).
Write a loop that iterates through the ArrayList and prints information about each shape to the screen (using the toString method).
Part 4- Another Class
Add in an additional class that inherits from the Shape class (not a Circle).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Use Property Number 6 to find L{f(t)} if f(t) = e at u(t 1)

Answered: 1 week ago

Question

Perform an Internet search. Discuss a company that uses EPLI.

Answered: 1 week ago

Question

How do you feel about employment-at-will policies? Are they fair?

Answered: 1 week ago