Browse Source

Final commit and push

master
JDenu 2 years ago
parent
commit
d5fe7053e8
2 changed files with 75 additions and 9 deletions
  1. 30
    8
      src/TriangleChecker.java
  2. 45
    1
      test/TriangleCheckerTest.java

+ 30
- 8
src/TriangleChecker.java View File

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

public class TriangleChecker {
public enum TriangleType {
NONE,
NONE,
NORMAL,
ISOSCELES, // Gleichschenklig
EQUILATERAL // Gleichseitig
@@ -32,23 +33,44 @@ 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) {
//find Hypotenuse
float[] tmp = {a, b, c};
Arrays.sort(tmp);
float hypo = tmp[2];
float a1 = tmp[0];
float b1 = tmp[1];


//normal
if (a1 + b1 > hypo) {

//gleichseitig
if (a1 + b1 + hypo == 3 * a1)
return TriangleType.EQUILATERAL;

//gleichschenklig
if (a1 == b1 || b1 == hypo || a1 == hypo)
return TriangleType.ISOSCELES;

return TriangleType.NORMAL;

}
return TriangleType.NONE;
}


}
}

+ 45
- 1
test/TriangleCheckerTest.java View File

@@ -1,8 +1,11 @@
import org.junit.Test;

import java.util.Arrays;

import static org.junit.Assert.*;

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

@@ -18,5 +21,46 @@ public class TriangleCheckerTest {
assertEquals(TriangleChecker.TriangleType.NORMAL, 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 testIsocelesTriangle() {

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

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

// Assert
assertEquals(TriangleChecker.TriangleType.ISOSCELES, type);
}
@Test
public void testNoTriangle() {

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

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

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

Loading…
Cancel
Save