Perl

Menu

In-Class Assignment 7

This assignment will introduce you to Class inheritance in Perl.

Download or copy the files ~lallip/public_html/ica7.pl and ~lallip/public_html/Polygon.pm. The Polygon file defines a class named Polygon. A Polygon is implemented simply as a list of lengths. (For example, a triangle whose lengths are 3, 4, and 5 would be implemented as a blessed reference to an array containing [ 3, 4, 5 ]). There are three methods written (in addition to the constructor): num_sides, perimeter, and area. The first method simply returns how many sides the Polygon has. The second returns the sum of the side lengths (taking advantage of the standard List::Util module to compute a sum). The third method prints out a warning saying that there is no way to know the area of a generic polygon.

Write a new module, Rectangle which inherits from Polygon. Your new module should define a constructor (named new), which takes in the length and width of a new rectangle. Your constructor will call the Polygon's constructor to define the new object (don't forget to bless your object into the correct class after it's been returned). You should also write two standard accessor methods for the length and width of the rectangle. Finally, write a new area method to compute and return the area of a rectangle.

Note that you are not permitted to change the implementation of the objects. Your Rectangle will be implemented just as a Polygon is - as an array of side lengths. If I were to print the array that your Rectangle object references, I should get a list of four side lengths. You should also not write your own perimeter or num_sides methods; they should be inherited from Polygon.

For one full bonus point, define another class, named Square within the same Rectangle.pm file. (That is, simply give another package statement upon the conclusion of your last Rectangle method, and start defining your new Square methods). Your Square class will inherit from Rectangle, not from Polygon. It should provide a constructor which accepts a single side length, and calls Rectangle's constructor to create the new object. Create a new length accessor, and a new area method. (Uncomment the relevant lines in ica7.pl to test your Square class).

Sample Output

Once you have defined your Rectangle class, the main file provided should produce this output:
This Polygon has 3 sides
Perimeter: 16
Don't know how to get the area of a Polygon! at ./ica7.pl line 27
Use of uninitialized value in print at ./ica7.pl line 27.
Area:
This Rectangle has 4 sides
Perimeter: 16
Area: 12
This Rectangle has 4 sides
Perimeter: 18
Area: 20
This Square has 4 sides
Perimeter: 12
Area: 9
This Square has 4 sides
Perimeter: 40
Area: 100

Submission

As per normal, submit your program on rcs-sun4.rpi.edu by running ~lallip/public/ic_submit.pl You should be submitting ONLY the Rectangle.pm file. Do not tar or gzip or compress any files. You are not permitted to modify either ica7.pl or Polygon.pm. I should be able to test your file just by adding your Rectangle.pm file to my directory.

This assignment is due at 6pm today, Wednesday, October 19