Shape Hierarchy Project – Part 3

By now, I’m sure you have got the hang of writing classes with private class variables, constructors to assign them, getter methods to return them, and other methods.

Let’s continue the geometry fun by adding in rectangle, rhombus, and trapezoid!

The Rectangle Class

A rectangle consists of four line segments. The rectangle will need private class variables to store this information. Name them private LineSeg a, b, c, d; 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 rectangle 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 rectangle is the length of all of the line segments added together, or the length of side a multiplied by the length of side b.

The formula for the area of a rectangle is the product of the length of two adjacent line segments.

The Rhombus Class

A rhombus is a quadrilateral (a four-sided shape). More specifically, it is a parallelogram (a four-sided shape whose opposite sides are parallel).

The rhombus will need four lines, perhaps named: LineSeg a, b, c, d; 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 rhombus 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 rhombus is the length of all of the line segments added together.

There are three formulas that you can use to find the area of a rhombus. Let’s use the “diagonals” method. We need to know the length of both diagonals that go across the inside of the rhombus, multiply them together, and then divide that by two.

How do you find the diagonals across the rhombus though? Which line segments and points should we use? Assume that the line segments are made so that A connects with B, B with C, C with D, and D with A. Remember, each line segment has two points, so line segment A has a P1 and P2. Line segement B has a P1 and P2 also.

Assume that A’s P2’s X and Y values are the same as B’s P1’s X and Y values. When we create the final program, the user won’t be able to mess this part up!

Anyway, one diagonal, let’s call it p, that can be from A1 to C1, and the other diagonal can be from B1 to D1. Draw this part out on a sheet of paper if it doesn’t make sense! Label all of your points and line segments!

The Trapezoid Class

A trapezoid is a quadrilateral (a four-sided shape) that has one pair of opposite parallel sides. So, copy-pasta, I mean write a new class with four line segments a b c d, a constructor, and getter methods. Then, make it implement shape and have an area and perimeter method.

The formula for the perimeter… well we probably know this one by now. Add the length of all of the sides!

The formula for the area though… I know that you can solve area for the trapezoid on the left. We could break any trapezoid up into a square and two triangles, but what if the trapezoid is rotated and floating somewhere in a 2D grid? That makes it a lot more difficult to find the height measurement, which is crucial for the easy solution.

The height of a trapezoid can be calculated using the following formula, where h stands for height, and a b c d mean the length of that line segment.

Finally, the area of any trapezoid can be found by the following formula, where a and c are the lengths of the parallel line segments (assumption, enforced in later steps), and h is the height of the trapezoid.

Area= a+c2*h