Modify the RationalNumber class so that it implements the Comparable interface.
ID: 3844373 • Letter: M
Question
Modify the RationalNumber class so that it implements the Comparable interface. To perform the comparison, compute an equivalent floating point value from the numerator and denominator for both RationalNumber objects, then compare them using a tolerance value of 0.0001. Write a main driver to test your modifications.
Hint:
go to the end of this code (p.315)
//--------------------------------------------------------------
// 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;
}
At this point insert this commment:
//-----------------------------------------------------------------
// Compares this RationalNumber2 object to the parameter.
//-----------------------------------------------------------------
Now insert the code that does what the comment says.
Now you need to modify RationalTester2.java . Begin by testing the Comparable nature of RationalNumber2 objects, like this:
public static void main(String[] args)
{
RationalNumber2 r1 = new RationalNumber2(6, 8);
RationalNumber2 r2 = new RationalNumber2(1, 3);
System.out.println("First rational number: " + r1);
System.out.println("Second rational number: " + r2);
int compareValue = r1.compareTo(r2);
if (compareValue == 0)
System.out.println("r1 and r2 are equal.");
else
if (compareValue < 0)
System.out.println("r1 is less than r2");
else
System.out.println("r1 is greater than r2");
RationalNumber2 r3 = new RationalNumber2(6, 7);
RationalNumber2 r4 = new RationalNumber2(6, 7);
You should be able to take it from here
Explanation / Answer
Below is your code: -
RelationalNumber.java
public class RationalNumber implements Comparable<RationalNumber> {
private int numerator, denominator;
public RationalNumber(int num, int den) {
if (den == 0)
den = 1;
if (den < 0) {
num = num * -1;
den = den * -1;
}
numerator = num;
denominator = den;
reduce();
}
// To return numerator
public int getNumerator() {
return numerator;
}
// To return denominator
public int getDenominator() {
return denominator;
}
public RationalNumber reciprocal() {
return new RationalNumber(denominator, numerator);
} // End reciprocal
// For adding rationals
public RationalNumber add(RationalNumber op2) {
int commonDenominator = denominator * op2.getDenominator();
int num1 = numerator * op2.getDenominator();
int num2 = op2.getNumerator() * denominator;
int sum = num1 + num2;
return new RationalNumber(sum, commonDenominator);
} // End Addition
// For subtracting rationals
public RationalNumber subtract(RationalNumber op2) {
int commonDenominator = denominator * op2.getDenominator();
int num1 = numerator * op2.getDenominator();
int num2 = op2.getNumerator() * denominator;
int diff = num1 - num2;
return new RationalNumber(diff, commonDenominator);
} // End Subtraction
public RationalNumber multiply(RationalNumber op2) {
int num = numerator * op2.getNumerator();
int den = denominator * op2.getDenominator();
return new RationalNumber(num, den);
} // End Multiply
// For Dividing rationals
public RationalNumber divide(RationalNumber op2) {
return multiply(op2.reciprocal());
} // End Divide
public String toString() {
String result;
if (numerator == 0)
result = "0";
else if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
} // End toString
// Reduced form
private void reduce() {
if (numerator != 0) {
int common = gcd(Math.abs(numerator), denominator);
numerator = numerator / common;
denominator = denominator / common;
}
} // End Reduce
private int gcd(int num1, int num2) {
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
} // End GCD
@Override
public int compareTo(RationalNumber o) {
double div1 = (double) this.numerator / (double) this.denominator;
double div2 = (double) o.numerator / (double) o.denominator;
if ((this.numerator == o.getNumerator() && this.denominator == o.getDenominator())
|| (Math.abs(div1 - div2) < 0.0001)) {
return 0;
} else if ((div1 - div2) < 0) {
return -1;
} else {
return 1;
}
}
} // End RationalNumber
RelationalTester2.java
public class RationalTester2 {
public static void main(String[] args) {
RationalNumber r1 = new RationalNumber(6, 8);
RationalNumber r2 = new RationalNumber(1, 3);
System.out.println("First rational number: " + r1);
System.out.println("Second rational number: " + r2);
int compareValue = r1.compareTo(r2);
if (compareValue == 0)
System.out.println("r1 and r2 are equal.");
else if (compareValue < 0)
System.out.println("r1 is less than r2");
else
System.out.println("r1 is greater than r2");
RationalNumber r3 = new RationalNumber(6, 7);
RationalNumber r4 = new RationalNumber(6, 7);
System.out.println("Third rational number: " + r3);
System.out.println("Fourth rational number: " + r4);
compareValue = r3.compareTo(r4);
if (compareValue == 0)
System.out.println("r3 and r4 are equal.");
else if (compareValue < 0)
System.out.println("r3 is less than r4");
else
System.out.println("r3 is greater than r4");
}
}
Sample Run: -
First rational number: 3/4
Second rational number: 1/3
r1 is greater than r2
Third rational number: 6/7
Fourth rational number: 6/7
r3 and r4 are equal.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.