msheppard74
asked on
Testing an implemented Comparable
I have the following class
public class RationalNumber
{
private int numerator, denominator;
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
//Constructor: Sets up the rational numebr by ensuring a nonzero denominator and making
//only the numerator signed.
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
public RationalNumber (int numer, int denom)
{
if (denom == 0)
denom = 1;
//Make the numerator "store" the sign
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;
reduce();
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
// Returns the numerator of this rational number
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
public int getNumerator ()
{
return numerator;
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
// Returns the denominator of this rational number.
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
public int getDenominator ()
{
return denominator;
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
// Returns the reciprocal of this rational number
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
public RationalNumber reciprocal ()
{
return new RationalNumber (denominator, numerator);
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
// Adds this rational number to the one passed a a parameter. A common denominator
// is found by multiplying the individual denominators
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
public RationalNumber add (RationalNumber op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;
return new RationalNumber (sum, commonDenominator);
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
// Subtracts the rational number passed as a parameter from this rational number
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- -
public RationalNumber subtract (RationalNumber op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;
return new RationalNumber (difference, commonDenominator);
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- --
// Multiplies this rational number by the one passed as a parameter
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- --
public RationalNumber multiply (RationalNumber op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();
return new RationalNumber (numer, denom);
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- --
// Divides this rational number by the one passes as a parameter by multiplying by the
// reciprocal of the second rational.
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- --
public RationalNumber divide (RationalNumber op2)
{
return multiply (op2.reciprocal());
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- --
// Divides this rational number by the one passed as a parameter by multiplying by the
// reciprocal of the second rational
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- --
public boolean iLike (RationalNumber op2)
{
return ( numerator == op2.getNumerator() &&
denominator == op2.getDenominator() );
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- --
// Returns this rational number as a string
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- --
public String toString ()
{
String result;
if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- --
//
public int compare(Object o)
{
double tolerance = 0.0001;
RationalNumber other = (RationalNumber)o;
double thisFrac = this.asDecimal();
double otherFrac = other.asDecimal();
if (Math.abs(thisFrac - otherFrac) < tolerance)
{
return 0; //equal
}
else
{
return 1;
}
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---
// Returns the Fractional for the objects
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---
public double asDecimal()
{
return numerator / denominator;
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---
// Reduces this rational number by dividing both the numerator and the denominator by
// their greatest common divisor.
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---
private void reduce ()
{
if (numerator != 0)
{
int common = gcd (Math.abs(numerator), denominator);
numerator = numerator = numerator / common;
denominator = denominator / common;
}
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---
// Computes and returns th greates common divisor of the two positive parameters.
// Uses Euclid's algorithm.
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---
private int gcd (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
}
}
I want to test the comparable methods that were set up. I created the following driver class but am unsure how to proceed. Please advise.
public class RationalNumber_Driver
{
public static void main (String[] args)
{
RationalNumber r1 = new RationalNumber (6, 8);
RationalNumber r2 = new RationalNumber (1, 3);
System.out.println (r1);
System.out.println (r2);
}
}
How can I code from here to test the methods?
public class RationalNumber
{
private int numerator, denominator;
//------------------------
//Constructor: Sets up the rational numebr by ensuring a nonzero denominator and making
//only the numerator signed.
//------------------------
public RationalNumber (int numer, int denom)
{
if (denom == 0)
denom = 1;
//Make the numerator "store" the sign
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;
reduce();
}
//------------------------
// Returns the numerator of this rational number
//------------------------
public int getNumerator ()
{
return numerator;
}
//------------------------
// Returns the denominator of this rational number.
//------------------------
public int getDenominator ()
{
return denominator;
}
//------------------------
// Returns the reciprocal of this rational number
//------------------------
public RationalNumber reciprocal ()
{
return new RationalNumber (denominator, numerator);
}
//------------------------
// Adds this rational number to the one passed a a parameter. A common denominator
// is found by multiplying the individual denominators
//------------------------
public RationalNumber add (RationalNumber op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;
return new RationalNumber (sum, commonDenominator);
}
//------------------------
// Subtracts the rational number passed as a parameter from this rational number
//------------------------
public RationalNumber subtract (RationalNumber op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;
return new RationalNumber (difference, commonDenominator);
}
//------------------------
// Multiplies this rational number by the one passed as a parameter
//------------------------
public RationalNumber multiply (RationalNumber op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();
return new RationalNumber (numer, denom);
}
//------------------------
// Divides this rational number by the one passes as a parameter by multiplying by the
// reciprocal of the second rational.
//------------------------
public RationalNumber divide (RationalNumber op2)
{
return multiply (op2.reciprocal());
}
//------------------------
// Divides this rational number by the one passed as a parameter by multiplying by the
// reciprocal of the second rational
//------------------------
public boolean iLike (RationalNumber op2)
{
return ( numerator == op2.getNumerator() &&
denominator == op2.getDenominator() );
}
//------------------------
// Returns this rational number as a string
//------------------------
public String toString ()
{
String result;
if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}
//------------------------
//
public int compare(Object o)
{
double tolerance = 0.0001;
RationalNumber other = (RationalNumber)o;
double thisFrac = this.asDecimal();
double otherFrac = other.asDecimal();
if (Math.abs(thisFrac - otherFrac) < tolerance)
{
return 0; //equal
}
else
{
return 1;
}
}
//------------------------
// Returns the Fractional for the objects
//------------------------
public double asDecimal()
{
return numerator / denominator;
}
//------------------------
// Reduces this rational number by dividing both the numerator and the denominator by
// their greatest common divisor.
//------------------------
private void reduce ()
{
if (numerator != 0)
{
int common = gcd (Math.abs(numerator), denominator);
numerator = numerator = numerator / common;
denominator = denominator / common;
}
}
//------------------------
// Computes and returns th greates common divisor of the two positive parameters.
// Uses Euclid's algorithm.
//------------------------
private int gcd (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
}
}
I want to test the comparable methods that were set up. I created the following driver class but am unsure how to proceed. Please advise.
public class RationalNumber_Driver
{
public static void main (String[] args)
{
RationalNumber r1 = new RationalNumber (6, 8);
RationalNumber r2 = new RationalNumber (1, 3);
System.out.println (r1);
System.out.println (r2);
}
}
How can I code from here to test the methods?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, call the method on one of the objects, passing the other in as an argument, and output the result.
ASKER
public class RationalNumber_Driver
{
public static void main (String[] args)
{
RationalNumber r1 = new RationalNumber (4, 10);
RationalNumber r2 = new RationalNumber (1, 3);
RationalNumber r3;
System.out.println (r1);
System.out.println (r2);
if (r1.compare(r2) == 0)
System.out.println ("The rational numbers are equal");
}
}
OK I tested this an it returned that the numbers are equal. Can you test and concur?? Pretty pretty please??
{
public static void main (String[] args)
{
RationalNumber r1 = new RationalNumber (4, 10);
RationalNumber r2 = new RationalNumber (1, 3);
RationalNumber r3;
System.out.println (r1);
System.out.println (r2);
if (r1.compare(r2) == 0)
System.out.println ("The rational numbers are equal");
}
}
OK I tested this an it returned that the numbers are equal. Can you test and concur?? Pretty pretty please??
ASKER
My problem is how to compare them with the methods I wrote! ;)