Shape Hierarchy Project – Part 2
With the shape interface, point class, and line class completed, you are ready to begin creating the shape hierarchy. Let’s begin with three simple shapes: A square, a triangle, and a circle.
The Square Class
A square consists of four line segments. The square will need private class variables to store this information. Name them private LineSeg L1, L2, L3, L4; or similarly. Create a constructor that links the parameter values with these private class variables. Do not forget to write getter methods for the line segments!
Make sure that your square class implements shape and that it includes the perimeter and area methods as signified in the shape class.
The formula for the perimeter of a square is the length of all of the line segments added together, or the length of one line segment multiplied by four.
The formula for the area of a square is the product of the length of two adjacent line segments, or a line segments length squared. Get it, squared! Haha! Okay, well nobody read this stuff anyway right?
The Triangle Class
A triangle consists of three line segments. The triangle will need private class variables to store this information. Name them private LineSeg L1, L2, L3; or similarly. Create a constructor that links the parameter values with these private class variables. Do not forget to write getter methods for the line segments!
Make sure that your triangle class implements shape and that it includes the perimeter and area methods as signified in the shape class.
The formula for the perimeter of a triangle is the length of all of the line segments added together.
The formula for the area of triangle depends on what kind of triangle is defined. The formula, ( ½ * base * height ), will only work if you know the exact height of the triangle, which is difficult given a possibly rotated scalene triangle. A solution does exist, but you may not have encountered it previously in your geometry class.
Heron’s formula calculates the area of a triangle and requires no arbitrary base or height measurement. Step one is calculating the semi-perimeter of the triangle. Remember, semi means half, so the semi-perimeter of a polygon just means find the perimeter and divide by two. Step two is to perform the following algorithm:
Area=s s-a s-b s-c
Where s is the semi-perimeter of the triangle, and a, b, and c are the lengths of the three sides of the triangle.
The Circle Class
A circle consists not of line segments, but of a point and radius. The private variables for this class will be Point p and double r. The constructor then must expect one variable of type Point and one of type double and link them to these values. Do not forget to write the getter methods for the point and radius!
Make sure that your circle class implements shape and that it includes the perimeter and area methods as signified in the shape class.
Circles have particularly interesting formulas to calculate their perimeter and area because they are not defined by line segments. Calculating the perimeter is not as easy as simply adding up the length of all of its line segments. We must use the mathematical constant known as pi. Pi is the ratio of a circle’s perimeter (aka circumference) to its diameter (twice its radius). This relationship yields the constant 3.14159… for any circle. In Java, we can use Math.PI to recall 3.141592653589793, which is more than enough decimals places than almost all applications need. Using pi, the perimeter of a circle is defined by the following formula:
Perimeter=2πr
And, finally the area of a circle is defined by the following formula:
Area=πr2