# Complex Numbers Java Problem

Printable View

• 01-01-2012, 11:19 PM
hani.itani
Complex Numbers Java Problem
Hey all! i'm having a problem with writing a complex number class.. i am having errors related to the toRect method, toPolar method, and conjugate (specifically the division part)... I can't figure out how to get around these errors. Basically, the program should have a tester(shown below)... the user should either use polar or rectangular... that is why i have created the toPolar method, it converts the rectangular form to a polar form but i'm not sure this is how it's supposed to be done. Any suggestions on how to work my errors out?
Thanks! :D

THIS IS THE CLASS
Code:

``` public class ComplexNumber implements ComplexInterface   {                 private double real,imaginary; //CONSTRUCTOR***********************************************       public ComplexNumber (double Re, double Im)             {                   real = Re;                   imaginary = Im;             } //*************************************************************       public double getReal ()             {                   return real;             }       public double getImaginary ()             {                   return imaginary;               } //CONJUGATE COMPLEX NUMBERS************************************         public ComplexNumber conjugate()           {               return new ComplexNumber (real, imaginary * (-1));           } //ADD COMPLEX NUMBERS******************************************         public ComplexNumber add (ComplexNumber cn2)           {             double real1 = real + cn2.getReal();             double imaginary1 = imaginary + cn2.getImaginary();           return new ComplexNumber (real1, imaginary1);             } //SUBTRACT COMPLEX NUMBERS*************************************               public ComplexNumber subtract (ComplexNumber cn2)           {             double real2 = real - cn2.getReal();             double imaginary2 = imaginary - cn2.getImaginary();             return new ComplexNumber (real2, imaginary2);           } //MULTIPLY COMPLEX NUMBERS*************************************                     public ComplexNumber multiply (ComplexNumber cn2)             {               double mul1 = real * cn2.getReal() - imaginary * cn2.getImaginary();               double mul2 = real * cn2.getImaginary() + imaginary * cn2.getReal();             return new ComplexNumber (mul1, mul2);             } //DIVIDE COMPLEX NUMBERS**************************************           public ComplexNumber divide (ComplexNumber cn2)             {               double con = cn2.getconjugate();               double phase1 = real * con.getReal() - imaginary * con.getImaginary();               double phase2 = real * con.getImaginary() + imaginary * con.getReal();               double phase3 = cn2.getReal() * con.getReal() - cn2.getImginary() * con.getImaginary();               double phase4 = phase1 / phase3;               double phase5 = phase2 / phase3;             return new ComplexNumber (phase4, phase5);             }            //CONVERT FROM POLAR TO RECTANGUAR****************************             public static ComplexNumber toRect (double mag, double angle)             {               double realv = mag * (Math.cos(angle));               double imaginaryv = mag * (Math.sin(angle));             return new ComplexNumber (realv, imaginaryv);               } //CHECK IF COMPLEX NUMBERS ARE EQUAL**************************             public boolean isLike (ComplexNumber cn2)             {               return ( real == cn2.getReal() && imaginary == cn2.getImaginary() );             } //Convert the numbers to polar*******************************             public ComplexNumber toPolar (double real, double imaginary)               {                 double magnitude = Math.sqrt(Math.pow(real, 2) + Math.pow(imaginary, 2));                 double angles = Math.atan2(real, imaginary);                 return new ComplexNumber (magnitude, angles);               } //Returns this complex number as a string*********************             public String toString () {                               String result;                       if (real == 0) {             if (imaginary == 0) {             return "0";             }             else{               if (imaginary == 0) {         return String.valueOf(real);             } else                 if (imaginary < 0) {         return(real + " " + imaginary + "i");             } else             {         return(real + " +" + imaginary + "i");     }   }  }```
THIS IS THE TESTER
Code:

```import java.util.Scanner; public class ComplexTester {     public static void main (String[] args) {                double num1, num2, num3, num4;         String choose;               Scanner scan = new Scanner(System.in);       System.out.println ("Would you like to have polar or rectangular resuts?(polar/rectangular): ");         choose = scan.nextLine();           System.out.println ("Enter the first real number: ");           num1 = scan.nextDouble();           System.out.println ("Enter the first imaginary number: ");           num2 = scan.nextDouble();           System.out.println ("Enter the second real number: ");           num3 = scan.nextDouble();           System.out.println ("Enter the second imaginary number: ");           num4 = scan.nextDouble();                 ComplexNumber one = new ComplexNumber (num1, num2);         ComplexNumber two = new ComplexNumber (num3, num4);         ComplexNumber three, four, five, six, ad, sub, mult, div;         System.out.println ("The first complex equation is:" +one);         System.out.println ("The second complex equation is:" +two);           if (one.isLike(two))           System.out.println ("one and two are equal");         else           System.out.println ("one and two are not equal");                   three = one.add(two);             four = one.subtract(two);             five = one.multiply(two);             six = one.divide(two);             ad = toPolar(three);             sub = toPolar(four);             mult = toPolar(five);             div = toPolar(six); if (choose.equals("rectangular"))               System.out.println ("complex equation one + complex equation two:" +three);         System.out.println ("complex equation one - complex equation two:" +four);         System.out.println ("complex equation one * complex equation two:" +five);         System.out.println ("complex equation one / complex equation two:" +six);             else             System.out.println ("complex equation one + complex equation two:" +ad);         System.out.println ("complex equation one - complex equation two:" +sub);         System.out.println ("complex equation one * complex equation two:" +mult);         System.out.println ("complex equation one / complex equation two:" +div);               } }```
• 01-01-2012, 11:57 PM
pbrockway2
Re: Complex Numbers Java Problem
Quote:

Any suggestions on how to work my errors out?
What errors? Does the code compile? If it doesn't and you can't understand the compiler message post the whole compiler output and say which line(s) of your code it is referring to.

Does the code compile but show unwanted or unintended behaviour at runtime (including runtime exceptions)? If so say exactly what the observed runtime behaviour is (output, exceptions). And also what behaviour you wanted or intended.

-----

It is a *very* good plan in these cases to work one small step at a time, with much compiling and testing. The idea is that you should be dealing with one error at a time.
• 01-02-2012, 01:25 PM
hani.itani
Re: Complex Numbers Java Problem
@pbrockway2

well the errors start with the division Complex Number part:
Code:

```public ComplexNumber divide (ComplexNumber cn2)             {               double con = cn2.getconjugate();               double phase1 = real * con.getReal() - imaginary * con.getImaginary();               double phase2 = real * con.getImaginary() + imaginary * con.getReal();               double phase3 = cn2.getReal() * con.getReal() - cn2.getImginary() * con.getImaginary();               double phase4 = phase1 / phase3;               double phase5 = phase2 / phase3;             return new ComplexNumber (phase4, phase5);             }```
I need to divide the complex numbers using the conjugate method but it doesn't seem to work. The other error consists of the toPolar method:

Code:

```public ComplexNumber toPolar (double real, double imaginary)               {                 double magnitude = Math.sqrt(Math.pow(real, 2) + Math.pow(imaginary, 2));                 double angles = Math.atan2(real, imaginary);                 return new ComplexNumber (magnitude, angles);               }```
how can i fix that?