import java.io.*;
//---------------------------------------------------------------------------------------------
// CS100 P2: Q3
//
// Prompts user for the sides of a triangle. Creates an object of class Triangle
// from the user's input values, then prints the kind of triangle the values
// present. Prints an error message if values are illegal or if they do not
// form a valid triangle.
//
// Author : Alan Renaud (ajr5@cornell.edu)
// Date : 9 July 1999
//---------------------------------------------------------------------------------------------
public class TestTriangle {
//---------------------------------------------------------------------------------------------
// getInput
//
// input : a prompt message
// return : the value input by user
//
// Prompts user for input and returns the value input.
//---------------------------------------------------------------------------------------------
private static int getInput(String message)
{
TokenReader in = new TokenReader(System.in);
System.out.print(message);
System.out.flush();
return in.readInt();
}
//---------------------------------------------------------------------------------------------
//
// initTriangle
//
// input : none
// return : object of class Triangle initialized by user
//
// Prompts the user for three triangle sides; returns an instance of class
// Triangle initialized by the user's input values.
//--------------------------------------------------------------------------------
private static Triangle initTriangle()
{
int s1 = getInput("What is the length of the first side of your triangle? ");
int s2 = getInput("What is the length of the second side of your triangle? ");
int s3 = getInput("What is the length of the third side of your triangle? ");
Triangle tri = new Triangle(s1, s2, s3);
return tri;
}
//---------------------------------------------------------------------------------------------
// printOutput
//
// input : object of class Triangle
// return : none
//
// Output to screen the kind of triangle the object's instance variables comprise.
//---------------------------------------------------------------------------------------------
private static void printOutput(Triangle tri)
{
TokenReader in = new TokenReader(System.in);
if (tri.isTriangle())
{
System.out.print("Pondering.... ");
if (tri.isRight()) System.out.println("Your triangle is a right triangle!") ;
if (tri.isEquilateral()) System.out.println("Your triangle is equilateral!");
if ((tri.isScalene())&&(!tri.isRight())) System.out.println("Your triangle is scalene!");
if (tri.isIsosceles()) System.out.println("Your triangle is isosceles!");
}
in.waitUntilEnter();
}
public static void main (String args[])
{
Triangle userTri = initTriangle();
printOutput(userTri);
}
}
//---------------------------------------------------------------------------------------------
//
// ClassTriangle accepts only integer inputs and contains three instance variables
// representing the three sides of a triangle. Triangle has methods isTriangle(),
// isRight(), isScalene(), isEquilateral(), isIsosceles() which all return boolean values.
// The Triangle constructor prints an error message if the user inputs illegal values.
//
//---------------------------------------------------------------------------------------------
class Triangle {
// the sides of the triangle
private int a, b, c;
//---------------------------------------------------------------------------------------------
// Constructor
//
// Returns error message if parameter values are values are illegal (< 0) or invalid
// (eg., s1 + s2 < s3).
//---------------------------------------------------------------------------------------------
public Triangle (int s1, int s2, int s3)
{ a = (s1 > 0 ? s1 : 0);
b = (s2 > 0 ? s2 : 0);
c = (s3 > 0 ? s3 : 0);
if (!isTriangle())
System.err.println("Invalid input: Sides do not form a triangle.");
}
//---------------------------------------------------------------------------------------------
// isTriangle
//
// input : none
// return : triangle lengths comprise a valid triangle?
//---------------------------------------------------------------------------------------------
public boolean isTriangle()
{
return !((a*b*c == 0) || (a + b < c) || (a + c < b) || (b + c < a));
}
//---------------------------------------------------------------------------------------------
// isRight
//
// input : none
// return : triangle is a right triangle?
//---------------------------------------------------------------------------------------------
public boolean isRight()
{
return (a*a + b*b == c*c)||(a*a + c*c == b*b)||(b*b + c*c == a*a);
}
//---------------------------------------------------------------------------------------------
// isScalene
//
// input : none
// return : triangle has no two sides the same length?
//---------------------------------------------------------------------------------------------
public boolean isScalene()
{
return (a != b) && (a != c) ;
}
//---------------------------------------------------------------------------------------------
// isEquilateral
//
// input : none
// return : triangle has all three sides the same length?
//---------------------------------------------------------------------------------------------
public boolean isEquilateral()
{
return (a==b)&&(b==c);
}
//---------------------------------------------------------------------------------------------
// isIsosceles
//
// input : none
// return : triangle has exactly two sides the same length?
//---------------------------------------------------------------------------------------------
public boolean isIsosceles()
{
return ((!isScalene())&&(!isEquilateral()));
}
}