I wanted to write this program to check type of triangle using either sides or angle inputs. I tried below code which is working perfectly as it should, as in logic and result. But I feel Its redundant and could be shorter with fewer methods.
So I want to know if this program could be shorter ?
import java.util.Scanner;
public class Test123 {
int a;
int b;
int c;
public Test123(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public void determineType() {
if(a >= (b+c) || c >= (b+a) || b >= (a+c) ) {
System.out.println( "Not a Triangle");
} else if(a==b && b==c) {
System.out.println( "Equilateral Triangle");
} else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b) || ((c * c) + (b * b)) == (a * a)) {
System.out.println( "Right Triangle");
} else if(a!=b && b!=c && c!=a) {
System.out.println( "Scalene Triangle" );
} else if ((a==b && b!=c ) || (a!=b && c==a) || (c==b && c!=a)) {
System.out.println( "Isosceles Triangle");
}
}
public void determineTypeA() {
if(a+b+c!=180) {
System.out.println( "Not a Triangle");
} else if(a==b && b==c) {
System.out.println( "Equilateral Triangle");
} else if (a==90||b==90||c==90) {
System.out.println( "Right Triangle");
} else if(a!=b && b!=c && c!=a) {
System.out.println( "Scalene Triangle" );
} else if ((a==b && b!=c ) || (a!=b && c==a) || (c==b && c!=a)) {
System.out.println( "Isosceles Triangle");
}
}
public static void calc() {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
Test123 t = new Test123(a, b, c);
t.determineType();
}
public static void calc2() {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
Test123 t = new Test123(a, b, c);
t.determineTypeA();
}
public static void main(String [] args) {
char choice;
Scanner in = new Scanner(System.in);
System.out.println("Enter 'a' for angle input and 's' for sides input");
choice=in.next().charAt(0);
switch(choice)
{
case 'a'
:System.out.println("Enter the angles of Triangle");
calc2();
break;
case 's'
:System.out.println("Enter the sides of Triangle");
calc();
break;
}
}
}
Please help.