 ## to Triangle in the Java Track

Published at Oct 13 2019 · 0 comments
Instructions
Test suite
Solution

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.

## Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

# Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

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

Use `gradlew.bat` if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore("Remove to run test")` annotation.

## 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 equilateralTrianglesAreAlsoIsosceles() throws TriangleException {
Triangle triangle = new Triangle(4, 4, 4);

assertTrue(triangle.isIsosceles());
}

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

assertFalse(triangle.isIsosceles());
}

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

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

@Ignore("Remove to run test")
@Test
public void thirdTriangleInequalityViolation() 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 allSidesEqualAreNotScalene() throws TriangleException {
Triangle triangle = new Triangle(4, 4, 4);

assertFalse(triangle.isScalene());
}

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

assertFalse(triangle.isScalene());
}

@Ignore("Remove to run test")
@Test
public void mayNotViolateTriangleInequality()
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());
}
}``````
``````class Triangle {

Triangle(double side1, double side2, double side3) throws TriangleException {

if(side1 <= 0 || side2 <= 0 || side3 <= 0)
throw new TriangleException("Triangle must have all sides >=0");
if(side1+side2 < side3 || side1+side3 < side2 || side2+side3 < side1)
throw new TriangleException("Triangle must have the sum of any 2 sides greater or equal the third side");

this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

/**
* An equilateral triangle has all three sides of the same length.
* @return true if equilateral
*/
boolean isEquilateral() {
return (side1 == side2 && side1 == side3);
}

/**
* An isosceles triangle has at least two sides of the same length.
* @return true if isosceles
*/
boolean isIsosceles() {
return (side1 == side2 || side1 == side3 || side2 == side3);
}

/**
* A scalene triangle has all sides of different lengths.
* @return true if scalene
*/
boolean isScalene() {
return ! isIsosceles();
}

double side1, side2, side3;
}``````