import com.symantec.itools.swing.models.StringTableModel;

public final class Rational extends java.lang.Number implements Comparable
{
    private int diff;
    private int num;
    private int den;
    
    public double doubleValue()
    {
        // This method is derived from class java.lang.Number
        // to do: code goes here
        return ((double)num/(double)den);
    }

    public float floatValue()
    {
        // This method is derived from class java.lang.Number
        // to do: code goes here
        return ((float)num/(float)den);
    }

    public int intValue()
    {
        // This method is derived from class java.lang.Number
        // to do: code goes here
        return (num/den);
    }

    public long longValue()
    {
        // This method is derived from class java.lang.Number
        // to do: code goes here
        return (long)(num/den);
    }

    public short shortValue()
    {
        // This method is derived from class java.lang.Number
        // to do: code goes here
        return (short)(num/den);
    }
    
    public Rational(int n, int d)
    {
        this.num = n;
        this.den = d;
        this.simplify();
        this.calcdiff();
		//{{INIT_CONTROLS
		//}}
	}
    
    public Rational(int n)
    {
        this.num = n;
        this.den = 1;
        this.calcdiff();
    }
    
    public Rational(Integer n, Integer d)
    {
        this.num = n.intValue();
        this.den = d.intValue();
        this.simplify();
        this.calcdiff();
    }
    
    public Rational(Integer n)
    {
        this.num = n.intValue();
        this.den = 1;
        this.calcdiff();
    }
    
    public Rational(Rational r)
    {
        this.num = r.num;
        this.den = r.den;
        this.simplify();
        this.calcdiff();
    }
    
    private int gcd(int a, int b) {
        if (a > b) return(gcd(b,a));
        else if (a < 0) return(gcd(-a,b));
        else if (a == 0) return(b);
        else return(gcd(b%a,a));
    }
    
    private void simplify()
    {
        int mg = gcd(this.num,this.den);
        if ((this.den == 0) && (this.num != 0)) this.num = 1;
        else if (this.den != 0) {
            this.num /= mg;
            this.den /= mg;
        }
        if (this.den < 0) {
            this.num = -this.num;
            this.den = -this.den;
        }
    }
    
    public String toString()
    {
        if (this.den == 1) {
            return(new String(Integer.toString(this.num)));
        } else {
            return("(" + new String(Integer.toString(this.num)) + "/" + new String(Integer.toString(this.den)) + ")");
        }
    }
    
    public boolean equals(Object obj)
    {
        if (obj.getClass() == this.getClass()) {
            Rational r = (Rational)obj;
            return(r.num*this.den == r.den*this.num);
        } else
            return(false);
    }
    
    public Rational plus(Rational r)
    {
        return(new Rational(r.den*this.num + r.num*this.den,r.den*this.den));
    }

    public Rational minus(Rational r)
    {
        return(new Rational(r.den*this.num - r.num*this.den,r.den*this.den));
    }

    public Rational times(Rational r)
    {
        return(new Rational(r.num*this.num,r.den*this.den));
    }

    public Rational dividedby(Rational r)
    {
        return(new Rational(r.den*this.num,r.num*this.den));
    }
    
    public Rational additiveInverse()
    {
        return(new Rational(-this.num,this.den));
    }

    public Rational op(ArithOperator ao, Rational r) throws BadOperatorException
    {
        if (ao.is(ArithOperator.PLUS))
            return(this.plus(r));
        else if (ao.is(ArithOperator.MINUS))
            return(this.minus(r));
        else if (ao.is(ArithOperator.TIMES))
            return(this.times(r));
        else if (ao.is(ArithOperator.DIVIDED_BY))
            return(this.dividedby(r));
        else
            throw new BadOperatorException();
    }
    
    public Object clone() throws CloneNotSupportedException
    {
        return(new Rational(this));
    }
        
    public int compareTo(Object o)
    {
        Rational r = (Rational)o;
        if ((this.den == 0) && (r.den == 0))
            return(0);
        else if (this.den == 0)
            return(1);
        else if (r.den == 0)
            return(-1);
        else
            return(this.num*r.den - this.den*r.num);
    }
	//{{DECLARE_CONTROLS
	//}}
	
	public int getdiff() {
	    return(this.diff);
	}
	
	private void calcdiff() {
	    
	}
	
}