-1

I need help writing a static version of the add and subtract methods. i have tried to do so but im not really sure if this is the right way and also my equals method is not working . When i compile my equals method it always prints false.

public static void main(String[] args) {
    Complex c1 = new Complex(7, 7);
    Complex c2 = new Complex(7, 7);

    System.out.println(Complex.add(c1, c2));
    System.out.println(c1.equals(c2));

}

}

class Complex {

private static double realPart;
private static double imaginaryPart;

public Complex() {
    realPart = 0;
    imaginaryPart = 0;

}

public Complex(double c1, double c2) {
    realPart = c1;
    imaginaryPart = c2;

}

public static Complex add(Complex firstNumber, Complex secondNumber) {

    Complex result = new Complex();
    firstNumber.realPart = 7;
    secondNumber.imaginaryPart = 7;
    result.realPart = firstNumber.realPart + secondNumber.imaginaryPart;

    return result;

}

public static Complex subtract(Complex firstNumber, Complex secondNumber) {
    Complex result = new Complex();
    firstNumber.realPart = 7;
    secondNumber.imaginaryPart = 7;
    result.realPart = firstNumber.realPart + secondNumber.imaginaryPart;
    return result;
}

public boolean equals(Complex firstNumber, Complex secondNumber) {

    if (firstNumber == secondNumber) {
        return true;
    } else {
        return false;
    }

}

public String toString() {

    return "result= " + realPart;
}
}
Dee
  • 115
  • 1
  • 1
  • 8

2 Answers2

1

Comparing Objects does not work. You need to compare the member variables within the Object. I believe that if you compare objects, all you are are comparing is the location the object is stored.

Next thing is...I don't think the realPart value and imaginaryPart value should be static. If you want each Complex Object to have a unique realPart and imaginaryPart value, it should just be a member variable.

kevkev97kk
  • 66
  • 1
  • 5
1

Objects are always compared using equals() method.

== isn't the way to compare Objects!!!

(THIS HAS BEEN POSTED ABOUT HUNDREDS OF TIMES OR MORE ON STACK OVERFLOW FORUM)

Also,add an @Override annotation to be more clear so that you're overriding the equals method...

Improve your code as :-

@Override
public boolean equals(Complex firstNumber, Complex secondNumber) {
if (firstNumber.equals(secondNumber)) {
    return true;
} else {
    return false;
}

}
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • but it still returns false . i would like to know why is it doing that? – Dee Nov 11 '14 at 03:45
  • 1
    It's so because objects will be equal only when they reference to the same locations. You just set firstNumber=SecondNumber; ANd then try equating using the answer I mentioned,then you'll come to know why it's so...If you want the other thing,go with the first answer as mentioned to compare the elements of both the objects to equate them! – Am_I_Helpful Nov 11 '14 at 03:47
  • @Dee-An upvote would be more favoured and helpful. THANKS IN ADVANCE... – Am_I_Helpful Nov 11 '14 at 19:15
  • @Dee- Chck this---> http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow – Am_I_Helpful Nov 11 '14 at 19:17