|
|
Homework J5: Triangulation
Due: Friday, 1 April 2005 by 10 a.m.
|
Introduction
For this homework, you will develop a program that will count types of
triangles. There are two files that need to be
submitted: Triangle.java
and TriangleCount.java. No skeleton code is being provided.
There are two parts to this assignment. The first, developing the
Triangle class, reviews creating classes and using if and switch statements
(your program can use one, the other, or both types of statements). The
second, developing the TriangleCount.java file, uses for and while loops.
Background
Recall that there are many types of triangles:
|
|
|
|
Right
triangle |
Isosceles
triangle |
Equilateral
triangle |
Scalene
triangle |
- Right triangle: where one of the angles is a right angle (90°).
The sides of a right triangle fulfills Pythagorean's theorem: a2+b2=c2,
where c is the length of the diagonal side, and a and b
are the lengths of the two sides adjacent to the right angle
- Isosceles triangle: where two of the sides are equal in length
- Equilateral triangle: where all three sides are equal in length
- Scalene triangle: where all three sides are of different lengths
Note that there are other types of triangles (obtuse, acute, etc.), but we
are ignoring those for this assignment.
Triangle class
For the first part of this assignment, you will need to develop a Triangle
class to represent a triangle. For the triangles in this assignment, all
the lengths of the sides are all of type int.
The class should thus include three private int variables, to hold the sides of
the triangle. We will call them a, b, and c in this
assignment (although you can name them to something else).
The class should include the following methods.
- public Triangle()
The default constructor, it sets the triangle's sides to 3, 4, and 5.
Note that because there are no required mutators for this homework, the
constructor can set the fields directly (you can also add a mutator, if you
want - but there won't be a bonus or penalty for either way of doing this).
- public Triangle (int x, int y, int z)
The specific constructor, it sets the sides a,
b, and c of the triangle
respectively to the values of parameters x,
y, and z.
The parameters x, y,
and z should have the property that
x < y
< z. If they are not in sorted
order, then assign the value 1 to a, b, and
c (as that is a valid triangle). Note
that because there are no required mutators for this homework, the
constructor can set the fields directly (you can also add a mutator, if you
want - but there won't be a bonus or penalty for either way of doing this).
- public boolean isTriangle()
This method will return true or
false, depending on whether the values in
the three sides make up a valid triangle. Given that
a, b, and
c are in sorted order (the specific
constructor only accepts them in sorted order), the sides represent a
valid triangle only if a +
b > c.
- public boolean isRight()
This method returns true or
false, depending on whether the triangle is a
right triangle or not. Specifically, it will return true if the sides
form a valid triangle (via a call to the isTriangle() method) AND the sides
fulfill the formula a2+b2=c2.
- public boolean isIsosceles()
This method returns true or
false, depending on whether the triangle is a
isosceles triangle or not. Specifically, it will return true if the
sides form a valid triangle AND two of the sides are equal (it can be any
two). Please make sure to spell this method right! It will cause
issues if you misspell isosceles.
- public boolean isScalene()
This method returns true or
false, depending on whether the triangle is a
scalene triangle or not. Specifically, it will return true if the
sides form a valid triangle AND none of the sides are equal to each other.
- public boolean isEquilateral()
This method returns true or
false, depending on whether the triangle is a
equilateral triangle or not. Specifically, it will return true if the
sides form a valid triangle AND all the sides are of equal length.
- public String toString()
This method will allow you to print out the triangle. Specifically, it
returns a String representation of the triangle. If the triangle has
side lengths of 3, 4, and 5, then this method will return the String
"(3,4,5)" (without the quotes, of course).
TriangleCount
The second part of this assignment is to write the TriangleCount class.
This class will just have the main() method.
First, the main method should initialize a Scanner object, and ask the user
to enter n (an int).
The program will test triangles of all possible sizes x, y, and z from (1,1,1) to (n,n,n).
Thus, if n is 5, then there are 5*5*5 or 125 possible triangles that must
be checked. This will most likely require three nested loops. For each of the triangles, the following steps must be done:
- Check if the sides are in sorted order (i.e.
x < y
< z). If not, then no output
should be provided for that collection of side lengths. This check (to
see if they are in sorted order) should be done in the nested loops, not in
the Triangle class (as the Triangle class will assign the values (1,1,1)).
- Create a new Triangle object using the current side lengths
- Check if it is a valid triangle - if it is not, then no output should be
provided for that collection of side lengths.
- Otherwise, indicate which
properties the triangle possesses (i.e., indicate whether it is a right, isosceles, equilateral, and scalene triangle). Some side length
values will correspond to more than 1 triangle (e.g., (3, 3, 3) is both
isosceles and equilateral), so do not assume that once a property is present, the
others are not.
Sample Run
The following is a sample run of the program. The
red number 5 was supplied by the user.
Enter n: 5
(1,1,1) isosceles equilateral
(1,2,2) isosceles
(1,3,3) isosceles
(1,4,4) isosceles
(1,5,5) isosceles
(2,2,2) isosceles equilateral
(2,2,3) isosceles
(2,3,3) isosceles
(2,3,4) scalene
(2,4,4) isosceles
(2,4,5) scalene
(2,5,5) isosceles
(3,3,3) isosceles equilateral
(3,3,4) isosceles
(3,3,5) isosceles
(3,4,4) isosceles
(3,4,5) right scalene
(3,5,5) isosceles
(4,4,4) isosceles equilateral
(4,4,5) isosceles
(4,5,5) isosceles
(5,5,5) isosceles equilateral
Note that a number of the possible combinations of numbers were not printed.
Any three numbers that are not ordered were not printed (such as 4,3,2).
And any values that are not valid triangles (such as 1,2,3) are also not printed.
The main() method should not print these combinations, as described above.
Also note that each possible triangle will print a triangle type: either all
sides are equal (equilateral/isosceles), 2 sides are equal (isosceles), or no
sides are equal (scalene).
Submission
When you are finished,
submit Triangle.java
and TriangleCount.java.