Question: 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

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

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.

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.

Here is the FractionTester.java:

import java.io.*; import java.util.*; public class FractionTester { public static void main( String args[] ) throws Exception { Random generator = new Random( 17 ); // POPULATE AN ARRAYLIST OF FRACTIONS WITH RANDOM VALUES ArrayList AList = new ArrayList(); for (int i=5 ; i>=0 ; --i ) AList.add( new Fraction(1+generator.nextInt(100), 1+generator.nextInt(100)) ); // numer and denom in [1..99] // MAKE DEEP COPY OF THOSE FRACTIONS IN THE ARAYLIST ABOVE, BUT STORE INTO PLAIN ARRAY OF FRACTION Fraction[] plainArr = new Fraction[ AList.size() ]; for ( int i = 0 ; i  
public class Fraction implements Comparable { private int numer; private int denom; // ACCESSORS (SETTERS) public int getNumer() { return numer; } public int getDenom() { return denom; } // MUTATORS (GETTERS) public void setNumer( int n ) { numer = n; } public void setDenom( int d ) { if (d==0) { System.out.println("Can't have 0 in denom"); System.exit(0); } else denom=d; } // FULL CONSTRUCTOR - an arg for each class data member public Fraction( int n, int d ) { int gcd = gcd( n, d ); setNumer(n/gcd); setDenom(d/gcd); } // GIVING YOU A WORKING (ITERATIVE) GCD J.I.C. YOU NEVER GOT YOURS FROM PROJ 7 TO WORK. private int gcd( int n, int d ) { int gcd = n if (n 0 ) { if (n%gcd==0 && d%gcd==0) return gcd; else --gcd; } return 1; // they were co-prime no GCD exceopt 1 :( } // REQUIRED BY THE COMPARABLE INTERFACE // if this == other return 0; if this > other return positive # ; else return negative # public int compareTo( Fraction other ) { // solution code is half the lines of this comment block! // you are only allowed to define one variable in this method // that variable should be a Fraction whose value is this - other // copy in your subtract() method from project 7 // now you can just examin the numer and denom of your diff fraction // to determine that fraction is postive negative or 0 // NO OTHER VARIABLES Of ANY KIND // NO DOUBLES, NO CASTING return 0; // REPLACE WITH YOUR CODE } public String toString() // USE AS IS. DO NOT DELETE OR MODIFY { return getNumer() + "/" + getDenom() + "\t=" + + ((double)getNumer()/(double)getDenom()); } 
}// EOF
  Your task for Lab#8 is to modify the Fraction class so that
Your output MUST look exactly like this screen shot below: Command Prompt C:\Users\tim\Desktop>java FractionTester rrayList OF FRACTIONS UNSORTED: 11/3 -3.6666666666666665 95/17 5.588235294117647 93/94 0.9893617021276596 5/16 0.3125 21.0 0.6666666666666666 2/3 plainArr oF FRACTIONS UNSORTED: 1/33.6666666666666665 95/17 5.588235294117647 93/94 0.9893617021276596 5/16 0.3125 .0 -0.6666666666666666 2/3 rrayList oF FRACTIONS SORTED: 5/16 =0.3125 2/3 93/940.9893617021276596 11/3 3.666666666666666!5 95/17 5.588235294117647 0.6666666666666666 7.0 plainArr oF FRACTIONS SORTED: 5/16 -0.312!5 2/3 93/94 0.9893617021276596 11/3 3.6666666666666665 95/17 5.588235294117647 =0.6666666666666666

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!