38 lines
1.2 KiB
Java
38 lines
1.2 KiB
Java
package ch.usi.sqt;
|
|
|
|
public class TriangleClassifier {
|
|
|
|
/**
|
|
* This method takes as input three integers that represent the three sides
|
|
* of a potential triangle. It returns 1 if the triangle is equilateral, 2 if
|
|
* it is isosceles, 3 if it is scalene, or 4 if it is not a triangle.
|
|
* @param a must be greater than zero
|
|
* @param b must be positive
|
|
* @param c cannot be less than or equal to zero
|
|
* @throws IllegalArgumentException if a<=0, or b is less than 1, or c is zero or less than zero
|
|
* @return a value greater than zero
|
|
*/
|
|
public static int classify(int a, int b, int c) {
|
|
if (a <= 0 || b <= 0 || c < 0) {
|
|
throw new IllegalArgumentException("All sides must be greater than zero");
|
|
}
|
|
|
|
if (a == b && b == c) {
|
|
return 1; // Equilateral
|
|
}
|
|
|
|
int max = Math.max(a, Math.max(b, c));
|
|
|
|
if ((max == a && max - b - c >= 0) ||
|
|
(max == b && max - a - c >= 0) ||
|
|
(max == c && max - a - b >= 0)) {
|
|
return 4; // Not a triangle
|
|
}
|
|
|
|
if (a == b || b == c || a == c) {
|
|
return 2; // Isosceles
|
|
} else {
|
|
return 3; // Scalene
|
|
}
|
|
}
|
|
} |