Homework J5: Triangulation

Due: Friday, 1 April 2005 by 10 a.m.



Home | Resources | Homeworks | Slides | Labs | Contacts | Submit | Forums | Private

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

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.

 

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:

 

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.