Browse Source

TriangleCheckerAssignment final changes -no private repository

master
Felix Enhuber 2 years ago
parent
commit
26ba7b4638
2 changed files with 52 additions and 9 deletions
  1. 21
    6
      src/TriangleChecker.java
  2. 31
    3
      test/TriangleCheckerTest.java

+ 21
- 6
src/TriangleChecker.java View File

@@ -1,9 +1,9 @@
import java.util.Scanner;

public class TriangleChecker {
public enum TriangleType {
NONE,
NONE,
NORMAL,
ISOSCELES, // Gleichschenklig
EQUILATERAL // Gleichseitig
@@ -32,23 +32,38 @@ public class TriangleChecker {
switch (type) {
case NONE:
System.out.println("Kein Dreieck");
break;
break;
case NORMAL:
System.out.println("Dreieck");
break;
break;
case ISOSCELES:
System.out.println("Gleichschenkliges Dreieck");
break;
break;
case EQUILATERAL:
System.out.println("Gleichseitiges Dreieck");
break;
break;
}
}

// Analyse der Dreiecksart
public static TriangleType checkTriangle(float a, float b, float c) {

if (checkTriangleShape(a, b, c)) {
if (a == b && a == c) return TriangleType.EQUILATERAL;
if (a == b || a == c || b == c)
return TriangleType.ISOSCELES;
return TriangleType.NORMAL;
}

return TriangleType.NONE;
}

public static boolean checkTriangleShape(float a, float b, float c) {

if (a <= 0 || b <= 0 || c <= 0)
return false;
return a + b > c && a + c > b && b + c > a;

}

}

+ 31
- 3
test/TriangleCheckerTest.java View File

@@ -2,7 +2,7 @@ import org.junit.Test;
import static org.junit.Assert.*;

public class TriangleCheckerTest {
@Test
public void testValidTriangle() {

@@ -10,12 +10,40 @@ public class TriangleCheckerTest {
float a = 2;
float b = 3;
float c = 4;
float d = 32;
float e = 8;
float f = 0;
float g = 6;
float h = 9;
float i = 28;

// Act
TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c);
TriangleChecker.TriangleType normal = TriangleChecker.TriangleType.NORMAL;
TriangleChecker.TriangleType none = TriangleChecker.TriangleType.NONE;
TriangleChecker.TriangleType isoceles = TriangleChecker.TriangleType.ISOSCELES;
TriangleChecker.TriangleType equilateral = TriangleChecker.TriangleType.EQUILATERAL;

assertTrue(TriangleChecker.checkTriangleShape(a, b, c));
assertFalse(TriangleChecker.checkTriangleShape(a, d, c));
assertFalse(TriangleChecker.checkTriangleShape(e, d, e));
assertFalse(TriangleChecker.checkTriangleShape(a, f, h));
assertFalse(TriangleChecker.checkTriangleShape(i, c, d));
assertTrue(TriangleChecker.checkTriangleShape(g, i, d));

// Assert
assertEquals(TriangleChecker.TriangleType.NORMAL, type);
assertEquals(normal, testTriangle(a, b, c));
assertEquals(none, testTriangle(a, b, d));
assertEquals(none, testTriangle(a, f, d));
assertEquals(none, testTriangle(f, f, d));
assertEquals(none, testTriangle(c, c, d));
assertEquals(equilateral, testTriangle(a, a, a));
assertEquals(isoceles, testTriangle(a, d, d));

}

public TriangleChecker.TriangleType testTriangle(float side1, float side2, float side3) {

return TriangleChecker.checkTriangle(side1, side2, side3);

}


Loading…
Cancel
Save