 # rootulp's solution

## to Triangle in the Java Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Determine if a triangle is equilateral, isosceles, or scalene.

An equilateral triangle has all three sides the same length.

An isosceles triangle has at least two sides the same length. (It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)

A scalene triangle has all sides of different lengths.

## Note

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. See Triangle Inequality.

## Dig Deeper

The case where the sum of the lengths of two sides equals that of the third is known as a degenerate triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles.

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

## Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### TriangleTest.java

``````import org.junit.Test;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

public class TriangleTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void equilateralTrianglesHaveEqualSides() throws TriangleException {
Triangle triangle = new Triangle(2, 2, 2);

assertTrue(triangle.isEquilateral());
}

@Ignore("Remove to run test")
@Test
public void trianglesWithOneUnequalSideAreNotEquilateral() throws TriangleException {
Triangle triangle = new Triangle(2, 3, 2);

assertFalse(triangle.isEquilateral());
}

@Ignore("Remove to run test")
@Test
public void trianglesWithNoEqualSidesAreNotEquilateral() throws TriangleException {
Triangle triangle = new Triangle(5, 4, 6);

assertFalse(triangle.isEquilateral());
}

@Ignore("Remove to run test")
@Test
public void trianglesWithNoSizeAreIllegal() throws TriangleException {
expectedException.expect(TriangleException.class);
new Triangle(0, 0, 0);
}

@Ignore("Remove to run test")
@Test
public void verySmallTrianglesCanBeEquilateral() throws TriangleException {
Triangle triangle = new Triangle(0.5, 0.5, 0.5);

assertTrue(triangle.isEquilateral());
}

@Ignore("Remove to run test")
@Test
public void isoscelesTrianglesHaveLastTwoSidesEqual() throws TriangleException {
Triangle triangle = new Triangle(3, 4, 4);

assertTrue(triangle.isIsosceles());
}

@Ignore("Remove to run test")
@Test
public void isoscelesTrianglesHaveTwoFirstSidesEqual() throws TriangleException {
Triangle triangle = new Triangle(4, 4, 3);

assertTrue(triangle.isIsosceles());
}

@Ignore("Remove to run test")
@Test
public void isoscelesTrianglesHaveFirstAndLastSidesEqual() throws TriangleException {
Triangle triangle = new Triangle(4, 3, 4);

assertTrue(triangle.isIsosceles());
}

@Ignore("Remove to run test")
@Test
public void isoscelesTrianglesCanHaveAllSidesEqual() throws TriangleException {
Triangle triangle = new Triangle(4, 4, 4);

assertTrue(triangle.isIsosceles());
}

@Ignore("Remove to run test")
@Test
public void isoscelesTrianglesMustHaveAtLeastTwoEqualSides() throws TriangleException {
Triangle triangle = new Triangle(2, 3, 4);

assertFalse(triangle.isIsosceles());
}

@Ignore("Remove to run test")
@Test
public void testSidesThatViolateTriangleInequalityAreNotIsoscelesEvenIfTwoAreEqual() throws TriangleException {
expectedException.expect(TriangleException.class);
new Triangle(1, 1, 3);
}

@Ignore("Remove to run test")
@Test
public void verySmallTrianglesCanBeIsosceles() throws TriangleException {
Triangle triangle = new Triangle(0.5, 0.4, 0.5);

assertTrue(triangle.isIsosceles());
}

@Ignore("Remove to run test")
@Test
public void scaleneTrianglesHaveNoEqualSides() throws TriangleException {
Triangle triangle = new Triangle(5, 4, 6);

assertTrue(triangle.isScalene());
}

@Ignore("Remove to run test")
@Test
public void trianglesWithAllSidesEqualAreNotScalene() throws TriangleException {
Triangle triangle = new Triangle(4, 4, 4);

assertFalse(triangle.isScalene());
}

@Ignore("Remove to run test")
@Test
public void trianglesWithOneUnequalSideAreNotScalene() throws TriangleException {
Triangle triangle = new Triangle(4, 4, 3);

assertFalse(triangle.isScalene());
}

@Ignore("Remove to run test")
@Test
public void testSidesThatViolateTriangleInequalityAreNotScaleneEvenIfTheyAreAllDifferent() throws TriangleException {
expectedException.expect(TriangleException.class);
new Triangle(7, 3, 2);
}

@Ignore("Remove to run test")
@Test
public void verySmallTrianglesCanBeScalene() throws TriangleException {
Triangle triangle = new Triangle(0.5, 0.4, 0.6);

assertTrue(triangle.isScalene());
}
}``````
``````public class Triangle {

private final double sideA;
private final double sideB;
private final double sideC;

public Triangle(double sideA, double sideB, double sideC) throws TriangleException {
if (triangleInvalid(sideA, sideB, sideC)) { throw new TriangleException(); }

this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}

public TriangleKind getKind() {
if (sideA == sideB && sideB == sideC && sideA == sideC) {
return TriangleKind.EQUILATERAL;
} else if (sideA == sideB || sideA == sideC || sideB == sideC) {
return TriangleKind.ISOSCELES;
} else {
return TriangleKind.SCALENE;
}
}

private boolean triangleInvalid(double sideA, double sideB, double sideC) {
return triangleInequality(sideA, sideB, sideC) ||
triangleInequality(sideC, sideB, sideA) ||
triangleInequality(sideA, sideC, sideB);
}

private boolean triangleInequality(double side1, double side2, double side3) {
return (side1 + side2 <= side3);
}

}``````