Browse Source

Triangle Checker

master
Sarah Pastors 2 years ago
parent
commit
003ab4d79d
2 changed files with 92 additions and 8 deletions
  1. 24
    7
      src/TriangleChecker.java
  2. 68
    1
      test/TriangleCheckerTest.java

+ 24
- 7
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,40 @@ 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 (a <= 0 || b <= 0 || c <= 0)
return TriangleType.NONE;

if (a + b > c && b + c > a && a + c > b) {
if (a == b && b == c) {
return TriangleType.EQUILATERAL;
}
if (a == b || a == c || b == c) {
return TriangleType.ISOSCELES;
}

return TriangleType.NORMAL;
}

return TriangleType.NONE;

}

}



}

+ 68
- 1
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() {

@@ -19,4 +19,71 @@ public class TriangleCheckerTest {

}

@Test
public void testUnvalidTriangle() {

// Arrange
float a = 1;
float b = 1;
float c = 100;

// Act
TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c);

// Assert
assertEquals(TriangleChecker.TriangleType.NONE, type);

}

@Test
public void testNegativeTriangle() {

// Arrange
float a = -2;
float b = 2;
float c = 3;

// Act
TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c);

// Assert
assertEquals(TriangleChecker.TriangleType.NONE, type);

}

@Test
public void testEquilateralTriangle() {

// Arrange
float a = 2;
float b = 2;
float c = 2;

// Act
TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c);

// Assert
assertEquals(TriangleChecker.TriangleType.EQUILATERAL, type);

}

@Test
public void testIsoscelesTriangle() {

// Arrange
float a = 3;
float b = 3;
float c = 4;

// Act
TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c);

// Assert
assertEquals(TriangleChecker.TriangleType.ISOSCELES, type);

}




}

Loading…
Cancel
Save