CS230 Lab: Inheritance and Polymorphism

Lab: Inheritance and Polymorphism

Goals:

Working with

  • inheritance,
  • collection of objects in hierarchy, and
  • polymorphic references.
 

Meet the Shapes

Download the lab4 folder to your Desktop.

[PRELAB]Introduction

  1. Inheritance is one of the principle ways in which software can be reused. Code of classes can be reused in other situations by modifying a basis class.
  2. The proper designations of base classes can provided a useful hierarchy of classes.
  3. Today, we have provided you with the abstract Shape class. This class is the base for a hierarchy of classes that define the properties of various geometric figures.
  4. We have also provided the class Circle that inherits from the Shape class and includes the radius property and the area calculation definition. The same is true of the provided Rectangle and Square classes. Examine the code and documentation of these classes. Next you will be defining a couple of your own.

    [END of PRELAB]

 

Task 1: The Triangle Class

Here you will create a new class Triangle that provides functionality for a triangle. Recall from Geometry that a triangle is defined by its three sides, let's call them a, b, and c. Its perimeter is simply the sum of its three sides. Its area can be computed using Heron's formula:

s = (a+b+c)/2

area = sqrt(s(s-a)(s-b)(s-c))

Task 1A: Triangle in the Shapes hierarchy

Determine where the Triangle class should be placed in the Shapes hierarchy. Start by writing the header of the Triangle class that extends the appropriate parent class. Add the main method so you are ready to test as you add methods.

Task 1B: Instance variables and constructor

Determine what instance variables should Triangle contain. Then, create the constructor that gives values to these instance variables. Do not forget to call any super constructor, if applicable. At this point, create a few triangle objects to test your constructor.

Task 1C: Other methods

There may be some methods that you have to overwrite, otherwise the compiler will not let you off the hook. Determine which ones they are, and implement them. Of course, test your implementation.

(optional for now) Task 1D: Pretty-print and document!

If you have time, at the end, make the printout better, like as shown below. Also, write javadoc-compliant documentation and enjoy the professional look that your documentation has!

Some informative output:

dot2: circle of area 105.683 (radius: 5.8)

dot3: circle of area 0 (radius: 0.0)

dot2 area (expected: 105.68) Got: 105.68317686676063

dot3 perimeter (expected: 0.0) Got: 0.0

 

Task 2: The Isosceles Triangle

Define one more class, in the Shape hierarchy, to represent an Isosceles triangle, that is a triangle that has any two of its sides equal. (When dealing with floating-point numbers, equality is approximate, not exact. If a difference is less than minDiff we consider the difference to be zero.) Think carefully about how to do so. Where should this class inherit from? What instance variables should you include? What inherited instance variables are visible from within this class? Discuss and answer these questions with your partner before writing any code.

As always, test your new class.

 

Task 3: A Collection of Shapes

Complete the ShapesCollection class

The ShapesCollection class will use an array to maintain a collection of Shape objects (Rectangles, Circles, Triangles). In the downloaded material, you are provided with a starting template for the ShapesCollection class. Look at the code already included in this class. Then set up the necessary instance variables, and constructor.

Write the following methods in your ShapesCollection class:

  1. a method to add an object of type Shape to the collection.
  2. a main() method to start your testing right away. Below is some starting sample testing output. You will need to add more testing!
    
    This collection contains 2 shapes:
    rectangle of area 12 (length: 2.0 width: 6.0)  
    triangle of area 6 (sides: 3.0, 4.0, 5.0)
    
  3. public int findLargest() Returns the index of the Shape with the largest area in the collection of shapes. Test comparing a couple of Shapes that you have added in the collection. Note that you will need to use a method to compare two shapes by comparing their areas. Do you need to write a compareTo() method, or is the method already available to you because it is defined in the appropriate class in the hierarchy?