help with interfaces

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xxbabysue123xx
    New Member
    • Jan 2007
    • 15

    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 commonDenominat or = denominator * op2.getDenomina tor();
    int numerator1 = numerator * op2.getDenomina tor();
    int numerator2 = op2.getNumerato r() * denominator;
    int sum = numerator1 + numerator2;

    return new Rational (sum, commonDenominat or);
    }

    public Rational subtract (Rational op2)
    {
    int commonDenominat or = denominator * op2.getDenomina tor();
    int numerator1 = numerator * op2.getDenomina tor();
    int numerator2 = op2.getNumerato r() * denominator;
    int difference = numerator1 - numerator2;

    return new Rational (difference, commonDenominat or);
    }

    public Rational multiply (Rational op2)
    {
    int numer = numerator * op2.getNumerato r();
    int denom = denominator * op2.getDenomina tor();

    return new Rational (numer, denom);
    }

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

    public boolean equals (Rational op2)
    {
    return ( numerator == op2.getNumerato r() &&
    denominator == op2.getDenomina tor() );
    }


    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(numer ator), 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 commonDenominat or = denominator * op2.getDenomina tor();
    int numerator1 = numerator * op2.getDenomina tor();
    int numerator2 = op2.getNumerato r() * denominator;
    int sum = numerator1 + numerator2;

    return new Rational (sum, commonDenominat or);
    }


    public Rational subtract (Rational op2)
    {
    int commonDenominat or = denominator * op2.getDenomina tor();
    int numerator1 = numerator * op2.getDenomina tor();
    int numerator2 = op2.getNumerato r() * denominator;
    int difference = numerator1 - numerator2;

    return new Rational (difference, commonDenominat or);
    }


    public Rational multiply (Rational op2)
    {
    int numer = numerator * op2.getNumerato r();
    int denom = denominator * op2.getDenomina tor();

    return new Rational (numer, denom);
    }

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


    public boolean equals (Rational op2)
    {
    return ( numerator == op2.getNumerato r() &&
    denominator == op2.getDenomina tor() );
    }

    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(numer ator), 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(Ratio nal op2)
    {
    if (Math.abs(compu te() - 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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by xxbabysue123xx
    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 commonDenominat or = denominator * op2.getDenomina tor();
    int numerator1 = numerator * op2.getDenomina tor();
    int numerator2 = op2.getNumerato r() * denominator;
    int sum = numerator1 + numerator2;

    return new Rational (sum, commonDenominat or);
    }

    public Rational subtract (Rational op2)
    {
    int commonDenominat or = denominator * op2.getDenomina tor();
    int numerator1 = numerator * op2.getDenomina tor();
    int numerator2 = op2.getNumerato r() * denominator;
    int difference = numerator1 - numerator2;

    return new Rational (difference, commonDenominat or);
    }

    public Rational multiply (Rational op2)
    {
    int numer = numerator * op2.getNumerato r();
    int denom = denominator * op2.getDenomina tor();

    return new Rational (numer, denom);
    }

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

    public boolean equals (Rational op2)
    {
    return ( numerator == op2.getNumerato r() &&
    denominator == op2.getDenomina tor() );
    }


    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(numer ator), 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 commonDenominat or = denominator * op2.getDenomina tor();
    int numerator1 = numerator * op2.getDenomina tor();
    int numerator2 = op2.getNumerato r() * denominator;
    int sum = numerator1 + numerator2;

    return new Rational (sum, commonDenominat or);
    }


    public Rational subtract (Rational op2)
    {
    int commonDenominat or = denominator * op2.getDenomina tor();
    int numerator1 = numerator * op2.getDenomina tor();
    int numerator2 = op2.getNumerato r() * denominator;
    int difference = numerator1 - numerator2;

    return new Rational (difference, commonDenominat or);
    }


    public Rational multiply (Rational op2)
    {
    int numer = numerator * op2.getNumerato r();
    int denom = denominator * op2.getDenomina tor();

    return new Rational (numer, denom);
    }

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


    public boolean equals (Rational op2)
    {
    return ( numerator == op2.getNumerato r() &&
    denominator == op2.getDenomina tor() );
    }

    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(numer ator), 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(Ratio nal op2)
    {
    if (Math.abs(compu te() - 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

    Code:
    int compareTo (Object o)

    Comment

    • xxbabysue123xx
      New Member
      • Jan 2007
      • 15

      #3
      would this work..and how would i put tolerance in the compareTo method

      public double getDecimalEquiv alent()
      {
      return (double)numerat or/denominator;
      }

      public int compareTo(Objec t obj)
      {
      double TOLERANCE=0.000 1;
      Rational myRational = (Rational) obj;
      double result = getDecimalEquiv alent() -
      myRational.getD ecimalEquivalen t();
      if (result > 0)
      return 1;
      else if (result < 0)
      return -1;
      return 0;
      }

      Comment

      • xxbabysue123xx
        New Member
        • Jan 2007
        • 15

        #4
        or would this work..

        public double getDecimalEquiv alent()
        {
        return (double)numerat or/denominator;
        }

        public int compareTo(Objec t obj)
        {
        double tolerance=0.000 1;
        Rational myRational = (Rational) obj;
        double result = getDecimalEquiv alent() -
        myRational.getD ecimalEquivalen t();
        if (result < tolerance)
        return 0;
        else if (getDecimalEqui valent() >myRational.get DecimalEquivale nt())
        return +1;
        else
        return -1;
        }

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by xxbabysue123xx
          would this work..and how would i put tolerance in the compareTo method

          public double getDecimalEquiv alent()
          {
          return (double)numerat or/denominator;
          }

          public int compareTo(Objec t obj)
          {
          double TOLERANCE=0.000 1;
          Rational myRational = (Rational) obj;
          double result = getDecimalEquiv alent() -
          myRational.getD ecimalEquivalen t();
          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.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by xxbabysue123xx
            or would this work..

            public double getDecimalEquiv alent()
            {
            return (double)numerat or/denominator;
            }

            public int compareTo(Objec t obj)
            {
            double tolerance=0.000 1;
            Rational myRational = (Rational) obj;
            double result = getDecimalEquiv alent() -
            myRational.getD ecimalEquivalen t();
            if (result < tolerance)
            return 0;
            else if (getDecimalEqui valent() >myRational.get DecimalEquivale nt())
            return +1;
            else
            return -1;
            }
            Try

            Code:
            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

            Comment

            • xxbabysue123xx
              New Member
              • Jan 2007
              • 15

              #7
              it works now...thank you very much for your help

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by xxbabysue123xx
                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.

                Comment

                • dubdos
                  New Member
                  • Oct 2007
                  • 1

                  #9
                  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(Objec t obj)
                  {
                  double tolerance=0.000 1;
                  RationalNumber myRational = (RationalNumber ) obj;
                  double result = getDecimalEquiv alent() -
                  myRational.getD ecimalEquivalen t();

                  if(Math.abs(res ult) > 0.0001)
                  {

                  if(getDecimalEq uivalent() > myRational.getD ecimalEquivalen t())
                  {
                  return 1;
                  }

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

                  If anyone is still out there, please help!

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by dubdos
                    it does not compile. The error message says: cannot find symbol - method getDecimalEquiv alent().

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

                    kind regards,

                    Jos

                    Comment

                    Working...