$30.00
Description
In this lab, you will be implementing the following class hierarchy. Your concrete classes must call the superclass constructor with the proper number of sides (which is constant for each concrete shape).
The perimeter method is implemented in the superclass as it does not change based on the number of sides. The area method must be overridden in each subclass as the area is dependent on the type of polygon. The areas of an equilateral triangle, square, and a regular
hexagon given side length a are
^{√}^{3} _{�}_{�}^{2}_{,} _{�}_{�}^{2}_{,} _{a}_{n}_{d}
^{4}
^{3}^{√}^{3}
^{2}
^{�}^{�}^{2}^{.}
The superclass area method should just return 0 as a default value. This is called “kludge”. Later, we will learn about abstract classes to deal with cases like this.
I have provided an empty template for a Regular Polygon, Triangle, Square, and Hexagon. You must fill in all the correct methods and make sure each subclass extends RegularPolygon. I
have also provided a driver that you may use to test your code. Take note of what is happening when I am filling the array containing RegularyPolygons and what happens when I print out the array. Output is on the next page. When complete show your TA.
RegularPolygon
– numSides: int
– sideLength: int
+ RegularPolygon(numSides: int, sideLength: int)
+ getNumSidos(): int
+ getSideLength(): int
+ perimeter(): double
+ area* double
Mangle Square Hexagon
+ Triangle(sideLength: int)
+ area°, double
+ Square(sideLength: int)
+ area(): double
+ Rexagor(sideLength: int)
+ area(): double
Simple way to create objects, as you’ve seen before. Area of each object is:
Triangle area: |
6.9282 |
^{Squar}^{e} ^{a}^{rea:} |
^{9.0000} |
^{Hexago}^{n} ^{area:} |
^{10.3923} |
An array containing triangles, squares, and hexagons has been created. How can this be? Polymorphism. Output is below:
Class: Triangle
Number of sides: 3
Side length: 3
Area: 3.8971
Class: Square
Number of sides: 4
Side length: 4
Area: 16.0000
Class: Hexagon
Number of sides: 6
Side length: 5
Area: 64.9519
Class: Triangle
Number of sides: 3
Side length: 6
Area: 15.5885
Class: Square
Number of sides: 4
Side length: 7
Area: 49.0000
Class: Hexagon
Number of sides: 6
Side length: 8
Area: 166.2769
Class: Triangle
Number of sides: 3
Side length: 9
Area: 35.0740
Class: Square
Number of sides: 4
Side length: 10
Area: 100.0000
Class: Hexagon
Number of sides: 6
Side length: 11
Area: 314.3672