- Practice writing JavaScript classes that inherit from other classes
In this lab we're going to create a geometry application that allows us to
calculate different properties of multiple shapes. We'll be using JavaScript's
class keyword to build objects that inherit from its base object.
- Define a
Polygonclass.Polygonwill accept anArrayof integers as a parameter, which will represent each side of a shape.
- Use the
getkeyword to make a getter methodcountSidesthat counts the number of sides (each index in the array). - Use the
getkeyword to make a getter methodperimeterthat calculates the sum of each side (each index in the array) of the polygon. This method will become accessible to its child classes.
- Define a
Triangleclass that inherits fromPolygon.
- It will automatically have access to
countandperimeterinherited fromPolygon. - Use the
getkeyword to make a getter methodisValidthat checks if the given 3 sides for a triangle is valid.
The sum of the lengths of any two sides of a triangle is greater than the length of the third side. If you take the three sides of a triangle and add them in pairs, the sum is greater than (not equal to) the third side. If that is not true, then it is not possible to construct a triangle with the given side lengths.
- Define a
Squareclass that inherits fromPolygon.
- It will automatically have access to
countandperimeterinherited fromPolygon. - Use the
getkeyword to make a getter methodisValidthat checks if the given 4 sides for a square is valid. A square is valid when the lengths of all sides are equal. - Use the
getkeyword to make a getter methodareathat calculates the area of the square.