Question

Write a program that creates a List of Rationals and sorts them into increasing order. Use...

Write a program that creates a List of Rationals and sorts them into increasing order. Use appropriate methods from the Collections Framework classes and sort and order the elements from smaller to bigger.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go this code and let me know if u need any changes in this.Thank You
_________________

// Rational.java

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

   }

}
_______________________

Output:

_____ Displaying the Rational numbers before Sorting _____
1.0/3.0
2.0/7.0
1.0/-4.0
3.0/11.0
5.0/8.0
1.0/2.0
_____ Displaying the Rational numbers after Sorting _____
1.0/-4.0
3.0/11.0
2.0/7.0
1.0/3.0
1.0/2.0
5.0/8.0

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Write a program that creates a List of Rationals and sorts them into increasing order. Use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT