/** * @author * Supports the use of rational numbers. This includes methods for the * standard arithmetic operations of add, subtract, multiply and divide. Included are * helper methods for reducing rational numbers, putting in simples form. */ public class Rational implements Comparable { private Pair r=new Pair(new Integer(0), new Integer(1)); private int numerator = r.getFirstElement(); private int denominator = r.getSecondElement(); //Constructors /** * Postcondition: Creates the canonical zero rational (0/1) */ public Rational() { } /** * * Precondition:
* Postcondition: Rational created is (n/d) in lowest terms * @throws IllegalArgumentException if d==0 */ public Rational(int n, int d){ if(d==0){ throw new IllegalArgumentException(); } try{ this.r.setFirstElement(Math.abs(n)); this.r.setSecondElement(Math.abs(d)); this.reduce(); } catch(IllegalArgumentException e){} } //getters public int getNumerator(){ return numerator; /*int num; num=r.getFirstElement(); return num;*/ } public int getDenominator(){ return denominator; /*int dem; dem=r.getSecondElement(); return dem;*/ } //setters /** * * Precondition:
* Postcondition: changes this so this.getNumerator()==d *@param n - int value for the numerator of the rational number * */ public void setNumerator(int n){ numerator = n; } /** * * Precondition: d!=0
* Postcondition: changes this so this.getDenominator()==d *@param d - int value for the denominator of the rational number * */ public void setDenominator(int d){ denominator = d; } /** * * Precondition: r!=null
* Postcondition: returns the sum of this instance of a rational number and r in simplest terms *@param r - a rational number *@return this+r */ public Rational add(Rational r){ return new Rational(); } /** * * Precondition: r!=null
* Postcondition: returns the difference of this instance of a rational number and r in simplest terms *@param r - a rational number *@return (this-r) */ public Rational substract(Rational r){ return new Rational(); } /** * * Precondition: r!=null
* Postcondition: returns the prodcut of this instance of a rational number and r in simplest terms *@param r - a rational number *@return (this*r) */ public Rational multiply(Rational r){ return new Rational(); } /** * * Precondition: r!=null && r!=(0/1)
* Postcondition: returns the quotient of this instance of a rational number and r in simplest terms *@param r - a rational number *@return (this/r) */ public Rational divide(Rational r){ return new Rational(); } /** * if a/b * Postcondition: * @throws NullPointerException if o is null *@param o *@return -1 if thiso */ public int compareTo(Rational o) { final int LESSTHAN=-1; final int EQUAL=0; final int GREATERTHAN=1; //complete the compareTo method. Use the constants defined above in returning the value return EQUAL; } /** * * Precondition: none
* Postcondition: return true if this and r are equivalent rational numbers *@param r - a rational number *@return (this-r) */ public boolean equals(Rational r){ return true; } //private methods /** * @see java.lang.Object#toString() * Postcondition: The value of the rational number is given as the string * (numerator/denominator) with no blanks. */ public String toString (){ return null; } /** * Use gcd to find the greatest commond divisor of the * numerator and denominator. * * Precondition:
* Postcondition: this reduced to lowest terms * */ private void reduce(){ int rd; rd = this.gcd(numerator, denominator); if(numerator<0 && denominator>0){ numerator = numerator/rd; denominator = denominator/rd; } if(denominator<0 && numerator<0){ numerator = (numerator*-1)/rd; denominator = (denominator*-1)/rd; } else { numerator = numerator/rd; denominator = denominator/rd; } } /** * Description: Recursive version of Euclid's algorithm for finding the greatest common divisor of two integers
* Precondition: both are positive integers. Call with ABS on each parameter.
* Postcondition: returns greatest common divisor of a and b *@param a *@param b *@return int which is the greatest common divisor of a and b */ //DO NOT MODIFY private int gcd(int m, int n){ int m1=Math.abs(m); int n1=Math.abs(n); if (m1