Public feed
#378033
/**
* @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<Rational> {
private Pair<Integer> r=new Pair<Integer>(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: <br>
* 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;
}
try{
numerator = n;
denominator = 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: <br>
* 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<br>
* 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<br>
* 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<br>
* 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<br>
* 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)<br>
* 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<c/d then ad<bc
* Precondition: <br>
* Postcondition:
* @throws NullPointerException if o is null
*@param o
*@return -1 if this<o, 0 if this==o, 1 if this>o
*/
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<br>
* 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: <br>
* Postcondition: this reduced to lowest terms
*
*/
private void reduce(){
int rd;
rd = this.gcd(numerator, denominator);
if(numerator==0)
{
denominator=1;
}
if(numerator>0 && denominator<0){
numerator = (numerator*-1)/rd;
denominator = (denominator*-1)/rd;
}
else 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<br>
* Precondition: both are positive integers. Call with ABS on each parameter.<br>
* 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<n1)
return gcd(n1, m1);
else
if (m1%n1==0)
return n1;
else
return gcd(n1, m1%n1);
}
//Needed for junit testing. visibility must be public due to inheritance
//DO NOT MODIFY
public boolean equals(Object o){
if (o.getClass() == this.getClass())
return this.equals((Rational)o);
else
return false;
}
} // End of Rational class definition