Question

In: Computer Science

Write a Comparator to compare two Rationals so that a/b>c/d if a>c i.e. sort by increasing...

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));
   }
  

   }

}

Solutions

Expert Solution


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));
        }


    }

}


Related Solutions

Let A = {a, b, c, d} and B = {b, d, e}. Write out all...
Let A = {a, b, c, d} and B = {b, d, e}. Write out all of the elements of the following sets. (a) B ∩ ∅ (b) A ∪ B (c) (A ∩ B) × B (d) P(A\B) (e) {X ∈ P(A) | |X| ≤ 3}
Write a C++ program to determine the sum of two 1-D matrices: A = [a b...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b c d] B = [e f g h] The user will provide the values of a to h.
(This is a multiple choice question, so the answer is either A, B, C, or D.)...
(This is a multiple choice question, so the answer is either A, B, C, or D.) Question: For probability density function of a random variable X, P(X < a) can also be described as: A.) The area under the curve to the right of a. B.) F(a), where F(X) is the cumulative distribution function. C.) The area under the curve between 0 and a. D.) 1- F(a) where F(X) is the cumulative distribution function.
Consider the cross: A/a; b/b; C/c; D/d; E/e x A/a; B/b; c/c; D/d; e/e a) what...
Consider the cross: A/a; b/b; C/c; D/d; E/e x A/a; B/b; c/c; D/d; e/e a) what proportion of the progeny will phenotypically resemble the first parent? b) what proportion of the progeny will genotypically resemble neither parent?
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
Java: How do I compare two lists for example: List<A> listOne {a,b,c,d,e} List<A> listTwo {a,d,e} 1....
Java: How do I compare two lists for example: List<A> listOne {a,b,c,d,e} List<A> listTwo {a,d,e} 1. I am supposed to make sure that whatever that is in listTwo is in listOne even though there's more in listOne and there are repetitives in listTwo e.g {a,a,d,e} but it would still turn out true. But if listTwo is {a,b,d,f} it would return false. 2. Also, how can I make sure that in listTwo, there can be repetitives of any elements in listOne...
In the diagram, which point is at the lowest potential? (a) A (b) B (c) C (d) D
In the diagram, which point is at the lowest potential?(a) A(b) B(c) C(d) D
Compare hepatitis A, B, C, D, and E in terms of source of infection, incubation period,...
Compare hepatitis A, B, C, D, and E in terms of source of infection, incubation period, acute disease manifestations, development of chronic disease, and the carrier state.
need to know a,b,c,d A. describe and compare the major structural features of the male and...
need to know a,b,c,d A. describe and compare the major structural features of the male and female reproductive systems. B. why should mem produce between 100 and 300 million sperm a fay when women produce just one haploid egg a month? Hypothesize as to the reason for the differences in the mode of reproduction between men and women. C. compare the function of GnRH, LH, FSH in males and females. are they similar functions? if so, explain why. D. considerinf...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT