-1

I'm getting an error I don't quite understand. My goal is to create a simple program that adds, subtracts, multiplies, and divides complex numbers. My ComplexNumber class compiles correctly, but when I try to compile with the first test case of my Tester class it gives me this error:

ComplexNumberTest.java:37: error: method add in class ComplexNumber cannot be applied to given types;
    assertEquals(test1.add()==0);
                      ^
  required: ComplexNumber
  found: no argument
  reason: actual and formal arguments differ in length

This is my add method

import java.lang.ArithmeticException;

public class ComplexNumber{
private float a; //real
private float b; //imaginary

public ComplexNumber(float a,float b)
{
    this.a = a;
    this.b = b;
} //end constructor



public ComplexNumber add(ComplexNumber otherNumber){
    ComplexNumber newComplex;
    float newA = a + otherNumber.getA();
    float newB = b + otherNumber.getB();
    newComplex = new ComplexNumber(newA, newB);
    return newComplex;
}//end add

This is my Tester class

import junit.framework.TestCase;

public class ComplexNumberTest extends TestCase{

private ComplexNumber test1;
private ComplexNumber test2;
private ComplexNumber test3;
private ComplexNumber test4;
private ComplexNumber test5;


public void setUp(){
    test1 = new ComplexNumber (1,-1);
    test2 = new ComplexNumber(2,2);
    test3 = new ComplexNumber(0,2);
    test4 = new ComplexNumber(3,1);
    test5 = new ComplexNumber(4,4);
}//end set up


/**
 * A method used to test the add method.
 * add two Complex numbers together
 * (ai+bi)=a+bi
 * 
 **/ 
public void testAdd()
{
    assertEquals(test1.add()==0);
    //assertTrue(test2.add()==4);
    //assertEquals(test3.add()==2);
    //assertEquals(test4.add()==3);
    //assertEquals(test5.add()==8);
}//end testAddition

I feel like the solution is fairly simple and I've just been staring at it for too long. Thanks for any advice.

Klate
  • 27
  • 4

1 Answers1

3

The error says it all:

ComplexNumberTest.java:37: error: method add in class ComplexNumber cannot be applied to given types;
    assertEquals(test1.add()==0);
                      ^
  required: ComplexNumber
  found: no argument
  reason: actual and formal arguments differ in length

It is saying that it found no argument when you tried to use add(). It also says that it requires a ComplexNumber in the argument.

public ComplexNumber add(ComplexNumber otherNumber){
    ComplexNumber newComplex;
    float newA = a + otherNumber.getA();
    float newB = b + otherNumber.getB();
    newComplex = new ComplexNumber(newA, newB);
    return newComplex; }//end add

You defined add to require another ComplexNumber to be passed as an argument which you haven't done when you used it as test1.add().

Additionally, based on the method signature of add, it will never return 0 as you have assumed in your assertion.

David Yee
  • 3,515
  • 25
  • 45
  • Okay, I see that now. So if I did something like test1.add(test2)==(3,1), the format would still be wrong though, correct? In my head, it works like (a1, b1) + (a2,b2) = (a1+a2), (b1+b2). but when I compile this it is expecting additional parentheses. – Klate Feb 04 '16 at 23:30
  • @Klate Yes, because your `add` method is returning a `ComplexNumber` object and `(3,1)` is invalid syntax. If you want to compare the return to another `ComplexNumber`, you need to implement the [`equals` and `hashCode` methods](http://stackoverflow.com/q/27581/2557554) for `ComplexNumber`. – David Yee Feb 04 '16 at 23:33
  • I knew I was missing something. I completely left out the equals to method. Thank you for your help – Klate Feb 04 '16 at 23:39
  • @Klate Please accept this answer if it answered your question. – David Yee Feb 04 '16 at 23:41