The technical definition of a right triangle is "a triangle in which one angle is a right angle (that is, a 90degree angle)".

https://en.wikipedia.org/wiki/Right_triangle

Visually, a right triangle looks like: see image.

For purposes of our exercise, we'll simply things a little and assume that the bottom length will be horizontal and that the right angle will be on the left side of the figure. The image above on the right meets this criteria. see image.

  • The point that defines the (x, y) coordinate of the bottom-left point
  • The height
  • The base

Using the three values, we can then calculate the (x, y) coordinates for each of the remaining points. We will follow the standard GUI convention that says that the upperleft point is (0, 0) with x increasing as you move right, and y increasing as you go down. Note that this is different than what you use in math class!

For example, a right triangle with bottomleft point (10.0, 110.0), base 120.0, and height 100.0 would look like the following. see image.

Note that Java has a standard, builtin class called Point2D.Double which can be used to keep up with an (x, y) coordinate for us. For example, we could create a variable named bottomLeftPoint at coordinate (4.2, 9.5) by writing:

Point2D.Double bottomLeftPoint = new Point2D.Double(4.2, 9.5);

You can read more about the Point2D.Double class at:

http://docs.oracle.com/javase/10/docs/api/java/awt/geom/Point2D.Double.html

Part 1

Be sure to read through all items before you begin.

1. Create a new Java project named YourLastNameMidtermExam

2. Add a new package named edu.westga.cs6311.midterm.model that contains a class called RightTriangle.

3. Create a second package named edu.westga.cs6311.midterm.controller.test with two classes (each with their own appropriate Javadoc comments):

a. RightTriangleDemo (this class will *not* have a main method)
b. TestDriver (this class will have a main method)

4. Consider the RightTriangle class specifications and determine the variable(s) that are necessary to perform the tasks in this class.

5. Write the Javadoc specification comments for the RightTriangle constructor. Recall that the purpose of the constructor is to initialize the instance variable(s). Once finished, write the Java code for this constructor.

6. Write the Javadoc specification comments for the accessor (getter) method named getBottomLeftPoint().

7. Write the Java code to implement the method getBottomLeftPoint() which takes no parameters and return a Point2D.Double object.

8. Once you feel that getBottomLeftPoint() is correct, go to the RightTriangleDemo class and define (don't forget the Javadoc comments!)

a. An instance variable of type RightTriangle
b. A 0-parameter constructor that will initialize the instance variable. You may use whatever values you like here to initialize the object.
c. Because we're going to be printing the (x, y) values of a bunch of points here, it would be a good idea to avoid writing duplicate code. We can do this by writing a method named displayPointInformation. This method will accept a Point2D.Double object that we're interested in testing and two double values (the expected x and y values)

If you check the Java API for Point2D.Double (you should) you'll find that this class defines getX() and getY(). These methods can be used to pull the x and y values out of the object. This method should end up looking something like: see image.

d. A method called testRightTrianglePart01. This method should accept no parameters and should not return a value. Inside the method body write code to:

i. Call on the getBottomLeftPoint method and store its result
ii. Display a line of output to describe what you are testing (here it's the bottom left point value)
iii. Pass the bottom left point value, along with the expected x and y values over to the displayPointInformation method

9. Now it's time to turn to the driver's code. Go to the TestDriver class' main method and include code to:

a. Declare a RightTriangleDemo object
b. Use that object to call the testRightTrianglePart01 method.

10. Run the program and confirm that the expected and actual values match. If they don't, go back and fix the logic errors so that they do.

Repeat this process implement the method, add test code to testRightTrianglePart01, run the program to test it, and fix any errors before moving on for each of the following methods:

a. getHeight
b. getBase
c. getTopPoint
d. getBottomRightPoint
e. getHypotenuse

Recall from Geometry class that the hypotenuse is calculated with the formula:

Hypotenuse2 = base2 + height2

Using the example from above with horizontal 120.0 and vertical 100.0, the hypotenuse would be:

square root of 24400.0 = 153.2

f. getArea

In order to calculate the area of a right triangle, use the following formula:

area = 1/2 * base * height

g. getPerimeter

Note: Recall that we are stressing code reuse, so be sure you are not repeating calculations or storing calculated results in instance variables.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.