-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangle.java
89 lines (82 loc) · 2.1 KB
/
Triangle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ecse428Project;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Triangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter sideA");
String sideA = sc.nextLine();
System.out.println("Please enter sideB");
String sideB = sc.nextLine();
System.out.println("Please enter sideC");
String sideC = sc.nextLine();
if(isANumber(sideA) && isANumber(sideB) && isANumber(sideC)){
int[] triangle = new int[3];
triangle = toNumber(sideA,sideB,sideC);
int result = decide(triangle);
System.out.println(printResult (result));
}
else {
System.out.println("Invalid type");
}
}
public static String printResult (int category) {
String result = "";
switch(category) {
case 1:
result ="Equilateral";
break;
case 2:
result ="Isosceles";
break;
case 3:
result ="Scalene";
break;
case 4:
result ="Not A Triangle";
break;
case 5:
result ="Out of range";
break;
}
return result;
}
public static int decide(int sides[]){
int result = 0;
int sideA = sides[0];
int sideB = sides[1];
int sideC = sides[2];
if(((sideA<=0)||(sideA>100))||((sideB<=0)||(sideB>100))||((sideC<=0)||(sideC>100))) {
result = 5;
}
else if (((sideA + sideB) <= sideC)||((sideB + sideC) <= sideA)||((sideA + sideC) <= sideB)) {
result = 4;
}
else if (sideA == sideB && sideB ==sideC) {
result = 1;
}
else if((sideA == sideB) || (sideB ==sideC) || (sideC == sideA)) {
result = 2;
}
else {
result = 3;
}
return result;
}
public static boolean isANumber(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
public static int[] toNumber(String A,String B,String C) {
int[] result = new int[3];
result[0] = Integer.parseInt(A);
result[1] = Integer.parseInt(B);
result[2] = Integer.parseInt(C);
return result;
}
}