Java Problem Complex Numbers

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 4385 Views - Last Post: 07 January 2012 - 05:29 AM Rate Topic: -----

#1 hani.itani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-January 12

Java Problem Complex Numbers

Posted 01 January 2012 - 06:55 AM

Hey all! I'm having a problem with this program (Complex Numbers)... Lots of errors are appearing but i don't understand why! I am positive about everything but i don't get why nothing is working. The problems i'm facing are with the division, toRect, and toPolar methods...I'll be very grateful if anyone helps me out! Thank you :)
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 c = 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*******************************


             private 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 post has been edited by smohd: 01 January 2012 - 10:10 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: Java Problem Complex Numbers

#2 mi14chal  Icon User is offline

  • D.I.C Head

Reputation: 81
  • View blog
  • Posts: 202
  • Joined: 11-December 10

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 07:02 AM

What errors are you getting?
Was This Post Helpful? 0
  • +
  • -

#3 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 07:07 AM

Please use the code tags provided when you post code.


Most of your problems are with closing brackets, just add the missing ones. And what is 'con' in the method

	public ComplexNumber divide(ComplexNumber cn2)
	{
		double c = 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);
	}



maybe you meant ComplexNumber con = cn2.getconjugate();?

This post has been edited by Mylo: 01 January 2012 - 07:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 hani.itani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-January 12

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 07:28 AM

Yeah i forgot to put "con"! thanks for that!
my errors are 15 so far.... one error is about the toRect method... it's saying the "modifier static not allowed" i dont get why that's not working.
another error is about the toPolar method...
also, double con = cn2.getconjugate(); ==> doesn't seem to work... i'm getting a "cannot find symbol" error!

i'm testing the program using this Tester: (there's an error with the if and else statement... they're saying that the else statement doesn't have an if statement!

Thanks a lot for your help! :)

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);
       }  
   
    }

}


i didn't put tag around my code sorry! how do you do that?

This post has been edited by smohd: 01 January 2012 - 10:11 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Was This Post Helpful? 0
  • +
  • -

#5 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 08:53 AM

Quote

i didn't put tag around my code sorry! how do you do that?


In the posting toolbar there will be something that says 'CODE', place your code in between the two of those =)

Quote

one error is about the toRect method... it's saying the "modifier static not allowed"


It works fine for me and should work fine, perhaps you missed a brace somewhere and is causing an incorrect error (well it is correct technically).

Quote

another error is about the toPolar method...


your isLike method is missing a closing brace, perhaps you confused the compiler. You didnt close your class body either.

Quote

cn2.getconjugate();


Because the method doesnt exist. The method signature is:

public ComplexNumber conjugate()

--

As for your tester class, you call toPolar(). Which is not defined in ComplexTester, but ComplexNumber. So you need

ComplexNumberObject.toPolar(...,...); Also, it does not take a ComplexNumber argument. It also must not be private.

Fix that up for now =)

This post has been edited by Mylo: 01 January 2012 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

#6 hani.itani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-January 12

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 09:33 AM

Mylo

for the toPolar.... should i use ComplexNumberObject.toPolar(...,...) this way:


ad = ComplexNumberObject.toPolar(three);
is this what you mean?

I'm still having trouble with the double con = cn2.getconjugate();(it still has an error) I can't figure out how to sort that problem..any suggestions?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#7 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 09:43 AM

Well just to be sure you are understanding, by ComplexNumberObject, I mean an instance of ComplexNumber, not literally ComplexNumberObject.

ComplexNumberObject.toPolar() is correct. However the paramter for toPolar is double real, double imaginary. You are passing in a ComplexNumber object.

so toPolar(ComplexNumberObject) is incorrect
but toPolar(1,2) is correct as the method takes to doubles.

Also, given how you have set up the toPolar method, did you intend for it to be static?



As for the getconjugate(). It is not defined in ComplexNumber.

You called the method getconjugate, but the method is called 'conjugate' not 'getconjugate'

	public ComplexNumber conjugate()
	{
		return new ComplexNumber(real, imaginary * (-1));
	}


This post has been edited by Mylo: 01 January 2012 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#8 hani.itani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-January 12

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 11:03 AM

Mylo


Okay, so i did what you said concerning the
double con = cn2.getconjugate();
i changed it to
double con = cn2.conjugate(); 
but it doesn't work too. The error shows: "incompatible types". What do i have to change?
As for the toPolar i still haven't figured that out. I'll give it another try, it's a bit confusing.

Basically, what i have to do is ask the user(using the ComplexTester) if he/she wants to add, subtract, multiply, and divide the complex equations in rectangular or polar form. Should i create a method for adding, subtracting,etc. polar complex numbers or should i just change the rectangular values to polar? i created the toPolar method to change the rectangular values to polar.. do you think that's a good idea? I hope you understand what i'm saying :/

Thank you for your help :smile2:
Was This Post Helpful? 0
  • +
  • -

#9 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 01:12 PM

Quote

public double getReal ()
I laughed out loud.

That aside... Don't you think it would be better if a method like conjugate() altered the data of the object it's called on, instead of returning a new number. Look, the reason this doesn't work:
double con = cn2.conjugate();
is because conjugate() returns a ComplexNumber, not a double. What if you were just able to say:
cn2.conjugate();
and then cn2 is changed itself, instead of having to create a new complex number for every change? I think it would be a more logical design.

This post has been edited by Sheph: 01 January 2012 - 01:13 PM

Was This Post Helpful? 0
  • +
  • -

#10 hani.itani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-January 12

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 01:28 PM

Sheph

Suppose i put
cn2.conjugate();
then i'll have another problem because if you look carefully:
 cn2.getconjugate();
              
              double phase3 = [b]cn2.getReal()[/b] * con.getReal() - [b]cn2.getImginary()[/b] * con.getImaginary();
              


I am already using
cn2.getReal()
etc.. so that will change the value for cn2 while all i wanted is to have the same value

get it? oh and what was so funny? :P this is the the first time i write a class!
Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 02:28 PM

What they are saying is: your conjugate() method does not return double, instead it returns new ComplexNumber so you have to assign the returned value to a ComplexNumber not double.

Also this snippet has simple example of Complex Class
Was This Post Helpful? 0
  • +
  • -

#12 hani.itani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-January 12

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 02:34 PM

smohd


how do you suppose i do that?
Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 02:41 PM

Please post your updated code and any remaining errors you are getting, then we will see what changes have you made to your program.

Look at the divide() method in the snippet, also I dont see anywhere in your method you use that c variable you created. May be you dont need that variable any more in your method.
Was This Post Helpful? 0
  • +
  • -

#14 hani.itani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-January 12

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 02:54 PM

smohd

this is the updated one(below)... i had a typing mistake i fixed it... but i'm still having the same problem. The conjugate part in the division method is messed up or it might be the conjugate method itself.



  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");
     }
   }
 }

Was This Post Helpful? 0
  • +
  • -

#15 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Java Problem Complex Numbers

Posted 01 January 2012 - 03:01 PM

Also you may need to look again at your toString() method:
if (real == 0) {
            if (imaginary == 0) {
            return "0";
             }
            else{
              if (imaginary == 0) { //this condition will never be true because the else part is executed only if imaginary != 0.
        return String.valueOf(real);
            } else 

And what if real != 0? you have n condition for that, do you?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2