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
9 10009
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
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;
}
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;
}
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.
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 - public int compareTo(Object obj) {
-
double tolerance=0.0001;
-
Rational myRational = (Rational) obj;
-
double result = getDecimalEquivalent() - myRational.getDecimalEquivalent();
-
if(Math.abs(result) > 0.0001) {
-
//not equal
-
if(getDecimalEquivalent() > myRational.getDecimalEquivalent()) {
-
//bigger
-
return 1;
-
}
-
else {
-
//smaller
-
return -1;
-
}
-
}
-
else {
-
//equal
-
return 0;
-
}
-
}
And tell me how it goes
P.S You are still forgetting to wrap your code with code tags
it works now...thank you very much for your help
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.
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!
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |