// // triangle // // Author : K.J. Chan // Date : Novermber 2008 // Language : Java // Description: triangle class // class triangle { ////////////////////////////////////////// // Declarations ////////////////////////////////////////// private double small ; private double medium ; private double large ; final int NOT_A_TRIANGLE = 0 ; final int EQUILATERAL = 1 ; final int ISOSCELES = 2 ; final int RIGHT_ANGLE = 3 ; final int SCALENE = 4 ; ////////////////////////////////////////// // Constructor ////////////////////////////////////////// public triangle( double side1, double side2, double side3 ) { double temp ; ////////////////////// // sort numbers out into order // set them to small, medium and large // output the results (for testing) ////////////////////// if ( side1 > side2 ) { temp = side1 ; side1 = side2 ; side2 = temp ; } if ( side2 > side3 ) { temp = side2 ; side2 = side3 ; side3 = temp ; } if ( side1 > side2 ) { temp = side1 ; side1 = side2 ; side2 = temp ; } small = side1 ; medium = side2 ; large = side3 ; } ////////////////////////////////////////// // method: what_triangle_type // work out what sort of triangle we have ////////////////////////////////////////// public int get_triangletype() { if ( large < medium+small ) { ////////////////////////////////////////// // we have a triangle ////////////////////////////////////////// if ( large == small ) return( EQUILATERAL ) ; else if ( (small == medium) || (medium == large) ) return( ISOSCELES ) ; else if ( large*large == medium*medium + small*small ) return( RIGHT_ANGLE ) ; else return( SCALENE ) ; } else { ////////////////////////////////////////// // we DO NOT have a triangle ////////////////////////////////////////// return( NOT_A_TRIANGLE ) ; } } }