Question: The FractionTester program generates several Fractions and inserts them into an ArrayList and then into a plain array. It then sorts both the plain array
The FractionTester program generates several Fractions and inserts them into an ArrayList and then into a plain array. It then sorts both the plain array of Fractions and the ArrayList of Fractions. In order to be able to call Arrays.sort( plain array ) or Collections.sort( ArryList ), the Fraction type must have an ordering relation defined that allows one Fraction to be compared to another. The standard way in which Java allows the writer of a class to define an ordering relation on their class is by having that class implement the Comparable interface. Doing so requires that a method named compareTo() be written inside the class definition which receives another of its own type and returns a number to indicate whether this object is less than, equal to or greater than the other object.
Your task for Lab#8 is to modify the Fraction class so that it implements Comparable and contains a working compareTo() method that accepts a Fraction (other) and returns a number that indicates this Fraction is less than, equal to, or greater than the other Fraction.
You have already written a Fraction class for a previous project. You will now be given a stripped down Fraction.java as a starter file. You will enhance it by implementing the Comparable interface. You must implement this interface using Generics. As such, the signature of your Fraction class must now read as follows in the given Fraction.java
public class Fraction implements Comparable
If you look in the Java API (Google: Java 10 API Comparable) and scroll down you will eventually come to a section that tells you that by implementing Comparable you are required to write a method in your Fraction class that has this signature:
public int compareTo( Fraction other )
This method will return some int value less than 0 if this Fractions is less than the other Fraction. It should return 0 if the two Fractions are equal, and should return some int value greater than 0 if the other Fraction is greater than this Fraction.
You must be thoughtful about how you write your compareTo() method. You are not allowed to divide numer by denom and store the result in a double then compare it to the other Fraction's doubler quotient. This approach is vulnerable to rounding error..
BIG HINT: (CODE RE-USE) What method(s) have you already written for the Fraction class in the previous project that could be reused to tell you whether this Fraction is less than, equal to, or greater than the other Fraction passed in? If you do this thoughtfully you can write it in a few lines of code re-using one of the arithmetic methods you wrote for the Fraction project last week.
--- PSEUDO CODE --- DIFF = THIS - OTHER IF DIFF GREATER THAN 0 THIS FRACTION IS GREATER THAN OTHER FRACTION ELSE IF DIFF EQUAL TO ZERO THIS FRACTION EQUAL TO OTHER FRACTION ELSE THIS FRACTION LESS THAN OTHER FRACTION
Notice you are given a new version of the toString method in your Fraction.java starter file not only echoes the numer and denom but also tacks on a double representation of the quotient. This double at the end of the print just makes it easier (by eye) to verify the values are sorted or not. Do not do the actual comparison via doubles.
Here is your FractionTester.java. and a stripped down to the bare bones Fraction.java file that will serve as a starting point.
You do not need to write any public methods that are not used by the main tester program. You may have to write private methods that are used internally to do the required job.
Your output MUST look exactly like this screen shot below:
public class Fraction implements Comparable
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
