In: Computer Science
Assignment 4, Fraction Comparable
Instructions
For this assignment, you will be updating the Fraction class from Assignment 1. To get started, you can either make a copy of your Assignment 1 Fraction.java or download the Fraction.java template at the bottom of this assignment.
You will need to update Fraction so that it implements the Comparable interface. This will require adding an implements statement to the class declaration as well as the compareTo method. You will also add a simplify method which reduces a fraction to its lowest terms and, to help you with this, a static method gcd which finds the greatest common divisor of two integers.
The requirements of the methods to be added are as follows:
When you have successfully written your simplify method, you should add calls to this method at the end of the Fraction(int n, int d) constructor and the add method. This will ensure that fractions are always stored in their simplest possible form.
To test your code, download the runner class student_fraction_runner.java (Links to an external site.) into the same folder that holds your Fraction.java. Execute the method student_fraction_runner.main, and verify that the output matches the sample run listed below. Remember to change the runner to test with other values to make sure your program fits all the requirements. We will use a similar but different runner to grade the program.
When you are ready, paste your entire Fraction.java class in the box below, click run to test the output, and click submit when you are satisfied with your results.
Note: once you have completely finished the assignment feel free to add more methods (e.g. multiply) or extend the functionality of your Fraction class to include, for example, negative fractions. Make sure you submit your assignment first before making any changes like this.
Sample Run
Fraction 1: 4/5 Fraction 2: 3/2 Fraction 3: 4/5 Compare fraction 1 to fraction 2: -1 Compare fraction 2 to fraction 1: 1 Compare fraction 1 to fraction 3: 0 Compare fraction 3 to fraction 1: 0 Compare fraction 2 to fraction 3: 1 Compare fraction 3 to fraction 2: -1
Milestones
As you work on this assignment, you can use the milestones below to inform your development process:
Milestone 1: Write the static gcd method and test this by calling it on pairs of integers from another class.
Milestone 2: Write the simplify method (you will probably find it useful to call the gcd method). Add calls to this method at the end of the constructor and the add method.
Milestone 3: Implement the Comparable interface and write the method compareTo. Download the runner file and ensure the results are as expected.
// Please find the required solution
class Fraction implements Comparable {
// define attributes for n and d
private int n;
private int d;
// constructor for Fraction with n, d
public Fraction(int n, int d) {
this.n = n;
this.d = d;
// invoke simplify from constructor
simplify();
}
// define getters
public int getN() {
return n;
}
public int getD() {
return d;
}
// to string representation of fraction
@Override
public String toString() {
return n + "/" + d;
}
// compares 2 fraction
@Override
public int compareTo(Object o) {
Fraction c = (Fraction) o;
return Integer.compare(n * c.d, c.n * d);
}
// returns gcd of 2 numbers
public static int gcd(int x, int y) {
if (y != 0)
return gcd(y, x % y);
else
return x;
}
// simplify fraction using the following method
void simplify() {
int gcd = gcd(n, d);
n = n / gcd;
d = d / gcd;
}
}
// sample student runner class
public class student_fraction_runner {
public static void main(String str[]) {
Fraction fraction1 = new Fraction(4, 5);
System.out.println("Fraction 1: " + fraction1);
Fraction fraction2 = new Fraction(3, 2);
System.out.println("Fraction 2: " + fraction2);
Fraction fraction3 = new Fraction(4, 5);
System.out.println("Fraction 3: " + fraction3);
System.out.println();
System.out.println("Compare fraction 1 to fraction 2: " + fraction1.compareTo(fraction2));
System.out.println("Compare fraction 2 to fraction 1: " + fraction2.compareTo(fraction1));
System.out.println();
System.out.println("Compare fraction 1 to fraction 3: " + fraction1.compareTo(fraction3));
System.out.println("Compare fraction 3 to fraction 1: " + fraction3.compareTo(fraction1));
System.out.println();
System.out.println("Compare fraction 2 to fraction 3: " + fraction2.compareTo(fraction3));
System.out.println("Compare fraction 3 to fraction 2: " + fraction3.compareTo(fraction2));
System.out.println();
Fraction fraction4 = new Fraction(12, 18);
System.out.println("Fraction 4: " + fraction4);
}
}
sample screenshot:
