Sha256: df2273eb107f86b631c32f38bb1cce1cded38f785e7295e7167a21db24a83eaf

Contents?: true

Size: 1.3 KB

Versions: 132

Compression:

Stored size: 1.3 KB

Contents

import java.util.HashSet;
import java.util.Set;

class Triangle {

    private double side1;
    private double side2;
    private double side3;
    private int uniqueSides;

    Triangle(double side1, double side2, double side3) throws TriangleException {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;

        if (allSidesAreZero() || hasImpossibleSides() || violatesTriangleInequality()) {
            throw new TriangleException();
        }

        this.uniqueSides = getNumberOfUniqueSides();
    }

    boolean isEquilateral() {
        return uniqueSides == 1;
    }

    boolean isIsosceles() {
        return (uniqueSides == 2 || isEquilateral());
    }

    boolean isScalene() {
        return uniqueSides == 3;
    }

    private boolean allSidesAreZero() {
        return side1 == 0 && side2 == 0 && side3 == 0;
    }

    private boolean hasImpossibleSides() {
        return side1 < 0 || side2 < 0 || side3 < 0;
    }

    private boolean violatesTriangleInequality() {
        return side1 + side2 <= side3 || side1 + side3 <= side2 || side2 + side3 <= side1;
    }

    private int getNumberOfUniqueSides() {
        Set<Double> sides = new HashSet<>();

        sides.add(side1);
        sides.add(side2);
        sides.add(side3);

        return sides.size();
    }

}

Version data entries

132 entries across 132 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.97 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.96 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.95 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.94 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.93 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.92 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.91 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.90 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.89 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.88 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.87 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.86 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.85 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.84 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.83 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.82 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.81 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.80 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java
trackler-2.2.1.79 tracks/java/exercises/triangle/.meta/src/reference/java/Triangle.java