public class Triangle extends Object {
// Include some appropriate fields. These should not be public.
// Your triangles are defined by side lengths, which are positive ints.
// Put your own Javadoc comments for all methods please.
// Here a, b, c should be the side lengths.
// Throw an IllegalArgumentException if any of the following happen:
// a, b, c are not all positive
// a, b, c do not obey the triangle inequality.
private static int a;
private static int b;
private static int c;
public Triangle (int a, int b, int c) throws IllegalArgumentException {
if (isTriangle (a,b,c)==false) throw new IllegalArgumentException("Not a Triangle!");
Triangle.a = a;
Triangle.b = b;
Triangle.c = c;
}
public static int[ ] getSideLengths ( ) {
Triangle T1 = new Triangle(1,1,1);
Triangle T2 = new Triangle(1,1,1);
System.out.println("Please enter the six inputs, each followed by hitting the enter key.");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = null;
try {
input = br.readLine();
T1.a = Integer.parseInt(input);
System.out.println("T1.a = " + T1.a);
input = br.readLine();
T1.b = Integer.parseInt(input);
System.out.println("T1.b = " + T1.b);
input = br.readLine();
T1.c = Integer.parseInt(input);
System.out.println("T1.c = " + T1.c);
input = br.readLine();
T2.a = Integer.parseInt(input);
System.out.println("T2.a = " + T2.a);
input = br.readLine();
T2.b = Integer.parseInt(input);
System.out.println("T2.b = " + T2.b);
input = br.readLine();
T2.c = Integer.parseInt(input);
System.out.println("T2.c = " + T2.c);
}
catch (IOException e) {
System.out.println("Error!");
System.exit(1);
}
int[] sidelengths = {T1.a, T1.b, T1.c, T2.a, T2.b, T2.c};
for (int i=0; i<sidelengths.length; i++) {
System.out.print(sidelengths[i] + " , ");
}
return sidelengths;
}
/*public boolean STriangle (int a, int b, int c){
boolean striangle = false;
if (a==b && b==c) striangle = true;
return striangle;
}*/
// We explicitly won't be using the following getAngles method.
// Please DON'T implement it.
// Using angles to test for similarity is risky, since the values you
// get back may be subject to rounding error.
//
// return the 3 angles, in radians. These should sum to 3.14159...
// public double[ ] getAngles ( ) {
// }
// do not include any setter methods.
// Triangles are not mutable.
// true if the triangle contains a right angle.
public static boolean isRight (int a, int b, int c) {
if ((a*a)+(b*b)==(c*c)) return true;
if ((b*b)+(c*c)==(a*a)) return true;
if ((a*a)+(c*c)==(b*b)) return true;
else return false;
/* formula for a right triangle
*
*/
}
// true if the triangle contains an angle greater than 90 degrees.
public static boolean isObtuse (int a, int b, int c ) {
if ((a*a)+(b*b)<(c*c)) return true;
else return false;
/* formula for an obtuse triangle
*
*/
}
// true if the triangle is not right or obtuse.
public static boolean isAcute ( int a, int b, int c) {
if ((a*a)+(b*b)>(c*c)) return true;
else return false;
/* formula for an acute triangle
*
*/
}
// true if a,b,c are the side lengths for a legitimate triangle.
// useful for testing arguments you want to pass to the constructor.
public static boolean isTriangle (int a, int b, int c) {
if (a+b<c || a<=0) return false;
if (a+c<b || b<=0) return false;
if (b+c<a || c<=0) return false;
else return true;
}
// always want one of these.
public static String toString(Triangle d) {
return "(" +d.a+" ,"+ d.b+" ,"+ d.c+ ")";
}
// This is the method you'll need in part 2.
// @Override
// public boolean equals (Object o) {
// }
public static void main(String [] args){
Triangle T1 = new Triangle(1,1,1);
Triangle T2 = new Triangle(1,1,1);
int[] a1 = (getSideLengths());
/*test*/ System.out.println("To String - " + toString(T1));
if (T1.isTriangle(a, b, c)==true){
System.out.println("The first one is a triangle");
System.out.print("The second one is ");
if (T1.isTriangle(a, b, c)==false) System.out.print("not ");
System.out.println("a triangle");
if (T1.isAcute(a, b, c)==true && T1.isRight(a, b, c)==false) System.out.println("The first one is an acute triangle");
if (T2.isAcute(a, b, c)==true && T2.isRight(a, b, c)==false) System.out.println("The second one is an acute triangle");
if (T1.isObtuse(a, b, c)==true && T1.isRight(a, b, c)==false) System.out.println("The first one is an obtuse triangle");
if (T2.isObtuse(a, b, c)==true && T2.isRight(a, b, c)==false) System.out.println("The second one is an obtuse triangle");
if (T1.isRight(a, b, c)==true) System.out.println("The first one is a right triangle");
if (T2.isRight(a, b, c)==true) System.out.println("The second one is a right triangle");
if (Triangle.CTriangle(a, b, c, a, b, c)==true) {
System.out.println("Those are congruent triangles");
System.out.println("Those are similar triangles");
}
else if (Triangle.STriangle(a, b, c, a, b, c)==true) System.out.println("Those are similar triangles");
}
else System.out.println("The first one is not a triangle");
}
}