Unlike most recitations, this first recitation will be more like a CS 401 lab. You will be writing a program that uses many of the concepts discussed in CS/COE 401.
Design and program a RightTriangle
class (name the class RightTriangle
) that represents the triangle's three sides, its area, and its perimeter. Consider what information should be fields and what information should be calculated from those fields. At a minimum, you must store the two legs of the triangle as fields. Below are the methods that your class must provide:
setLegA
- Set the length for leg A. This must be a number greater than 0.setLegB
- Set the length for leg B. This must be a number greater than 0.getLegA
- Get the length for leg A.getLegB
- Get the length for leg B.getHypotenuse
- Get the hypotenuse of the triangle.getPerimeter
- Get the perimeter of the triangle.Your mutator methods must handle the case that they are given bad values. They should not assign the bad value to the field. They must alert the user (i.e. the programmer using your RightTriangle
class) that the value was bad.
The class must maintain encapsulation and data abstraction.
Name your class RightTriangle
.
Demonstrate that your class behaves as described by writing a program in a separate file. The program must have the following steps:
getHypotenuse
and print out the returned value.getPerimeter
and print out the returned value.Consider the following questions:
A right triangle is a triangle where one angle is a right angle (i.e. 90 degrees). The side of the triangle opposite that right angle is called the hypotenuse; the other two sides are called legs. An image of a right triangle is shown below.
The formula for calculating the hypotenuse is: SQRT(A2 + B2)
, where A
is the length of leg A, B
is the length of side B, and SQRT
is the square root function (which you can find in the Math class).
The formula for calculating the triangle's perimeter is: A + B + hypotenuse
, where A
is the length of leg A, B
is the length of side B, and hypotenuse
is the length of the hypotenuse.
This is for your practice. You should be able to complete this before the end of recitation. If you do, call the TA over to look over your program to make sure it is correct. If you do not have time to complete it before the end of recitation, you have the option of uploading it to CourseWeb once you are finished to receive feedback on it from the TA. If you upload it after Monday, September 7, please email the TA so he knows to look at it.