/** Rational.java provides a class to represent Rational numbers.
 */

import ann.easyio.Keyboard;

class Rational extends Object
{
    // constructors -- every rational has a numerator and a denominator
    Rational( ) { myNum = 0; myDenom = 1; }

    Rational( int num, int denom ) {
	myNum = num;
	myDenom = denom;
	if( myDenom == 0 ) myDenom = 1;
    }

    public String toString() {
	if( myDenom == 1 ) return( "" + myNum );
	return( myNum + "/" + myDenom );
    }

    // the getters
    public double getNum() { return myNum; }    
    public double getDenom() { return myDenom; }

    // read a Rational from the keyboard
    public boolean read( Keyboard theKeyboard ) {
	myDenom = 1;		// the default denominator is 1

	theKeyboard.EatWhiteSpace();
	// see if the input is valid
	if( !Character.isDigit(theKeyboard.peekChar()) ) {
	    // no number out here......
	    return false;
	}

	// read the numerator
	myNum = 0;
	while( Character.isDigit(theKeyboard.peekChar()) ) {
	    myNum = myNum * 10 + (theKeyboard.getChar()-'0');
        }

	// check for a denominator
	theKeyboard.EatWhiteSpace();
	if( theKeyboard.peekChar() == '/' ) {
	    theKeyboard.getChar(); // eat the '/'
	    theKeyboard.EatWhiteSpace();
	    myDenom = 0;
    	    while( Character.isDigit(theKeyboard.peekChar()) ) {
	        myDenom = myDenom * 10 + (theKeyboard.getChar()-'0');
            }
	    if( myDenom == 0 ) return false;
	}
	return true;
    }


    // private attributes
    private int myNum, myDenom;
}

