/** * @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(); } if(n==0){ d=1; } this.r.setFirstElement(n); this.r.setSecondElement(d); this.reduce(); } //getters public int getNumerator(){ return numerator; } public int getDenominator(){ return denominator; } //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){ this.r.setFirstElement(n); this.reduce(); } /** * * 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){ this.r.setSecondElement(d); this.reduce(); } /** * * 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; int compare; if((o.getNumerator()/o.getDenominator()) < (this.getNumerator()/this.getDenominator())){ compare = GREATERTHAN; return compare; } if((o.getNumerator()/o.getDenominator() > this.getNumerator()/this.getDenominator())){ compare = LESSTHAN; return compare; } if((o.getNumerator()/o.getDenominator()) == (this.getNumerator()/this.getDenominator())){ compare = EQUAL; return compare; } else return EQUAL; //complete the compareTo method. Use the constants defined above in returning the value } /** * * 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){ boolean eql = false; if(this == r) { eql = true; } return eql; } //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 (){ String str1; str1 = ("(" + getNumerator() + "/" + getDenominator() + ")"); return str1; } /** * 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=1; int n=this.r.getFirstElement(); int d=this.r.getSecondElement(); if(n!=0 && d!=0) { rd = gcd(n,d); } if(rd!=1) { n=n/rd; d=d/rd; if( (d<0 && n>0) || (d<0 && n<0)) { n=n*-1; d=d*-1; } } this.setDenominator(d); this.setNumerator(n); } /** * 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