asg6

advertisement
Computer Programming I
COP 2210
Instructor: Greg Shaw
Programming Assignment #6
(Decision-Making)
Consider the following triangle definitions:
Triangle
Definition
Equilateral
all three sides are the same length
Isosceles
any two sides are the same length


Right
any two side lengths where the sum of their squares is equal to the
square of the other side length
Obtuse
any two side lengths where the sum of their squares is less than the
square of the other side length
Acute
a triangle is an acute triangle if it is neither a right triangle nor an obtuse
triangle
If the length of any side is greater than the sum of the lengths of the other two sides, then
there is no triangle
Note that Isosceles triangles are also either Right, Obtuse, or Acute
I. Implement a TriangleAnalyzer Class (Triangalyzer?)
Your class will have
1.
private instance variables that store the integer lengths of the 3 sides
2.
a constructor that initializes the sides to parameters passed by the user
3.
accessor methods that return each of the side lengths (optional: instead, you may have
one accessor method that returns a string containing all three side lengths)
4.
a method that returns a String indicating the type of triangle (Equilateral, Isosceles, etc).
If the three side lengths do not form a triangle, an appropriate message should be
returned. See “Additional Specifications,” below
5.
a method that computes and returns the area using Heron’s formula
area =
√ s (s-a) (s-b) (s-c)
where a,b, and c are the lengths of the sides, and
s = ½ the perimeter
II. Write a Class to Test Your TriangleAnalyzer Class
1.
have the user enter the lengths of the 3 sides
2.
create an object
3.
call the accessor method(s) to get the side lengths and print them
4.
call the method that returns the type of triangle (or an appropriate message if the sides
do not form a triangle) and print the String returned
5.
if the side lengths do form a triangle, call the method that computes and returns the
area and print it
III. Additional Specifications
1.
As stated above, the side lengths are to be type int
2.
Assume valid input
3.
If the triangle formed is Isosceles, then the string returned should be either Isosceles
Right, Acute Isosceles, or Obtuse Isosceles, as the case may be
(If all three sides are the same length, then the String returned should be just
Equilateral, since by definition every Equilateral triangle is also acute)
IV. Data to be Used
Test your program with the following sets of data and hand in the output for all 7 sets:
9
4
13
3
2
9
5
9
3
5
7
6
8
8
9
5
12
3
3
8
4
V. Due Date:
Tuesday, March 27th
VI. "Extra Credit" Opportunity
To gain valuable programming experience, expand your programming knowledge, simplify
your class by eliminating duplicate code, and earn 5 extra credit programming points, add
boolean methods to your TriangleAnalyzer class to indicate whether or not a triangle is
Equilateral, Isosceles, etc. These methods will be called from the method that returns the
String containing the type of triangle formed.
Download