Complex class won't compile


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Complex class won't compile

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Chicago
    Posts
    24

    Help!

    Can someone help me out my complex class won't complie?
    This thing has been buging me for days I can't get it to work
    Who made up imaginary numbers anyway

    Code:
    public class Complex
    {
      //Data
      double real, imaginary;
    
      //Constructors
      Complex(double x)
      {
      real = x;
      imaginary = 0;
      return;
      }
    
      Complex(double x, double y)
      {
      real = x;
      imaginary = y;
      return;
      }
    
      //Methods or Operatios
      public Complex plus(Complex y)
      {
      double num = real + y.real;
      return new Complex(num, y.imaginary);
      }
    
      public Complex minus(Complex y)
      {
      double num1 = real - y.real;
      double num2 = imaginary - y.imaginary;
      return new Complex(num1, num2);
      }
    
      public Complex times(Complex y)
      {
      double num2;
      double num1 = real * y.real;
      if (imaginary != 0)
      {
              if (y.imaginary != 0)
            {
              num2 = (imaginary * y.imaginary)*(-1);
              num1 = num1 + num2;
              return new Complex (num1);
            }
      } 
            return new Complex(num1, 0);
      }
     
     public Complex abs() 
     {
       
       double num1 = real;
       double num2 = imaginary;
       double num3;
    
                   if (num1 == 0 && num2 == 0) {
                           num3 = 0;
                    return new Complex(num3);
                   } else if (num1>num2) {
                           double temp = num2/num1;
                           num3 = num1*Math.sqrt(1 + 
    
    temp*temp);
                   return new Complex(num3);
                   } else {
                           double temp = num1/num2;
                           num3 = num2*Math.sqrt(1 + 
    
    temp*temp);
                   return new Complex(num3);
                   }
     }
    
     public Complex conjugate()
     {
       double num1 = real;
       double num2 = imaginary;
    
            return new Complex(num1, -num2);
     }
    
    public Complex reciprocal() 
    {
            Complex reciprocal = this.conjugate();
            double sqAbs = this.abs();
            reciprocal.setReal(reciprocal.getReal()/sqAbs);
    	reciprocal.setImaginary(reciprocal.getImaginary()/sqAbs);
            return reciprocal;
        }
    
      public boolean equals (Complex cvalue) {
        return ( (real == y.real) &&
                 (imaginary  == y.imaginary) ) ;
      }
    
     public String toString()
      {
      if (imaginary == 0)
        return Double.toString(real);
      if (imaginary<0)
        return 
    
    Double.toString(real)+Double.toString(imaginary)+"*i";
      return Double.toString(real)+ "+" 
    
    +Double.toString(imaginary)+"*i";
      }
    }
    Last edited by Mcody2; 11-15-2005 at 12:59 AM.

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    Can you post the error you get when you try to compile?
    ~evlich

  3. #3
    Join Date
    Oct 2005
    Location
    Chicago
    Posts
    24
    I think it has something to do with the equals and the reciprocal class

  4. #4
    Join Date
    Aug 2003
    Posts
    313
    Can you post the exact output from the compiler?
    ~evlich

  5. #5
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    It looks to me like you tried to modify the code I posted on the other thread and created some inconsistencies in the process. In your code, you are returning a Complex object from the method abs() and in another place it is expecting a double
    Code:
     double sqAbs = this.abs();
    Anyway, I fail to understand why you should return another Complex object instead of a double. The formula is as simple as sqrt(real^2 + imaginary^2) and it is always real. Of course, if you want to represent everything as Complex, you should take care of this point.

    Your member variables "real" and "imaginary" do not have a getReal() and getImaginary() method. It seems like you didn't include it at all. Similarly for the set methods. You need to have them to provide the get methods atleast. Alternatively, you could make the variables public and refer to them directly. But it would be quite a violation of OO principles.
    Happiness is good health and a bad memory.

  6. #6
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    Here's my sample code again, with some additions. See if this helps.
    Code:
    /* Note: Class not defined public to stuff all the code in one source file. */
    
    /* Complex class */
    class Complex {
        private double a;   // Real part of the complex number
        private double b;   // Imaginary part of the complex number
    
        /* Constructors */
        public Complex() {
            // Default no-arg constructor.
            // Real and imaginary parts are 0.0d by default.
        }
    
        public Complex(double real, double imaginary) {
            // Set the real and imaginary parts
            this.a = real;
            this.b = imaginary;
        }
    
        public Complex(Complex complex) {
            // Create a copy of the Complex object passed as argument.
            this.a = complex.getReal();
            this.b = complex.getImaginary();
        }
    
    
        /* Overloaded toString() method from java.lang.Object 
           to print the Complex number representation. */
        public String toString() {
            return this.a +(this.b>0?"+":"") +this.b +"j";
        }
    
    
        /* Overloaded equals() method from java.lang.Object 
           to test the equality of two Complex objects */
        public boolean equals(Complex complex) {
            return (this.a == complex.getReal()) && (this.b == complex.getImaginary());
        }
    
    
        /* Getter methods */
        public double getReal() {  return this.a;  }
        public double getImaginary() {  return this.b;  }
    
    
        /* Setter methods */
        public void setReal(double real) {
            this.a = real;
        }
        public void setImaginary(double imaginary) {
            this.b = imaginary;
        }
    
    
        /* Returns the absolute value */
        public double abs() {
            // This method will work with JDK 1.5
            return Math.hypot(this.a, this.b);
            /*
                For older versions of JDK, use the following line instead:
                Math.sqrt(Math.pow(this.a, 2) +Math.pow(this.b, 2));
            */
        }
    
        /* Square of absolute value */
        public double sqAbs() {
            return Math.pow(this.a, 2) +Math.pow(this.b, 2);
        }
    
    
        /* Complex Conjugate of the complex number */
        public Complex conjugate() {
            // Returns a-b*j
            return new Complex(this.a, -this.b);
        }
    
    
        /* Reciprocal of the Complex number */
        public Complex reciprocal() {
            // Returns (a-b*j)/(a^2 +b^2)
            Complex reciprocal = this.conjugate();
            double sqAbs = this.sqAbs();
            reciprocal.setReal(reciprocal.getReal()/sqAbs);
            reciprocal.setImaginary(reciprocal.getImaginary()/sqAbs);
            return reciprocal;
        }
    
    
        // Additional methods.
    
        /* Adding another Complex number to the present Complex number
           Note: The method returns void since the state of the original object 
                 will change, for which the method was called. */
        public void add(Complex complex) {
            this.a += complex.getReal();
            this.b += complex.getImaginary();
        }
    
    
        /* Subtract */
        public void subtract(Complex complex) {
            this.a -= complex.getReal();
            this.b -= complex.getImaginary();
        }
    
    
        /* Multiply a Complex number to the present number */
        public void multiply(Complex complex) {
            double real = complex.getReal();
            double imag = complex.getImaginary();
            this.a = a*real - b*imag;
            this.b = b*real + a*imag;
        }
    
    
        /* Divide */
        public void divide(Complex complex) {
            // Division is the same as multiplying with the reciprocal.
            this.multiply(complex.reciprocal());
        }
    
    
        /* TODO: Add other methods for complex number operations here. */
    
    }
    
    
    /* Class to test the Complex number operations. */
    public class TestComplex {
    
        public static void main(String[] argv) {
            Complex complex = new Complex(1, 2);
            System.out.println("Complex number               = " +complex);
            System.out.println("Absolute value               = " +complex.abs());
            System.out.println("Reciprocal                   = " +complex.reciprocal());
            complex.add(new Complex(2, 5));
            System.out.println("Adding a complex number      = " +complex);
            complex.subtract(new Complex(0, 3));
            System.out.println("Subtracting a complex number = " +complex);
            complex.multiply(new Complex(7, 2));
            System.out.println("Multiplying a complex number = " +complex);
            complex.divide(new Complex(4, 9));
            System.out.println("Dividing a complex number    = " +complex);
    
            System.out.println("Testing equality...");
            System.out.println(new Complex(1, 2).equals(new Complex(1, 2)));
            System.out.println(new Complex(1, 2).equals(new Complex(2, 1)));
        }
    
    }
    Happiness is good health and a bad memory.

  7. #7
    Join Date
    Nov 2005
    Posts
    10
    and mine..
    for ready to java
    import hsa.*;

    public class ComplexTest
    {
    public static void main (String[] args)
    {
    double localN = 0;
    Console c = new Console ();
    Complex x = new Complex (5, -5);
    // x.get(c);
    c.println (x.toString ());
    Complex y = new Complex (10, 0);
    Complex adds = new Complex ();
    Complex subtracts = new Complex ();
    Complex multiplys = new Complex ();
    Complex divides = new Complex ();
    Complex power = new Complex ();
    adds = x.add (y);
    subtracts = x.subtract (y);
    multiplys = x.multiply (y);
    divides = x.divide (y);
    c.println ("addition");
    c.println (adds.toString ());
    c.println ("subtraction");
    c.println (subtracts.toString ());
    c.println ("multiplication");
    c.println (multiplys.toString ());
    c.println ("division");
    c.println (divides.toString ());
    c.println ("sqrt (a^2 +b^2) is:" + x.abs ());
    c.print ("enter a value of n to calculate power");
    localN = c.readDouble ();
    power = (x.pow (localN));
    c.println ("the power is " + power.toString ());
    c.println ("in relation to second complex, the first is");
    c.println ("greater?" + x.greaterThan (y));
    c.println ("less?" + x.lessThan (y));
    c.println ("equal to?" + x.equalTo (y));

    }
    }
    /*
    class Complex
    This class stores a complex number and allows user to get, add, subtract, multiply, divide, find absolute value,
    return value of power

    Fields:
    a - simple part of complex number, a double
    b - imaginary part of complex number, a double
    Methods:
    constructor
    get -gets field values of the complex number from the user
    toString -prints the complex number in correct form with regards to +/- zeroes and ones
    (eg) if a=5 and b=-1, it prints:5 - i
    add -adds Complex with a given Complex
    subtract -subtracts Complex with a given Complex
    multiply -multiplies Complex with a given Complex
    divide -divides Complex with a given Complex
    abs -returns value of square root of (a^2+b^2)
    pow -returns value of (a+bi)^n, given n where (a+bi)^n=r^ncos(nt)+r^n*sin(nt)i
    -where r=abs() t=atan(b/a)
    */
    class Complex
    {
    protected double a;
    protected double b;

    /**********************************************************************************
    Constructor for class Complex
    Parameters: two parts of complex number
    Return Value:none
    **************************************************************************************/
    public Complex (double aLocal, double bLocal)
    {
    a = aLocal;
    b = bLocal;
    }


    /**********************************************************************************
    Overloaded constructor for class Complex
    Parameters: none
    Return Value:none
    **************************************************************************************/

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


    /**********************************************************************************
    get
    Inputsnewvaluesinto the complex(values from user)
    Parameters: The console object to get input from
    Returns Value: none(void)
    **************************************************************************************/
    public void get (Console c)
    {
    c.print ("enter a value for a");
    a = c.readDouble ();
    c.print ("enter a value for b");
    b = c.readDouble ();
    }


    /****************************************************************************************** ***************
    toString
    Createa a string representation of the contents of the object
    prints the complex number in correct form with regards to +/- zeroes and ones and correct spacing
    (eg) if a=5 and b=-1, it prints:5 - i
    ****************************************************************************************** **************/
    public String toString ()
    {
    String s = "";
    boolean noA = false;
    if (a == 0 && b == 0)
    s = "0";
    else if (a == 0)
    {
    s = s;
    noA = true;
    }
    else
    s = s + a;
    if (b == 0)
    s = s;
    else if (b == 1)
    {
    if (!(noA))
    s = s + " + i";
    else
    s = s + "i";
    }
    else if (b == -1)
    if (!(noA))
    s = s + " " + "- i";
    else
    s = s + "" + "-i";
    else if (b < 0)
    if (!(noA))
    s = s + " " + "- " + - b + "i";
    else
    s = s + "" + b + "i";
    else if (b > 0)
    {
    if (!(noA))
    s = s + " + " + b + "i";
    else
    s = s + "" + b + "i";
    }
    return s;
    }
    /**********************************************************************************
    Method add
    adds the Complex to a given complex number
    Parameters: none
    Returns Value: new a and b values, as result of addition, a new complex
    ***********************************************************************************/
    public Complex add (Complex s)
    {
    Complex result = new Complex ();
    result.a = (a + s.a);
    result.b = (b + s.b);
    return result;
    }


    /**********************************************************************************
    Method subtract
    subtracts the Complex by a given complex number
    Parameters: none
    Returns Value: new a and b values, as result of subtraction, a new complex
    ************************************************************************************/
    public Complex subtract (Complex s)
    {
    Complex result = new Complex ();
    result.a = (a - s.a);
    result.b = (b - s.b);
    return result;
    }


    /* Method multiply
    multiplies the Complex to a given complex number
    Parameters: none
    Returns Value: new a and b values, as result of multiplication, a new complex
    */
    public Complex multiply (Complex s)
    {
    Complex result = new Complex ();
    result.a = (a * s.a) - (b * s.b);
    result.b = (a * s.b) + (s.a * b);
    return result;
    }


    /* Method divide
    divides the Complex by a given complex number
    Parameters: none
    Returns Value: new a and b values, parts of a new complex
    */

    public Complex divide (Complex s)
    {
    Complex result = new Complex ();
    result.a = ((a * (s.a)) + ((s.b) * b)) / (Math.pow (s.a, 2) + (Math.pow (s.b, 2)));
    result.b = ((b * s.a) - (a * s.b)) / (Math.pow (s.a, 2) + (Math.pow (s.b, 2)));
    return result;
    }


    /* Method abs
    returns the value of square root of (a^2 + b^2) for the current complex
    Parameters:none
    ReturnsValue: square root of (a^2 + b^2)
    */

    public double abs ()
    {
    return Math.sqrt (Math.pow (a, 2) + Math.pow (b, 2));
    }


    /* Method pow
    returns the value of (a+bi)^n for the current complex
    (r^n)*cos(nt)+r^n(sin(nt))i
    Parameters: none
    Returns Value: value of (a+bi)^n, a double
    */
    public Complex pow (double n)
    {
    double r = abs ();
    double t = Math.atan (b / a);
    Complex result = new Complex ();
    result.a = Math.pow (r, n) * (Math.cos (n * t));
    result.b = Math.pow (r, n) * (Math.sin (n * t));
    return result;
    }
    /* method lessThan
    compares the current complex to another to determine if the current complex is less than another,
    returns true or false, true if the complex is less than another,
    false, if the complex is not less than a given complex
    Parameters: Complex y, a given complex
    Returns Value:boolean */

    public boolean lessThan (Complex y)
    {
    if (abs () < y.abs ())
    return true;
    else
    return false;
    }

    /* method greaterThan
    compares the current complex to another to determine if the current complex is greater than another,
    returns true or false,true if the current complex is greater than than a given complex
    false,if the complex is not greater than a given complex
    Parameters:Complex y,agiven complex
    ReturnsValue:boolean */

    public boolean greaterThan (Complex y)
    {
    if (abs () > y.abs ())
    return true;
    else
    return false;
    }


    /*
    method equalTo
    compares the current complex to another to determine if the current complex is equal to another,
    returns true or false,true if the current complex is equal to a given complex
    false,if the complex is not equal to a given complex
    Parameters:Complex y,a given complex
    ReturnsValue:boolean
    */

    public boolean equalTo (Complex y)
    {
    if (abs () == y.abs ())
    return true;
    else
    return false;
    }
    }

  8. #8
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    Would you please edit your post and use the code tags for formatting the code?
    Happiness is good health and a bad memory.

Similar Threads

  1. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  2. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  3. Replies: 1
    Last Post: 04-03-2002, 06:37 PM
  4. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 07:53 PM
  5. WinForm Compile Error
    By Robert in forum .NET
    Replies: 1
    Last Post: 10-24-2000, 11:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center