Skip to content
Snippets Groups Projects
Commit 632ee34b authored by Muhammad Ismail's avatar Muhammad Ismail
Browse files

Lab1, part1 uploaded!

parent 8529d6cd
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>part1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
File added
File added
File added
File added
File added
package triangle;
public class InvalidTriangleException extends Exception {
}
package triangle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws InvalidTriangleException {
System.out.print("Enter the triangle side lengths: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
final Triangle triangle = new Triangle();
try {
final String readLine = br.readLine();
System.out.println(readLine);
final String[] split = readLine.split("\\s+");
int[] intSides = new int[split.length];
int i = 0;
for (String string : split) {
try {
intSides[i++] = Integer.parseInt(string);
}catch(NumberFormatException e) {
System.err.println("invalid Input");
}
}
triangle.setSides(intSides);
System.out.println(triangle.getType());
} catch (InvalidTriangleException e) {
System.err.println("invalid triangle");
} catch (IOException e) {
System.err.println("could not read from stdin");
}
}
}
package triangle;
public class Triangle {
public enum TriangleType {
Isosceles, Equilateral, Scalene, NaT;
public String toString() {
return name();
};
}
private int[] sides;
public TriangleType getType() throws InvalidTriangleException
{
TriangleType result = TriangleType.NaT;
if (sides.length != 3) {
//throw new InvalidTriangleException();
return result;
}
if (sides[0] > sides[1])
swap(sides, 0, 1);
if (sides[0] > sides[2])
swap(sides, 0, 2);
if (sides[1] > sides[2])
swap(sides, 1, 2);
if (sides[0] <= 0 || sides[2] - sides[0] >= sides[1]) {
throw new InvalidTriangleException();
}
if (sides[0] == sides[2]) {
result = TriangleType.Equilateral;
} else if (sides[0] == sides[1] || sides[1] == sides[2]) {
result = TriangleType.Isosceles;
} else {
result = TriangleType.Scalene;
}
return result;
}
private static void swap(int s[], int i, int j) {
int tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
public int[] getSides() {
return sides;
}
public void setSides(int[] sides) {
this.sides = sides;
}
}
package triangle;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TriangleTest {
private triangle.Triangle triangle;
@BeforeEach
public void setUp() {
triangle = new triangle.Triangle();
}
@Test
public void testException() throws InvalidTriangleException {
final int[] is = new int[]{1, -1, 1};
triangle.setSides(is);
Assertions.assertThrows(InvalidTriangleException.class, ()->triangle.getType());
}
@Test
public void testEquilateral() throws InvalidTriangleException {
final int[] is = new int[]{1, 1, 1};
triangle.setSides(is);
Assertions.assertEquals(triangle.getType(), Triangle.TriangleType.Equilateral);
}
@Test
public void testIsosceles() throws InvalidTriangleException{
final int[] is = new int[]{3, 3, 2};
triangle.setSides(is);
Assertions.assertEquals(triangle.getType(), Triangle.TriangleType.Isosceles);
}
@Test
public void testScalene() throws InvalidTriangleException{
final int[] is = new int[]{4, 5, 6};
triangle.setSides(is);
Assertions.assertEquals(triangle.getType(), Triangle.TriangleType.Scalene);
}
@Test
public void testNat() throws InvalidTriangleException{
final int[] is = new int[]{1, 1, 2, 5};
triangle.setSides(is);
Assertions.assertEquals(triangle.getType(), Triangle.TriangleType.NaT);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment