1

I am doing a very basic Complex Numbers class in Java but when I test my add and multiply functions I don't get the results I expect. I don't understand what is wrong or how to fix it.

When I run the program I get the following output:

a+b: ComplexNumber@1540e19d

a*b: ComplexNumber@677327b6

I should get the proper addition and multiplication of two complex numbers (following the rules of complex numbers of course)

Any help would be much appreciated! Thanks in advance.

Here is the code:

public class ComplexNumber {

private double real;
private double imaginary;

public ComplexNumber(double r, double i) {
    real = r;
    imaginary = i;
}

public double real() {
    return real;
}

public double imaginary() {
    return imaginary;
}

public ComplexNumber add(ComplexNumber c) {
    double newr = real + c.real();
    double newi = imaginary + c.imaginary();
    return new ComplexNumber(newr, newi);
}
public ComplexNumber multiply(ComplexNumber c) {
    double newr = real*c.real() - imaginary*c.imaginary();
    double newi = real*c.imaginary() + imaginary*c.real();
    return new ComplexNumber(newr, newi);
}

 public static void main(String[] args) {

    ComplexNumber c1 = new ComplexNumber(1.0, 2.0);
    ComplexNumber c2 = new ComplexNumber(-1.0, 0.5);
    String c1plusc2 = c1.add(c2).toString();
    String c1timesc2 = c1.multiply(c2).toString();

    System.out.println("a+b :" + c1plusc2);
    System.out.println("a*b :" + c1timesc2);

    }
    }
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
CHANTAL COX
  • 73
  • 2
  • 13
  • You should really get into the habit of formatting your code properly. Especially since there are nowadays a lot of editors / IDEs that do it automatically. Otherwise you're showing disrespect to the people who read it. – Sergei Tachenov Jan 18 '16 at 15:22
  • @SergeyTachenov I actually wanted to do it but stackoverflow required 4 indentations in order to submit my code for each line and it took me forever. Any trick on how to move my code all 4 indents to the right at once? It is a hassle to do one line at a time! – CHANTAL COX Jan 18 '16 at 15:25
  • I usually just select it in my IDE (NetBeans, but many other will work), press Tab to shift it right, then press Ctrl-C and Ctrl-Z (to undo the shift). But really, even without that (suppose you don't have an IDE at hand), if it's properly formatted, then only the first and the last lines (`class` and `}`) will be screwed up. Then just add 4 spaces to these lines and leave the rest untouched, and you end up with almost properly formatted code (and nobody will really mind those two lines if it's just one class anyway). – Sergei Tachenov Jan 18 '16 at 15:31
  • Now you call `toString()` and then assign the result to `ComplexNumber`. It won't even compile because the types aren't compatible. You should call `toString()` only when printing the result and moreover, it is done implicitly, so simple `"a+b :" + c1plusc2` will work just as fine as `"a+b :" + c1plusc2.toString()`. – Sergei Tachenov Jan 18 '16 at 17:00
  • @ChantalMarin In questions and answers in StackOverflow, maybe not so much comments, you can highlight the code and press Ctrl+K – plainOldNerd Jan 19 '16 at 09:54

1 Answers1

6

You need to override the toString method in the ComplexNumber class:

@Override
public String toString() {
    return real + " + i*" + imaginary;
}

Your .add() & .multiply() methods return a ComplexNumber object. By default,

System.out.println("a*b :" + c1.multiply(c2));

evaluates to

System.out.println("a*b :" + c1.multiply(c2).toString());

The toString() method is inherited from the Object class (since all classes inherit from Object). And since you're not overriding it in the ComplexNumber class, you get the default return value from Object's toString() method:

ClassName@hashCodeOfTheObject

EDIT:

toString() returns a String. Change

ComplexNumber c1plusc2 = c1.add(c2).toString();
ComplexNumber c1timesc2 = c1.multiply(c2).toString();

to

String c1plusc2 = c1.add(c2).toString();
String c1timesc2 = c1.multiply(c2).toString();

Output:

a+b :0.0 + i*2.5
a*b :-2.0 + i*-1.5
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • I tend to agree, but this also means that you are not printing a value, you are just printing the String version of Type@memoryAddress – plainOldNerd Jan 18 '16 at 15:18
  • String version of what ? – Mohammed Aouf Zouag Jan 18 '16 at 15:24
  • objects in Java are seen as a Type (what class it is an instance of) at some memory address. So, in your case it is saying the method returned an object of type ComplexNumber that lives at memory address 1540e19d, so it prints out the String "ComplexNumer@1540e19d" – plainOldNerd Jan 18 '16 at 15:26
  • That's not a memory address actually, it's the **hashcode** of that particular `ComplexNumber` instance. – Mohammed Aouf Zouag Jan 18 '16 at 15:27
  • How do you know that? You have a reference/link/URL I can look at to verify that? I'm not saying I don't believe you, but it comes as a surprise. – plainOldNerd Jan 18 '16 at 15:32
  • @MohammedAoufZOUAG it still doesn't work even though i added the toString Method and added the toString in my SystemOutPrintln. Any idea? public static void main(String[] args) { ComplexNumber c1 = new ComplexNumber(1.0, 2.0); ComplexNumber c2 = new ComplexNumber(-1.0, 0.5); ComplexNumber c1plusc2 = c1.add(c2); ComplexNumber c1timesc2 = c1.multiply(c2); System.out.println("a+b :" + c1plusc2.toString()); System.out.println("a*b :" + c1timesc2.toString()); } – CHANTAL COX Jan 18 '16 at 15:36
  • 1
    @Mohammed Cheers, I guess the hash of the object contains all information, including meta-information, so no two would likely be the same, but I never guessed that. Thanks. – plainOldNerd Jan 18 '16 at 15:39
  • @CHANTALMARIN could you update the post with the full code ? – Mohammed Aouf Zouag Jan 18 '16 at 15:41
  • @plainOldNerd, since `hashCode` is native, I suppose it just boils down to the address or the hash code of the address on machines where address doesn't fit into 32 bits. Even the `hashCode` docs say: "This is typically implemented by converting the internal address of the object into an integer". – Sergei Tachenov Jan 18 '16 at 16:04
  • @MohammedAoufZOUAG Updated! Thanks for having a look :) – CHANTAL COX Jan 18 '16 at 16:31
  • @MohammedAoufZOUAG That makes sense but it still doesn't work. Any other suggestion? Thanks! (I also edited the code with the last modifications you suggested!) – CHANTAL COX Jan 18 '16 at 17:20
  • @CHANTALMARIN it's working fine for me, what seems to be the problem with the output ? – Mohammed Aouf Zouag Jan 18 '16 at 17:23
  • @MohammedAoufZOUAG I still get: a+b :ComplexNumber@1540e19d a*b :ComplexNumber@677327b6 – CHANTAL COX Jan 18 '16 at 17:32
  • There is no way... Check if you're compiling & running an old version of the file. – Mohammed Aouf Zouag Jan 18 '16 at 17:34
  • @SergeyTachenov So effectively (not quite, but almost), it's Class @ memory address. Anyway, thanks. I'll checck out the hashCode docs sometime soon. – plainOldNerd Jan 19 '16 at 09:40