0

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?

2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by Samyx
0

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 .

0
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

0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.