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 { 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); } // COPY CONSTRUCTOR - takes ref to some already initialized Comparable object public Fraction( Fraction other ) { this( other.getNumer(), other.getDenom() ); // call my full C'Tor with other Fraction's data } // 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 ) { // define a Fraction that is this - other // HINT: 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

Command Prompt C:Users\tim\Desktop>java FractionTester ArrayList OF FRACTIONS UNSORTED: 11/3 3. 6666666666666665 95/17 5.588235294117647 93/940.9893617021276596 5/16 0.312!5 2/3 0.6666666666666666 plainArr oF FRACTIONS UNSORTED: 11/3 3.6666666666666665 95/17 =5. 588235294117647 93/940.9893617021276596 5/16 =0.3125 .0 -0.6666666666666666 2/3 ArrayList OF FRACTIONS SORTED: 5/16 0.3125 2/3 93/940.9893617021276596 11/3 3.666666666666666!5 95/17-5.588235294117647 =0.6666666666666666 27.0 plainArr oF FRACTIONS SORTED: 5/16 0.3125 2/3 93/940.9893617021276596 11/33.6666666666666665 95/17 =5.588235294117647 -0.6666666666666666 c:users\tim\Desktop>

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!