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.
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
Write a program that creates a List of Rationals and sorts them into increasing order. Use...
Write a test program that prompt the user to enter seven numbers, stores them in an array list and do the following: 1. Displays its elements (numbers) in increasing order by invoking a method with the following header that sorts the array: public static void sort(ArrayList<Integer>list) 2. Write a method that returns and displays the sum of all numbers (elements) of the ArrayList. Using the following header: public static double sum(ArrayList<Integer>list) 3. Write a method that removes the duplicate numbers...
//Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.
write a program in x86 assembly language that sorts 10 integers entered from the keyboard and displays them in order
Write a c++ program that sorts one – dimensional array’s value into ascending order using the selection sort method
Write a program that creates three identical arrays, list1, list2, and list3, of 5000 elements. The program then sorts list1 using bubble sort, list2 using selection sort, and list3 using insertion sort and outputs the number of comparisons and item assignments made by each sorting algorithm Note: (Complete Programming Exercise #15 on page 1345. Submit a screen shot testing with 20 numbers, 500 numbers, 1,000 numbers. Show the output of the arrays of 20 so you can see they are...
Write a program that inputs ten (10) integers and then sorts them in order from largest to smallest in the programming language called Perl. You do not need to error check the input; you can assume the user enters integers. You can select the sorting algorithm of your choice, but you must implement this algorithm yourself. You cannot use a built-in sorting function. [15 points] Below is an example of a sample program run: Unsorted: 10, 4, 23, 99, 7,...
Write a program that takes a list of integer numbers from a user and creates a new sorted in ascending order list. As output you need to print original list and sorted one. use list and while python
Write a program that creates three identical arrays,list1,list2, and list3, of 5000 elements. The program then sortslist1using bubble sort, list2using selection sort, andlist3using insertion sort, and outputs the number of comparisons and item assignments made by each sorting algorithm. Submit a screen shot testing with 20 numbers, 500 numbers, 1000 numbers. Show the output of the arrays of 20 so you can see they are in order. please use c++
*MATLAB CODE* 3)Write a function 'mydsort' that sorts a vector in descending order (using a loop, not the built-in sort function). 4)write a function that will receive a vector and will return two index vectors: one for ascending order and one for descending order. Check the function by writing a script that will call the function and then use the index vectors to print the original vector in ascending and descending order. **Please make sure they work. I have found...