By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,469 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,469 IT Pros & Developers. It's quick & easy.

help with interfaces

P: 15
THe question is:

Modify the RationalNumber Class so that it implements the Comparable interface. To perform the comparison, compute an equivalent floating point value from the numerator and denominator for both Rational objects, then compare them using a tolerence value of 0.0001. Write a main driver to test your modifications.

I tried by got an error (see last program)

// THE Original Program
// Rational.java Author: Lewis/Loftus

public class Rational
{
private int numerator, denominator;

public Rational (int numer, int denom)
{
if (denom == 0)
denom = 1;

// Make the numerator "store" the sign
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}

numerator = numer;
denominator = denom;

reduce();
}

public int getNumerator ()
{
return numerator;
}

public int getDenominator ()
{
return denominator;
}

public Rational reciprocal ()
{
return new Rational (denominator, numerator);
}

public Rational add (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;

return new Rational (sum, commonDenominator);
}

public Rational subtract (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;

return new Rational (difference, commonDenominator);
}

public Rational multiply (Rational op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();

return new Rational (numer, denom);
}

public Rational divide (Rational op2)
{
return multiply (op2.reciprocal());
}

public boolean equals (Rational op2)
{
return ( numerator == op2.getNumerator() &&
denominator == op2.getDenominator() );
}


public String toString ()
{
String result;

if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;

return result;
}

private void reduce ()
{
if (numerator != 0)
{
int common = gcd (Math.abs(numerator), denominator);

numerator = numerator / common;
denominator = denominator / common;
}
}

private int gcd (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;

return num1;
}
}


//MY PROGRAM

public class Rational implements Comparable

{
private int numerator, denominator;

public Rational (int numer, int denom)
{
if (denom == 0)
denom = 1;

if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}

numerator = numer;
denominator = denom;

reduce();
}


public int getNumerator ()
{
return numerator;
}

public int getDenominator ()
{
return denominator;
}

public Rational reciprocal ()
{
return new Rational (denominator, numerator);
}


public Rational add (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;

return new Rational (sum, commonDenominator);
}


public Rational subtract (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;

return new Rational (difference, commonDenominator);
}


public Rational multiply (Rational op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();

return new Rational (numer, denom);
}

public Rational divide (Rational op2)
{
return multiply (op2.reciprocal());
}


public boolean equals (Rational op2)
{
return ( numerator == op2.getNumerator() &&
denominator == op2.getDenominator() );
}

public String toString ()
{
String result;

if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;

return result;
}


private void reduce ()
{
if (numerator != 0)
{
int common = gcd (Math.abs(numerator), denominator);

numerator = numerator / common;
denominator = denominator / common;
}
}

private int gcd (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
}
public float compute ()
{
float value = getNumerator() / getDenominator();
return value;
}
public int compareTo(Rational op2)
{
if (Math.abs(compute() - op2.compute()) < .0001)
return 0;
if (compute() > op2.compute())
return + 1;
if (compute() < op2.compute())
return - 1;
}
}
THIS IS WHAT I HAVE AND I GET THE ERROR :Rational.java:7: Rational should be declared abstract; it does not define compareTo(java.lang.Object) in Rational. THANKS FOR THE HELP
Jan 25 '07 #1
Share this Question
Share on Google+
9 Replies


10K+
P: 13,264
THe question is:

Modify the RationalNumber Class so that it implements the Comparable interface. To perform the comparison, compute an equivalent floating point value from the numerator and denominator for both Rational objects, then compare them using a tolerence value of 0.0001. Write a main driver to test your modifications.

I tried by got an error (see last program)

// THE Original Program
// Rational.java Author: Lewis/Loftus

public class Rational
{
private int numerator, denominator;

public Rational (int numer, int denom)
{
if (denom == 0)
denom = 1;

// Make the numerator "store" the sign
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}

numerator = numer;
denominator = denom;

reduce();
}

public int getNumerator ()
{
return numerator;
}

public int getDenominator ()
{
return denominator;
}

public Rational reciprocal ()
{
return new Rational (denominator, numerator);
}

public Rational add (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;

return new Rational (sum, commonDenominator);
}

public Rational subtract (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;

return new Rational (difference, commonDenominator);
}

public Rational multiply (Rational op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();

return new Rational (numer, denom);
}

public Rational divide (Rational op2)
{
return multiply (op2.reciprocal());
}

public boolean equals (Rational op2)
{
return ( numerator == op2.getNumerator() &&
denominator == op2.getDenominator() );
}


public String toString ()
{
String result;

if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;

return result;
}

private void reduce ()
{
if (numerator != 0)
{
int common = gcd (Math.abs(numerator), denominator);

numerator = numerator / common;
denominator = denominator / common;
}
}

private int gcd (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;

return num1;
}
}


//MY PROGRAM

public class Rational implements Comparable

{
private int numerator, denominator;

public Rational (int numer, int denom)
{
if (denom == 0)
denom = 1;

if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}

numerator = numer;
denominator = denom;

reduce();
}


public int getNumerator ()
{
return numerator;
}

public int getDenominator ()
{
return denominator;
}

public Rational reciprocal ()
{
return new Rational (denominator, numerator);
}


public Rational add (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;

return new Rational (sum, commonDenominator);
}


public Rational subtract (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;

return new Rational (difference, commonDenominator);
}


