patternjavaMinor
Determining triangle type from three integer inputs
Viewed 0 times
threeinputsdeterminingtypetrianglefrominteger
Problem
The logic is simple, but I was wondering if anyone could help with rewriting the conditional logic. Also, the only error conditions I can think of are the sides should not be equal to or less than zero. Are there any other error conditions that seem to occur to you?
public class TriangleType {
static Triangle getType(int a, int b, int c)
{
if(a<=0||b<=0||c<=0)
throw new IllegalArgumentException("Length of sides cannot be equal to or less than zero");
if(a==b&&b==c&&c==a)
return Triangle.EQUILATERAL;
else if((a==b)||(b==c)||(c==a))
return Triangle.ISOSCELES;
else if(a!=b&&b!=c&&c!=a)
return Triangle.SCALENE;
else
return Triangle.ERROR;
}
public static void main(String[] args)
{
System.out.println(TriangleType.getType(13, 13, 0));
}
}
enum Triangle
{
ISOSCELES(0),
EQUILATERAL(1),
SCALENE(2),
ERROR(3);
private int n;
Triangle(int n)
{this.n = n;}
}Solution
You could encapsulate the conditions in a
This would vastly simplify your enum selection code, which I would put directly into the enum:
PotentialTriangle class. Note that the fields are of type long to avoid overflow when calculating whether triangle inequality is violated.public class PotentialTriangle {
private final long a, b, c;
public PotentialTriangle(int sideA, int sideB, int sideC) {
a = sideA;
b = sideB;
c = sideC;
}
public boolean isAnySideTooShort() {
return a b + c || b > a + c || c > a + b;
}
public boolean areSidesEqual() {
return a == b && b == c;
}
public boolean areAtLeastTwoSidesEqual() {
return a == b || b == c || c == a;
}
}This would vastly simplify your enum selection code, which I would put directly into the enum:
public enum TriangleType {
ISOSCELES,
EQUILATERAL,
SCALENE;
public static TriangleType ofPotentialTriangle(PotentialTriangle triangle) {
throwIf(triangle.isAnySideTooShort(),
"Length of sides cannot be equal to or less than zero");
throwIf(triangle.violatesTriangleInequality(),
"Sum of any two sides must be larger than the remaining side");
if (triangle.areSidesEqual()) {
return EQUILATERAL;
}
if (triangle.areAtLeastTwoSidesEqual()) {
return ISOSCELES;
}
return SCALENE;
}
private static void throwIf(boolean condition, String message) {
if (condition) {
throw new IllegalArgumentException(message);
}
}
}Code Snippets
public class PotentialTriangle {
private final long a, b, c;
public PotentialTriangle(int sideA, int sideB, int sideC) {
a = sideA;
b = sideB;
c = sideC;
}
public boolean isAnySideTooShort() {
return a <= 0 || b <= 0 || c <= 0;
}
public boolean violatesTriangleInequality() {
return a > b + c || b > a + c || c > a + b;
}
public boolean areSidesEqual() {
return a == b && b == c;
}
public boolean areAtLeastTwoSidesEqual() {
return a == b || b == c || c == a;
}
}public enum TriangleType {
ISOSCELES,
EQUILATERAL,
SCALENE;
public static TriangleType ofPotentialTriangle(PotentialTriangle triangle) {
throwIf(triangle.isAnySideTooShort(),
"Length of sides cannot be equal to or less than zero");
throwIf(triangle.violatesTriangleInequality(),
"Sum of any two sides must be larger than the remaining side");
if (triangle.areSidesEqual()) {
return EQUILATERAL;
}
if (triangle.areAtLeastTwoSidesEqual()) {
return ISOSCELES;
}
return SCALENE;
}
private static void throwIf(boolean condition, String message) {
if (condition) {
throw new IllegalArgumentException(message);
}
}
}Context
StackExchange Code Review Q#18463, answer score: 6
Revisions (0)
No revisions yet.