In: Computer Science
Write a Comparator to compare two Rationals so that a/b>c/d if a>c i.e. sort by increasing order of numerators only. Test your program by modifying the code below. Note do NOT change the Rational class in order to solve this problem.
Code:
public class Rational implements
Comparable<Rational>
{
//Declaring instance variables
double numerator = 0;
double denominator = 1;
//Parameterized constructor
public Rational(double n,double d){
numerator=n;
denominator=d;
}
// getters and setters
public void setNumerator(double value) {
this.numerator = value;
}
public void setDenominator(double value) {
this.denominator = value;
}
public double getNumerator() {
return this.numerator;
}
public double getDenominator() {
return this.denominator;
}
//
-----------------------------------------------------------------
// 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;
}
@Override
public int compareTo(Rational o) {
int val = 0;
double currVal =
numerator/denominator;
Rational r = (Rational) o;
double paraVal =
o.getNumerator()/o.getDenominator();
if (currVal > paraVal)
val = 1;
else if (currVal <
paraVal)
val = -1;
else if (currVal == paraVal)
val = 0;
return val;
}
}
_____________________
// Test.java
import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String[] args) {
ArrayList<Rational> arl=new
ArrayList<Rational>();
arl.add(new Rational(1, 3));
arl.add(new Rational(2, 7));
arl.add(new Rational(1, -4));
arl.add(new Rational(3, 11));
arl.add(new Rational(5, 8));
arl.add(new Rational(1, 2));
System.out.println("_____ Displaying the Rational
numbers before Sorting _____");
for(int i=0;i<arl.size();i++)
{
System.out.println(arl.get(i));
}
Collections.sort(arl);
System.out.println("_____ Displaying the Rational
numbers after Sorting _____");
for(int i=0;i<arl.size();i++)
{
System.out.println(arl.get(i));
}
}
}
import java.util.Comparator; public class NumeratorComparator implements Comparator<Rational> { @Override public int compare(Rational o1, Rational o2) { return Double.compare(o1.getNumerator(), o2.getNumerator()); } }
import java.util.ArrayList; import java.util.Collections; public class Test { public static void main(String[] args) { ArrayList<Rational> arl = new ArrayList<Rational>(); arl.add(new Rational(1, 3)); arl.add(new Rational(2, 7)); arl.add(new Rational(1, -4)); arl.add(new Rational(3, 11)); arl.add(new Rational(5, 8)); arl.add(new Rational(1, 2)); System.out.println("_____ Displaying the Rational numbers before Sorting _____"); for (int i = 0; i < arl.size(); i++) { System.out.println(arl.get(i)); } Collections.sort(arl, new NumeratorComparator()); System.out.println("_____ Displaying the Rational numbers after Sorting _____"); for (int i = 0; i < arl.size(); i++) { System.out.println(arl.get(i)); } } }