# artemkorsakov's solution

## to Triangle in the Java Track

Published at May 18 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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 testSidesThatViolateTriangleInequalityAreNotIsoscelesEvenIfTwoAreEqual_2() throws TriangleException {
expectedException.expect(TriangleException.class);
new Triangle(1, 3, 1);
}

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

@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());
}
}``````
``````import java.util.Arrays;

class Triangle {
private static final double EPSILON = 0.00001;
private double[] sides;

Triangle(double side1, double side2, double side3) throws TriangleException {
sides = new double[]{side1, side2, side3};
Arrays.sort(sides);
checkTriangleInequality();
}

boolean isEquilateral() {
return isDoubleEquals(sides[0], sides[2]);
}

boolean isIsosceles() {
return isDoubleEquals(sides[1], sides[0]) || isDoubleEquals(sides[1], sides[2]);
}

boolean isScalene() {
return !isIsosceles();
}

private void checkTriangleInequality() throws TriangleException {
if (sides[0] <= 0 || sides[0] + sides[1] < sides[2]) {
throw new TriangleException();
}
}

/**
* @return true if the difference between values is less {@value #EPSILON}.
*/
private static boolean isDoubleEquals(double value1, double value2) {
return Math.abs(value1 - value2) < EPSILON;
}
}``````