Browse Source

Check now working for all kinds of triangles.

Including Tests for all Checks.
master
Frederik Hagen 2 years ago
parent
commit
cb1c503016

+ 10
- 0
.idea/libraries/jars.xml View File

@@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="jars">
<CLASSES>
<root url="jar://$PROJECT_DIR$/jars/hamcrest-core-1.3.jar!/" />
<root url="jar://$PROJECT_DIR$/jars/junit-4.12.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

+ 6
- 0
.idea/misc.xml View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

+ 8
- 0
.idea/modules.xml View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/TriangleCheckerAssignment.iml" filepath="$PROJECT_DIR$/TriangleCheckerAssignment.iml" />
</modules>
</component>
</project>

+ 6
- 0
.idea/vcs.xml View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

+ 13
- 0
TriangleCheckerAssignment.iml View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jars" level="project" />
</component>
</module>

+ 68
- 46
src/TriangleChecker.java View File

@@ -1,54 +1,76 @@
import java.util.Arrays;
import java.util.Scanner;

public class TriangleChecker {
public enum TriangleType {
NONE,
NORMAL,
ISOSCELES, // Gleichschenklig
EQUILATERAL // Gleichseitig
}


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Geben Sie die Seitenlängen ein.");
float a = enterFloat(s, "a: ");
float b = enterFloat(s, "b: ");
float c = enterFloat(s, "c: ");
s.close();
printAnalysis(a, b, c);
}

// Eingabe einer Seitenlänge
private static float enterFloat(Scanner s, String prompt) {
System.out.print(prompt);
return s.nextFloat();
}

// Ausgabe der ermittelten Dreiecksart
private static void printAnalysis(float a, float b, float c) {
TriangleType type = checkTriangle(a, b, c);
switch (type) {
case NONE:
System.out.println("Kein Dreieck");
break;
case NORMAL:
System.out.println("Dreieck");
break;
case ISOSCELES:
System.out.println("Gleichschenkliges Dreieck");
break;
case EQUILATERAL:
System.out.println("Gleichseitiges Dreieck");
break;

public enum TriangleType {
NONE,
NORMAL,
ISOSCELES, // Gleichschenklig
EQUILATERAL // Gleichseitig
}


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Geben Sie die Seitenlängen ein.");
float a = enterFloat(s, "a: ");
float b = enterFloat(s, "b: ");
float c = enterFloat(s, "c: ");
s.close();
printAnalysis(a, b, c);
}

// Eingabe einer Seitenlänge
private static float enterFloat(Scanner s, String prompt) {
System.out.print(prompt);
return s.nextFloat();
}

// Ausgabe der ermittelten Dreiecksart
private static void printAnalysis(float a, float b, float c) {
TriangleType type = checkTriangle(a, b, c);
switch (type) {
case NONE:
System.out.println("Kein Dreieck");
break;
case NORMAL:
System.out.println("Dreieck");
break;
case ISOSCELES:
System.out.println("Gleichschenkliges Dreieck");
break;
case EQUILATERAL:
System.out.println("Gleichseitiges Dreieck");
break;
}
}
}

// Analyse der Dreiecksart
public static TriangleType checkTriangle(float a, float b, float c) {
return TriangleType.NONE;
}
// 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;
}

}

+ 46
- 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,47 @@ 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