hi everyone. well I have a small problem. Actually, I have an interface (ComplexNum) and a class (ComplexNumb).The end result is to do small calculation with some complex numbers. I have already implemented the interface in the class , but eclipse keeps on telling me that I have to implement one function (which is already present ) in the class.Can you please help me out??

public interface ComplexNum {

    public int getReal();
    public int getComplex();

    public void setReal(int r);
    public void setComplex(int c);

    public String add();
    public String subtract();
    public String multiply();

    public void display();
}

public class ComplexNumb implements ComplexNum{

    private int real;
    private int comp;

    public ComplexNumb(){
        real=0;
        comp=0;
    }

    public ComplexNumb(int rl,int cmp){
        real=rl;
        comp=cmp;
    }
    public int getReal(){return real;}
    public int getComplex(){return comp;}
    public void setReal(int rt){real=rt;}
    public void setComplex(int cp){comp=cp;}

    public String add (ComplexNumb a, ComplexNumb b){
        int x=a.getReal()+ b.getReal();
        int y=a.getComplex()+b.getComplex();
        //String ys ;
        String ys =y  + "i"  ;
        System.out.println( "("   +    x   +   ','   +   ys   +   ')'    );
        return ( "("   +    x   +   ','   +   ys   +   ')'    );

    }
    public String subtract(ComplexNumb a, ComplexNumb b){
        int x=a.getReal()-b.getReal();
        int y=a.getComplex()-b.getComplex();
         String yst=y +"i";
        System.out.println( '(' +x+ ',' + y + ')');
        return ( "(" +  x  + "," +   yst    + ")"     );
    }
    public String multiply(ComplexNumb a, ComplexNumb b){
        int x=a.getReal()*b.getReal() - a.getComplex()*b.getComplex();
        int y=a.getReal()*b.getComplex() + a.getComplex()*b.getReal() ;
        String ym= y + "i";
        System.out.println( "(" + x + "," + ym + ")");
        return ( "(" + x + "," + ym + ")");
    }

    public void display(ComplexNumb a, ComplexNumb b){
        String f=add(a,b);
        String g=subtract(a,b);
        String h=multiply(a,b);

        System.out.println(f);
        System.out.println(g);
        System.out.println(h);

    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

If you are talking about the void display() then no you haven't implemented it.

public void display(ComplexNumb a, ComplexNumb b){
String f=add(a,b);
String g=subtract(a,b);
String h=multiply(a,b);

System.out.println(f);
System.out.println(g);
System.out.println(h);

}
I don't really understand what you said about not implementing the display method. Can you please be more explicit. And as far as the parameters are concerned .....is everything ok with them?

The interface has this method: void display() The class has this method: void display(ComplexNumb a, ComplexNumb b) So you don't implement the void display() method. You just declared another method that happens to have the same name, but it is not the same method.

I thought I were doing some kind on method overloading here, just like for the add,subtract and multiply methods. But why is the display method different from the other methods....is it because of the void return type??

And in eclipse, the error was saying that I did not implement the method multiply from the interface I were implementing.Why is it so?

Thanks a lot for your concern!

The same thing I told you about the display method applies to all:
These method(int i); and method(); are not the same methods.
So you must implement exactly the same method at the class.

Therefore, there is no way of overloading the methods in the interface?

I've been thinking of another way of passing parameters to the methods in the interface itself...but unfortunately I were unsuccessful.

For instance, the methods implemented in the class take as parameter an object...but how can we specify this in the interface?How are we going to say in the interface, that an object needs to be passed as parameter. More importantly, what will be its type, since it is going to be extended in the class??
Help me out please.

Thanks.

If you decide how the interface would be written try this and tell me:

public interface ComplexNum {

public int getReal();
public int getComplex();

public void setReal(int r);
public void setComplex(int c);

public String add(ComplexNum a, ComplexNum b);
public String subtract(ComplexNum a, ComplexNum b);
public String multiply(ComplexNum a, ComplexNum b);

public void display(ComplexNum a, ComplexNum b);
}

Also I believe that the last methods: (add, ..., display), should be static. Because you don't use any of the attributes and they do nothing to change or display the state of the instance. They just take objects as parameters and use them.

Sorry to bother you once more...I just have a last small question. The way an object is pass as argument in the interface is perfectly clear. But in the class that is going to implement that interface....what the object that is going to be passed as argument to the methods implemented will be of what type....ComplexNum or ComplexNumb??

Or do we just write it the same way as:

public String add(ComplexNum a, ComplexNum b);
public String subtract(ComplexNum a, ComplexNum b);
public String multiply(ComplexNum a, ComplexNum b);
public void display(ComplexNum a, ComplexNum b);

Snice in the inerface it is like this: public void display(ComplexNum a, ComplexNum b) Then the same must be in the class.

public String subtract(ComplexNum a, ComplexNum b){
int x=a.getReal()-b.getReal();
int y=a.getComplex()-b.getComplex();
String yst=y +"i";
System.out.println( '(' +x+ ',' + y + ')');
return ( "(" + x + "," + yst + ")" );
}

But you don't have to worry, because the inerface ComplexNum has all the methods you are using in that method.

I would like to add that those methods (add, multiply, ...) need to be static. That's because they do nothing to the state of the class they are in.
The class ComplexNumb has some attributes real, comb and methods that use them (getReal, setReal, ..)
But the other methods don't use those values, they simply add 2 numbers. So they have nothing to do with the instance itself. In one of those, you add two numbers. What happens in there has nothing to do with the values of real, comb of the class itself. The numbers that take as arguments are irrelevant to the rest of the object.

They could be in a separate class and they don't have to be in the interface:

public interface ComplexNum {

public int getReal();
public int getComplex();

public void setReal(int r);
public void setComplex(int c);
}

MathUtil

public class MathUtil {
   public static String add(ComplexNum a, ComplexNum b) {
      int x=a.getReal()+ b.getReal();
      int y=a.getComplex()+b.getComplex();
     String ys =y + "i" ;
     System.out.println( "(" + x + ',' + ys + ')' );
      return ( "(" + x + ',' + ys + ')' );
   }  
}

Inside the method you use methods of the ComplexNum interface. What is inside does not affect the the class that the method is inside so it is static. The fact that I put it in another class it is optional, but better design.
Now you can do this:

SomeClass a1 = new SomeClass();
SomeClass b1 = new SomeClass();
MathUtil.add(a1, b1);

AnotherClass a2 = new AnotherClass();
AnotherClass b2 = new AnotherClass();
MathUtil.add(a2, b2);

MathUtil.add(a1, b2);

The SomeClass, AnotherClass must implement the ComplexNum interface. That is the idea of interfaces. You put only methods that you will use (getReal, getComp) and use it in any way you want: add(ComplexNum, )
Then that method will work for any class that implements that inerface

I thank you very much for all your notes.
You are better than my professor.
Thanks again!!

This question has already been answered. Start a new discussion instead.