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

Threaded 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 01:59 AM.

Similar Threads

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

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