Question
Help in JAVA: Need help fixing my compareTo method. The method should compare two fractions to one another and if A > B return1, if
Help in JAVA: Need help fixing my compareTo method. The method should compare two fractions to one another and if A > B return1, if A < B return -1 and if they're equal to eachother return 0. I have my fraction below with a compareTo method but I run into a few problems please help me fix my method.
Fraction.java:
public static class Fraction implements Comparable
denominator = 1; } //Constructor public Fraction(int num, int denom) { this.numerator = num; this.denominator = denom; } //Getters and Setters public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
} //toString to print public String toString() {
return numerator + "/" + denominator;
} //compareTo public int compareTo(Fraction f) { if ((f.denominator*this.numerator)>(f.numerator*this.denominator))
return 1;
else if ((f.denominator*this.numerator)<(f.numerator*this.denominator))
return -1;
else
return 0; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started