class Complex {
  double real, img;
 
        void setValue(double i, double x){
                real=i;
                img=x;
        }
        public double getRealPart(){
                double a=real;
                return a;
        }
        public double getImagPart(){
                double b=img;
                return b;
        }
        public double getMagnitude(){
                double magn=Math.sqrt(real*real+img*img);
                return magn;
        }
        public Complex add(Complex c){
                return new Complex(real+c.real, img+c.img);
        }
        public Complex multiply(Complex c){
                return new Complex(real*c.real-img*c.img,real*c.img+img*c.real);
        }
        public String toString(){
                return "real part is" + real + "Imaginary part is" + img;
        }
}
public class TestComplex {

  public static void main (String[] argv)
  {
    Complex c1 = new Complex();
    Complex c2 = new Complex (4.35, 9.002); // (4.35, 9.002)
    Complex c3 = new Complex (8.93);        // (8.93, 0)
    Complex c4 = c1.add (c2);
    Complex c5 = c2.multiply (c3);
    Complex c6 = new Complex (4.35, 9.002);

    System.out.println ("c6 = " + c6);

    c1.setValue (1.0, 1.0);

    System.out.println ("c1's real part = " + c1.getRealPart()
                        + "\n" +
                        "c1's imag part = " + c1.getImagPart());
    System.out.println ("|c1| = " + c1.getMagnitude());

    //Complex c2 = new Complex();
    //c2.setValue (6.0, 8.0);

    System.out.println ("c2's real part = " + c2.getRealPart()
                        + "\n" +
                        "c2's imag part = " + c2.getImagPart());
    System.out.println ("|c2| = " + c2.getMagnitude());

    double magSum = c1.getMagnitude() + c2.getMagnitude();
    System.out.println ("|c1| + |c2| = " + magSum);
    
  }
}

I am getting the errors:

[32] $ javac TestComplex.java
TestComplex.java:21: cannot find symbol
symbol : constructor Complex(double,double)
location: class Complex
return new Complex(real+c.real, img+c.img);
^
TestComplex.java:24: cannot find symbol
symbol : constructor Complex(double,double)
location: class Complex
return new Complex(real*c.real-img*c.img,real*c.img+img*c.real);
...

Dani AI

Generated

Short summary for future readers: the compile-time failures came from calls to parameterized constructors that did not exist in Complex. and pointed this out; the practical fixes are to supply the appropriate constructors (or to change the call sites to use the no-arg object and then populate it). The code in the thread shows add and multiply returning new instances, so providing matching constructors is the cleanest fix.

A minimal constructor set that covers the usages shown in the test code is:

public Complex() {
  this(0.0, 0.0);
}

public Complex(double real) {
  this(real, 0.0);
}

public Complex(double real, double imag) {
  this.real = real;
  this.img = imag;
}

These let new Complex(4.35, 9.002) and new Complex(8.93) compile and keep the add/multiply calls valid.

Additional suggestions beyond the thread: make fields private (and final if you prefer immutability), use constructor chaining as shown, and prefer Math.hypot(real, img) for magnitude to reduce overflow/underflow issues. Improve toString formatting for readability, and implement equals/hashCode if instances will be compared or used in collections. ’s idea of forwarding values to a setter from a constructor works, but direct assignment or chaining is more idiomatic. Noting for completeness: after changes, recompile all affected files so the updated Complex class is used by TestComplex.

Recommended Answers

All 3 Replies

It's telling you that you can't use constructors that don't exist.

commented: Beat me to it lol :) +6

You have not specified a constructor for class Complex that accepts two doubles as parameters. There are two things you could do:

1- Rename void "setValue" to "public Complex" and give the Complex constructor two doubles as arguments.

Or,

2- Declare a two argument constructor that sends the two doubles to the method setValue.

I hope this helps.

sorry for the late reply, i was kind busy with school and work.

Thank you for ur help, I followed your instructions and got the problem fixed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.