Problem 2.2

CIS 210 Home Page Last updated 2007/09/10 14:03:20

Problem 2.2 - Validating dimensions of a triangle

In this problem you will write a small program in which you will code the logic to determine if three given dimensions can be the sides of a triangle. This program will only require the use of Java expressions and if-else statements. You will not need, and are not permitted to use looping constructs or library classes.

Problem Statement

A triangle can be uniquely described by the lengths of the three sides. However, not every set of three dimensions can describe a triangle. In particular, in a triangle, the sum of the lengths of any two sides must be greater than the remaining side. For example, if we have lengths 1, 4, and 2, this does not describe a triangle since 1+2 is not larger than 4. On the other hand, if we have lengths 2, 3, and 4, this does describe a triangle since you cannot find a pair whose sum is not larger than the other side.

The perimeter of a triangle is the sum of the lengths of the sides.

For this problem, you must write the code for a method that calculates the perimeter of a triangle described by three dimensions. You must first validate the dimensions, using the rule above (and also checking that the dimensions are positive). If the dimensions are valid, then the method will return the perimeter; otherwise it will return zero.

Starting code

Code to get you started is provided in Triangle.java. The main method has code that prompts for the three dimensions, which will be realized as three precision numerical values. Although we will cover methods more completely next week, your code for this assignment must be in the method getPerimeter. This will make it easier for us to test and grade your code accurately. You must fill in the logic for the method getPerimeter.

You should not change the main method.

What to turn in

Turn in your code for Triangle.java that has the method getPerimeter filled in according to the constraints described above. Make sure you comment your code and indent it properly.