new program

This commit is contained in:
Johannes Weichelt 2021-12-24 12:01:01 +01:00
parent 0fa3277440
commit 8fd57fc6c6
2 changed files with 75 additions and 13 deletions

View File

@ -1,9 +1,9 @@
import java.util.Scanner; import java.util.Scanner;
public class TriangleChecker { public class TriangleChecker {
public enum TriangleType { public enum TriangleType {
NONE, NONE,
NORMAL, NORMAL,
ISOSCELES, // Gleichschenklig ISOSCELES, // Gleichschenklig
EQUILATERAL // Gleichseitig EQUILATERAL // Gleichseitig
@ -32,21 +32,34 @@ public class TriangleChecker {
switch (type) { switch (type) {
case NONE: case NONE:
System.out.println("Kein Dreieck"); System.out.println("Kein Dreieck");
break; break;
case NORMAL: case NORMAL:
System.out.println("Dreieck"); System.out.println("Dreieck");
break; break;
case ISOSCELES: case ISOSCELES:
System.out.println("Gleichschenkliges Dreieck"); System.out.println("Gleichschenkliges Dreieck");
break; break;
case EQUILATERAL: case EQUILATERAL:
System.out.println("Gleichseitiges Dreieck"); System.out.println("Gleichseitiges Dreieck");
break; break;
} }
} }
// Analyse der Dreiecksart // Analyse der Dreiecksart
public static TriangleType checkTriangle(float a, float b, float c) { public static TriangleType checkTriangle(float a, float b, float c) {
return TriangleType.NORMAL;
if (checkTriangleBasePossibility(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 checkTriangleBasePossibility(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;
}
} }

View File

@ -2,14 +2,14 @@ import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class TriangleCheckerTest { public class TriangleCheckerTest {
@Test @Test
public void testValidTriangle() { public void testIsTriangle() {
// Arrange // Arrange
float a = 7; float a = 3;
float b = 3; float b = 2;
float c = 1; float c = 4;
// Act // Act
TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c); TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c);
@ -17,6 +17,55 @@ public class TriangleCheckerTest {
// Assert // Assert
assertEquals(TriangleChecker.TriangleType.NORMAL, type); assertEquals(TriangleChecker.TriangleType.NORMAL, type);
// Arrange
a = -2;
b = 5;
c = 2;
// Act
type = TriangleChecker.checkTriangle(a, b, c);
// Assert
assertEquals(TriangleChecker.TriangleType.NONE, type);
// Arrange
a = 5;
b = 5;
c = 10;
// Act
type = TriangleChecker.checkTriangle(a, b, c);
// Assert
assertEquals(TriangleChecker.TriangleType.NONE, type);
} }
} @Test
public void testIsoTriangle(){
// Arrange
float a = 6;
float b = 6;
float c = 3;
// Act
TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c);
// Assert
assertEquals(TriangleChecker.TriangleType.ISOSCELES, type);
}
@Test
public void testEqualTriangle(){
// Arrange
float a = 5;
float b = 5;
float c = 5;
// Act
TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c);
// Assert
assertEquals(TriangleChecker.TriangleType.EQUILATERAL, type);
}
}