Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with fixing an error dealing with interface implementation

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Need help with fixing an error dealing with interface implementation

    Hi everyone, I wrote the following code for a Fraction class that implements an interface called StdMathOps which consists of 5 methods called add, sub, div mult, and toString. I am getting a compilation error which says "Fraction is not abstract and does not override abstract method mult(Object) in StdMathOps." I do not intend for Fraction to be an abstract class as it does implement each of the 5 methods in the interface, and am wondering if anyone can help me in identifying what needs to be corrected. Any help would be greatly appreciated.

    Here is the interface:

     
    public interface StdMathOps 
    {
        public Object add(Object o);
        public Object sub(Object o);
        public Object div(Object o);
        public Object mult(Object o);
        @Override
        public String toString();
    }

    Here is the Fraction class:

     
    public class Fraction implements StdMathOps
    {
        private int num;
        private int den;
     
        public Fraction(int a, int b) 
        {
            num = a;
            den = b;
        }
     
        public Fraction() 
        {
            num = 1;
            den = 1;
        }
     
        @Override
        public String toString()
        {
            return num + "/" + den;
        }
     
        public double getDecimal()
        {
            double decimal = ((double)num / den);
            return decimal;
        }
     
        public String reduce()
        {   
            int n = num;
            int d = den;
     
            while(n != d)
            {
                if(n > d)
                    n = n - d;
     
                if(d > n)
                    d = d - n;
            }
            return((num / d) + "/" + (den / d));
        }
     
        public String toMixed()
        {
            int a = num;
            int b = den;
            int whole = num / den;
     
            while(a != b)
            {
                if(a > b)
                    a = a - b;
                if(b > a)
                    b = b - a;
            }
     
            Fraction g = new Fraction(num % den / b, den / b);
     
            if(whole > 0)
                return whole + " " + g.toString();
            else
                return g.toString();
        }
     
        public Fraction add(Fraction a)
        {
            int newNum = (((a.den * den) / a.den) * a.num) + (((den * a.den) / den) * num);
            int newDen = (a.den * den );
            Fraction x = new Fraction(newNum, newDen);
            return x;
        }
     
        public Fraction sub(Fraction a)
        {
            int newNum = ((num * a.den) - (a.num * den));
            int newDen = den * a.den;
            Fraction x = new Fraction (newNum, newDen);
            return x;
        }
     
        public Fraction div(Fraction a)
        {
            int newNum = num * a.den;
            int newDen = den * a.num;
            Fraction x = new Fraction (newNum, newDen);
            return x;
        }
     
        public Fraction mult(Fraction a)
        {
            int newNum = num * a.num;
            int newDen = den * a.den;
            Fraction x = new Fraction (newNum, newDen);
            return x;
        }
    }


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Need help with fixing an error dealing with interface implementation

    Hi jc,

    You need to actually have the function definition match that of the interface. So you should change from Fraction in the parameters and return to Object.

    Or, perhaps a smarter solution would be to use generics in the StdMathOps interface, such as the following. ( I'll let you work out the last part of the puzzle.... )

    public interface StdMathOps<T>
    {
        public T add(T o);
        public T sub(T o);
        public T div(T o);
        public T mult(T o);
        @Override
        public String toString();
    }

    If you would like more information about generics, then check this out: http://docs.oracle.com/javase/tutorial/java/generics/

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    jc100 (November 4th, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with fixing an error dealing with interface implementation

    Got it, thank you for your assistance.

Similar Threads

  1. Need help with fixing this error: ArrayIndexOutOfBoundsException: 1
    By mikuya707 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 13th, 2013, 02:23 PM
  2. Error: class, interface, or enum expected.
    By duderski in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2012, 02:26 PM
  3. [SOLVED] Error: Class,interface or enum expected
    By FJIW in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 18th, 2011, 03:08 PM
  4. I am having a hard time fixing this error
    By KuruptingYou in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 28th, 2011, 10:12 PM
  5. Interface Implementation
    By Samyx in forum Object Oriented Programming
    Replies: 1
    Last Post: December 2nd, 2009, 03:46 AM