public Rational multiply (Rational op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();

return new Rational (numer, denom);
}

public Rational divide (Rational op2)
{
return multiply (op2.reciprocal());
}


public boolean equals (Rational op2)
{
return ( numerator == op2.getNumerator() &&
denominator == op2.getDenominator() );
}

public String toString ()
{
String result;

if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;

return result;
}


private void reduce ()
{
if (numerator != 0)
{
int common = gcd (Math.abs(numerator), denominator);

numerator = numerator / common;
denominator = denominator / common;
}
}

private int gcd (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
}
public float compute ()
{
float value = getNumerator() / getDenominator();
return value;
}
public int compareTo(Rational op2)
{
if (Math.abs(compute() - op2.compute()) < .0001)
return 0;
if (compute() > op2.compute())
return + 1;
if (compute() < op2.compute())
return - 1;
}
}
THIS IS WHAT I HAVE AND I GET THE ERROR :Rational.java:7: Rational should be declared abstract; it does not define compareTo(java.lang.Object) in Rational. THANKS FOR THE HELP
Please use code tags next time when posting code.

When a class implements an interface, it signs a contract. The contract says that I shall have all the methods in the interface I am implementing otherwise I shall be declared abstract.
Now your Rational class implements Comparable meaning it must contain a method called with the signature

Expand|Select|Wrap|Line Numbers
  1. int compareTo (Object o) 
Jan 25 '07 #2

P: 15
would this work..and how would i put tolerance in the compareTo method

public double getDecimalEquivalent()
{
return (double)numerator/denominator;
}

public int compareTo(Object obj)
{
double TOLERANCE=0.0001;
Rational myRational = (Rational) obj;
double result = getDecimalEquivalent() -
myRational.getDecimalEquivalent();
if (result > 0)
return 1;
else if (result < 0)
return -1;
return 0;
}
Jan 25 '07 #3

P: 15
or would this work..

public double getDecimalEquivalent()
{
return (double)numerator/denominator;
}

public int compareTo(Object obj)
{
double tolerance=0.0001;
Rational myRational = (Rational) obj;
double result = getDecimalEquivalent() -
myRational.getDecimalEquivalent();
if (result < tolerance)
return 0;
else if (getDecimalEquivalent() >myRational.getDecimalEquivalent())
return +1;
else
return -1;
}
Jan 25 '07 #4

10K+
P: 13,264
would this work..and how would i put tolerance in the compareTo method

public double getDecimalEquivalent()
{
return (double)numerator/denominator;
}

public int compareTo(Object obj)
{
double TOLERANCE=0.0001;
Rational myRational = (Rational) obj;
double result = getDecimalEquivalent() -
myRational.getDecimalEquivalent();
if (result > 0)
return 1;
else if (result < 0)
return -1;
return 0;
}
You should use {} to make your if else statements more readable.
You forgot the tolerence value of 0.0001.
Jan 25 '07 #5

10K+
P: 13,264
or would this work..

public double getDecimalEquivalent()
{
return (double)numerator/denominator;
}

public int compareTo(Object obj)
{
double tolerance=0.0001;
Rational myRational = (Rational) obj;
double result = getDecimalEquivalent() -
myRational.getDecimalEquivalent();
if (result < tolerance)
return 0;
else if (getDecimalEquivalent() >myRational.getDecimalEquivalent())
return +1;
else
return -1;
}
Try

Expand|Select|Wrap|Line Numbers
  1. public int compareTo(Object obj) {
  2.         double tolerance=0.0001;
  3.         Rational myRational = (Rational) obj;
  4.         double result = getDecimalEquivalent() - myRational.getDecimalEquivalent();
  5.         if(Math.abs(result) > 0.0001) {
  6.             //not equal
  7.             if(getDecimalEquivalent() > myRational.getDecimalEquivalent()) {
  8.                 //bigger
  9.                 return 1;
  10.             }
  11.             else {
  12.                 //smaller
  13.                 return -1;
  14.             }
  15.         }
  16.         else {
  17.             //equal
  18.             return 0;
  19.         }
  20.     }
And tell me how it goes

P.S You are still forgetting to wrap your code with code tags
Jan 25 '07 #6

P: 15
it works now...thank you very much for your help
Jan 25 '07 #7

10K+
P: 13,264
it works now...thank you very much for your help
Good. Come back for help anytime. And don't forget to help others as well where you can.
Jan 26 '07 #8

P: 1
I was just assigned this program too, but when I use the code provided placed into the code seen in the sample here:

private int gcd (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;

return num1;
}

public int compareTo(Object obj)
{
double tolerance=0.0001;
RationalNumber myRational = (RationalNumber) obj;
double result = getDecimalEquivalent() -
myRational.getDecimalEquivalent();

if(Math.abs(result) > 0.0001)
{

if(getDecimalEquivalent() > myRational.getDecimalEquivalent())
{
return 1;
}

it does not compile. The error message says: cannot find symbol - method getDecimalEquivalent().

If anyone is still out there, please help!
Oct 16 '07 #9

Expert 10K+
P: 11,448
it does not compile. The error message says: cannot find symbol - method getDecimalEquivalent().

If anyone is still out there, please help!
Well, you forgot to copy/paste the getDecimalEquivalent() method or if you typed
it over you made a typo somewhere.

kind regards,

Jos
Oct 17 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.