Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 { //Attributes private int numerator; private int denominator; //Default Constructor public Fraction() { numerator = 1;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions

Question

The nature and importance of the global marketplace.

Answered: 1 week ago