Public feed
#381747
/**
* @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;
}
this.r.setFirstElement(n);
this.r.setSecondElement(d);
this.reduce();
}
//getters
public int getNumerator(){
return numerator;
}
public int getDenominator(){
return denominator;
}
//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){
this.r.setFirstElement(n);
this.reduce();
}
/**
*
* 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){
this.r.setSecondElement(d);
this.reduce();
}
/**
*
* 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;
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<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){
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: <br>
* 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<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