Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program the following java class Parallelogram A parallelogram can be thought of as a rectangle that has been skewed sideways by a certain angle. In

Program the following java class

image text in transcribed

image text in transcribed

image text in transcribed

Parallelogram A parallelogram can be thought of as a rectangle that has been skewed sideways by a certain angle. In a rectangle, this angle is always a/2 (that is, 90), so the upper left point always had the same x coordinate as the lower left point: upper Right lowerLeft The parallelogram extends this concept by skewing the shape by some angle strictly less than a radians (180): upper Right skewAngle lowerLefte... base With that in mind, create a subclass of Rectangle called Parallelogram.java: Rectangle - lowerLeft : Point get>> - upperRight : Point get >> + Rectangle() + Rectangle(Point, Point) + Rectangle(double, double) + getLowerRight(): Point + getUpperLeft(): Point Parallelogram - skew Angle: double get >> + Parallelogram) + Parallelogram(Point, Point, double) + baseLength(): double Parallelogram adds the private attribute skewAngle, which is the angle between the positive x axis and the left edge of the parallelogram. The default constructor should delegate to the default Rectangle constructor, and manually set skewAngle to 7/2. The second constructor takes two points, the upper right and lower left corners, and a skew angle. Again, you can delegate to the parent constructor before setting the angle. The angle must be strictly less than 2, and must also be greater than the angle of the diag- onal of the surrounding rectangle, (i.e. skew Angle > Math.atan2(y2 - y1, x2 - x1)). For convenience, check that the angle is in bounds, and if not simply set it back to the default of r/2. Add the method base Length() to return the length of the base of the parallelogram. The base shown in the diagram above refers to the base of the right triangle formed by cutting off one of its sides. You can calculate its length with this formula: base height tan Note that when the skew angle is wider than 1/2 radians, the orientation of this triangle is flipped, which will result in this value being negative. Override get LowerRight) and get UpperLeft to return the coordinates of the skewed corner points. In general, the y coordinates of the opposing corners will be the same, but the x coordinates will be offset, either to the left or to the right, by the base length. Override width() to return the total width of the parallelogram. If @ n/2, then the width is extended by 2x the base length. Override both area() and perimeter(). The area of a parallelogram is still width x height, but you can't calculate the width by subtracting the lower left x coordinate from the upper right. Instead, the length of top and bottom edges is Xright Xleft - base. Likewise, the length of the skewed vertical edges is calculated differently: v = height With this information in mind, it should be easy to calculate both the area and perimeter. Override inBounds () to check whether a given point is inside the parallelogram. You can easily check that the point's y coordinate is between the top and bottom edges of the parallelogram. You also need to check that the point is below the line of its left edge and above the line for its right edge, (or vice versa if > 1/2). A point (x,y) is within those two lines if its coordinates satisfy both of these inequalities: height height, y - Ybottom base (x-Xleft) y - Ytop hau (x - Xright) The math is almost the same in the case where e > 1/2, but with the > and > - upperRight : Point get >> + Rectangle() + Rectangle(Point, Point) + Rectangle(double, double) + getLowerRight(): Point + getUpperLeft(): Point Parallelogram - skew Angle: double get >> + Parallelogram) + Parallelogram(Point, Point, double) + baseLength(): double Parallelogram adds the private attribute skewAngle, which is the angle between the positive x axis and the left edge of the parallelogram. The default constructor should delegate to the default Rectangle constructor, and manually set skewAngle to 7/2. The second constructor takes two points, the upper right and lower left corners, and a skew angle. Again, you can delegate to the parent constructor before setting the angle. The angle must be strictly less than 2, and must also be greater than the angle of the diag- onal of the surrounding rectangle, (i.e. skew Angle > Math.atan2(y2 - y1, x2 - x1)). For convenience, check that the angle is in bounds, and if not simply set it back to the default of r/2. Add the method base Length() to return the length of the base of the parallelogram. The base shown in the diagram above refers to the base of the right triangle formed by cutting off one of its sides. You can calculate its length with this formula: base height tan Note that when the skew angle is wider than 1/2 radians, the orientation of this triangle is flipped, which will result in this value being negative. Override get LowerRight) and get UpperLeft to return the coordinates of the skewed corner points. In general, the y coordinates of the opposing corners will be the same, but the x coordinates will be offset, either to the left or to the right, by the base length. Override width() to return the total width of the parallelogram. If @ n/2, then the width is extended by 2x the base length. Override both area() and perimeter(). The area of a parallelogram is still width x height, but you can't calculate the width by subtracting the lower left x coordinate from the upper right. Instead, the length of top and bottom edges is Xright Xleft - base. Likewise, the length of the skewed vertical edges is calculated differently: v = height With this information in mind, it should be easy to calculate both the area and perimeter. Override inBounds () to check whether a given point is inside the parallelogram. You can easily check that the point's y coordinate is between the top and bottom edges of the parallelogram. You also need to check that the point is below the line of its left edge and above the line for its right edge, (or vice versa if > 1/2). A point (x,y) is within those two lines if its coordinates satisfy both of these inequalities: height height, y - Ybottom base (x-Xleft) y - Ytop hau (x - Xright) The math is almost the same in the case where e > 1/2, but with the > and

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions