In: Computer Science
/* * Assignment: #3 * Topic: Identifying Triangles * Author: <YOUR NAME> */ package edu.depaul.triangle; import static edu.depaul.triangle.TriangleType.EQUILATERAL; import static edu.depaul.triangle.TriangleType.ISOSCELES; import static edu.depaul.triangle.TriangleType.SCALENE; import java.util.Scanner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * A class to classify a set of side lengths as one of the 3 types * of triangle: equilateral, isosceles, or scalene. * * You should not be able to create an invalid Triangle. The * constructor throws an IllegalArgumentException if the input * cannot be interpreted as a triangle for any reason. */ public class Triangle { private static final Logger logger = LoggerFactory.getLogger(Triangle.class); private int[] sides = new int[3]; /** * Define as private so that it is not a valid * choice. */ private Triangle() {} public Triangle(String[] args) { validateArgs(args); parseArgs(args); validateLength(); } public TriangleType classify() { TriangleType result; if ((sides[0] == sides[1]) && (sides[1] == sides[2])) { result = EQUILATERAL; } else if ((sides[0] == sides[1]) || (sides[1] == sides[2])) { result = ISOSCELES; } else { result = SCALENE; } logger.debug("classified as: " + result); return result; } private void parseArgs(String[] args) { // throws IllegalArgumentException on a failed parse for (int i = 0; i < args.length; i++) { sides[i] = Integer.parseInt(args[i]); } } private void validateArgs(String[] args) { if ((args == null) || (args.length != 3)) { throw new IllegalArgumentException("Must have 3 elements"); } } private void validateLength() { for (int i = 0; i < sides.length; i++) { if (sides[i] > 400) { throw new IllegalArgumentException("max size is 400"); } } } }
/* * Assignment: #3 * Topic: Identifying Triangles * Author: <YOUR NAME> */ package edu.depaul.triangle; import static org.junit.jupiter.api.Assertions.*; import static edu.depaul.triangle.TriangleType.ISOSCELES; import static edu.depaul.triangle.TriangleType.EQUILATERAL; import static edu.depaul.triangle.TriangleType.SCALENE; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class TriangleTest { @Test @DisplayName("Remove me and add proper tests") void testPlaceHolder() { } }
Need to write tests to see if the program works correctly. (using assert)
Here's the Another easy code for identify Triangle Type
import java.util.*;
public class MainClass {
public static void main(String[] args) {
int sides[] = new int[3];
for(int i = 0; i<sides.length; i++){
if(i == 0){
System.out.println("What is the first side Measure?");
Scanner side1 = new Scanner(System.in);
sides[i] = side1.nextInt();
}
else if(i == 1){
System.out.println("What is the second side Measure?");
Scanner side2 = new Scanner(System.in);
sides[i] = side2.nextInt();
}
else{
System.out.println("What is the third side Measure?");
Scanner side3 = new Scanner(System.in);
sides[i] = side3.nextInt();
}
}
if(sides[0] == sides[1] && sides[1] == sides[2]){
System.out.println("It's an equilateral triangle.");
}
else if(sides[0] == sides[1] || sides[1] == sides[2] || sides[0] ==
sides[2]){
System.out.println("It's an isosceles triangle.");
}
else{
System.out.println("It's a scalene triangle.");
}
}
}