We help IT Professionals succeed at work.

# Testing an implemented Comparable

on
Medium Priority
164 Views
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
//-------------------------------------------------------------------------------------
{
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?
Comment
Watch Question

## View Solution Only

Commented:
I'm thinkin'... create two objects with different values, compare 'em with the methods you wrote, and see if the results are right.

I mean, you could do something fancy like make an array of 'em and call a sort algorithm that relies on the Comparable interface, but... I'm lazy.  (-:

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
Didn't I just create two objects in this driver class?

My problem is how to compare them with the methods I wrote! ;)

Commented:
Well, call the method on one of the objects, passing the other in as an argument, and output the result.

Commented:
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??
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile