Question
You need to create an ArrayList with 10 Fraction objects. Each fraction should be created with a random numerator and denominator between 1 and 100. The fractions should be printed to the screen, sorted from low decimal value to high, then, reprinted to the screen as a sorted list of fractions.
Sample Output: (Your fraction values will vary)
Before sort:
0 The fraction is 67/37 Decimal value is 1.81
1 The fraction is 97/77 Decimal value is 1.26
2 The fraction is 40/53 Decimal value is 0.75
3 The fraction is 5/47 Decimal value is 0.11
4 The fraction is 15/18 Decimal value is 0.83
5 The fraction is 41/33 Decimal value is 1.24
6 The fraction is 22/74 Decimal value is 0.30
7 The fraction is 40/71 Decimal value is 0.56
8 The fraction is 32/93 Decimal value is 0.34
9 The fraction is 99/27 Decimal value is 3.67
After sort:
0 The fraction is 5/47 Decimal value is 0.11
1 The fraction is 22/74 Decimal value is 0.30
2 The fraction is 32/93 Decimal value is 0.34
3 The fraction is 40/71 Decimal value is 0.56
4 The fraction is 40/53 Decimal value is 0.75
5 The fraction is 15/18 Decimal value is 0.83
6 The fraction is 41/33 Decimal value is 1.24
7 The fraction is 97/77 Decimal value is 1.26
8 The fraction is 67/37 Decimal value is 1.81
9 The fraction is 99/27 Decimal value is 3.67
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
import java.util.ArrayList;import java.util.Collections;
public class Solution {
public static void main(String[] args)
{
ArrayList<Fraction> list = new ArrayList<Fraction>();
for(int i = 0; i < 10; i++)
{
Fraction temp = new Fraction();
temp.setDenominator((int)(Math.random()*100 + 1));
temp.setNumerator((int)(Math.random()*100 + 1));
System.out.println(i + " The fraction is " + temp.getNumerator() + "/" + temp.getDenominator() + " Decimal value is " + temp.getValue());
list.add(temp);
}
System.out.println("After sort");
Collections.sort(list);
for(int i = 0; i < 10; i++)
{
System.out.println(i + " The fraction is " + list.get(i).getNumerator() + "/" + list.get(i).getDenominator() + " Decimal value is " + list.get(i).getValue());
}
}
}...