Hi everyone,

I am a beginner in java. I am still trying to learn the structure and other basic stuff.
I am trying to add some code so that the methods in main() work correctly.

class Complex {
  double real, img;

        void setValues(double i, double x){
                real=i;
                img=x;
        }

        public double getRealPart(){
                double a=real;
        }

        public double getImagPart(){
                double b=img;
        }

        public double getMagnitude(){
                double magn=sqrt(real*real+img*img);
        }

public class TestComplex {

  public static void main (String[] argv)
  {
    Complex c1 = new Complex();
    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());
  }
}

what is wrong with my code?

Dani AI

Generated

Good catch by and thanks to for posting the working fix. Below are a few concise, practical improvements to turn that learning example into a safer, more reusable Complex value type and to avoid similar runtime or numeric pitfalls later.

Make the fields private (preferably final) and initialize via a constructor so instances are immutable and never partially initialized. For magnitude use Math.hypot(real, imag) instead of Math.sqrt(real*real + imag*imag); hypot handles large and tiny values more robustly. Provide small operations (add, multiply) that return new Complex objects rather than mutating state.

Example implementation sketch:

public final class Complex {
  private final double real;
  private final double imag;

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

  public double getReal() { return real; }
  public double getImag() { return imag; }

  public double magnitude() {
    return Math.hypot(real, imag);
  }

  public Complex add(Complex other) {
    return new Complex(real + other.real, imag + other.imag);
  }

  @Override
  public String toString() {
    return real + (imag >= 0 ? "+" : "") + imag + "i";
  }
}

Additional notes: keep method names consistent (avoid typos like setValue vs setValues), avoid unnecessary local variables in simple getters, and remember a public class must be in a file with the same name (e.g., TestComplex.java for public class TestComplex). For the magnitude detail see the Java docs for Math.hypot [https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#hypot(double,double)].

Recommended Answers

All 4 Replies

Lines 9, 13, 17 - These functions are not void. They must return something.

Line 21 - I assume that TestComplex is not intended to be an inner class. If not, you need a bracket to end the Complex class on line 20.

Lines 26, 34 - Check your spelling. Function names must match. The function is called setValues , not setValue .

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=sqrt(real*real+img*img);
                return magn;
        }
}
public class TestComplex {
         
  public static void main (String[] argv)
  {
    Complex c1 = new Complex();
    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());
  }
}

Now this is the error I am getting:

[51] $ javac TestComplex.java
TestComplex.java:20: cannot find symbol
symbol : method sqrt(double)
location: class Complex
double magn=sqrt(real*real+img*img);
^
1 error

If you intend to use the sqrt function from the Math class, the compiler has no idea that that is what you intend unless you specify that. Otherwise, it looks for a sqrt function in the class that you have written, which doesn't exist. Hence an error.

double magn=Math.sqrt(real*real+img*img);

Add red part above.

Thank you so much. Problem solved!
Clearly I have to be wayyy more attentive from now on.

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.