473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help with interfaces

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
9 10009
r035198x
13,262 8TB
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
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
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
r035198x
13,262 8TB
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
r035198x
13,262 8TB
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
it works now...thank you very much for your help
Jan 25 '07 #7
r035198x
13,262 8TB
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
dubdos
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
JosAH
11,448 Expert 8TB
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

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

Similar topics

3
by: Peter Sparago | last post by:
(Sorry in advance for the long post.) Hi, I'm having a great deal of difficulty buiding a Python COM extension. I am using the MSHTML ActiveX control in my application but I need to interact...
0
by: David Dolheguy | last post by:
I am in desperate need to get help in answering some questions in regards to building a DCOM Server using C#. I need to create a DCOM server using C#.NET, I realise that you first need to create...
1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
5
by: Anand Ganesh | last post by:
Hi All, I need some help. I am sort of not sure how to approach this problem. I have a MAINPROGRAM. This is the core application. I have asked two of my staff to developed two different...
7
by: cider123 | last post by:
I'm coding a project using the following article as reference: http://www.codeproject.com/csharp/DynamicPluginManager.asp In this type of project, plugins are loaded dynamically into a Plugin...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
8
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface...
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.