Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Complex Number Calculations

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question Complex Number Calculations

    Problem: A complex number is defined as z=a+i*b, where a is the real part, and b is the imaginary part. In other words, in order to define a complex number, we need the two floating numbers a and b.

    Write methods that perform for each of the following operations with complex numbers z1 = a1 + i*b1, and z2 = a2 + i*b2:

    Addition: z1 + z2=(a1+a2) + i*(b1+b2)
    Subtraction: z1 - z2=(a1-a2) + i*(b1-b2)
    Multiplication: z1*z2 = (a1*a2 – b1*b2) + i*(a1*b2 + b1*a2)
    Division: z1/z2 = (a1*a2 +b1*b2)/(a2^2 + b2^2) + i*(b1*a2 – a1*b2)/(a2^2 + b2^2)
    Create a test program that asks for the real and imaginary parts of two complex numbers from the user, and displays the results of the four operations, writing the formula as shown above, and replacing the a1, a2, b1 and b2 with the numbers entered by the user.

    ****
    The professor used the incorrect complex number equations and has notified the students of his error. I have run into a few problems thus far.

    1. I'm not sure how to use floating numbers with the Math.pow(double, double) function, since its requires doubles!? So instead of using floating numbers, I've knowingly switched them all to double in order to see if the code itself is working properly for the purposes of this forum. Is there a way that I can modify this function so that it will work for floating numbers?

    2. Regarding the division method, an error stating that c and d have not been initialized and I'm not sure how to change it because the other calculation methods work fine. Why do I need to initialize c and d in the division method and not the others?

    3. Am I on the right path? I have surfed the web to see how others completed the program and they all appear very different than mine...

    4. As always feel free to offer an constructive criticism on how I can improve on the code. It's always important to me that I create an easy to read and streamlined program. I'm just beginning my journey into programming, so please be critical! Thank you.
    ****


    package program5;
     
    import java.util.Scanner;
     
    public class Program5 {
     
        static double a, b, c, d;
        static double i = Math.pow(-1,1/2);
     
        public static void main(String[] args) {
     
            Scanner input = new Scanner(System.in);
            System.out.println(" + Calculating Complex Numbers + ");
            System.out.printf("%15s\n","z = a + (i*b) and z = c + (i*d)");
            System.out.print("Enter a: ");
            a = input.nextFloat();       
            System.out.print("Enter b: ");
            b = input.nextFloat();
            System.out.print("Enter c: ");
            c = input.nextFloat();
            System.out.print("Enter d: ");
            d = input.nextFloat();
     
            addition("Addition: ");
            subtraction("Subtraction: ");
            multiplication("Multiplication: ");
            division("Division: ");
        }
        public static void addition(String name){
            double add = (a + c) + i*(b + d);
            System.out.println(name + add);
        }
        public static void subtraction(String name){
            double subtract = (a - c) + i*(b - d);
            System.out.println(name + subtract);
        }
        public static void multiplication(String name){
            double multiply = ((a * c) - (b * d)) + i*((b * c) + (a * d));
            System.out.println(name + multiply);
        }
        public static void division(String name){
            double c = Math.pow(c,2);
            double d = Math.pow(d,2);
     
            double division = (((a * c) + (b * d)) / (c + d)) + i*((b * c)- 
                              (a * d)) / ((c + d));
            System.out.println(name + division);
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Complex Number Calculations

    Why do I need to initialize c and d in the division method and not the others?
    Look at the differences between how the variables are defined and used in the other methods.
    division() defines c and tries to use it before it has been assigned a value.

    Something like:
    int x = x + 1;
    What is the value of x there?

    Define it, assign it a value and then use it:
    int x = 1;
    x = x + 1;

    NOTE: Having local variables with the same name as class variables is not a good idea. The local variables hide the class variables and make for confusion about what the code is doing. Change the local variables' names so they are different from the class variables.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question Re: Complex Number Calculations

    thank you for the feedback Norm. I used a different formula for the division that didn't involve Math.pow(). I hope that I am doing the right thing here.

     public class Program5 {
     
        static double realA, imagineB, realC, imagineD;
        static double i = Math.pow(-1,1/2);
     
        public static void main(String[] args) {
     
            Scanner input = new Scanner(System.in);
            System.out.println(" + Calculating Complex Numbers + ");
            System.out.printf("%15s\n","z = a + (i*b) and z = c + (i*d)");
            System.out.print("Enter real a: ");
            realA = input.nextFloat();       
            System.out.print("Enter imaginary b: ");
            imagineB = input.nextFloat();
            System.out.print("Enter real c: ");
            realC = input.nextFloat();
            System.out.print("Enter imaginary d: ");
            imagineD = input.nextFloat();
            System.out.println();
     
            addition("Addition: ");
            subtraction("Subtraction: ");
            multiplication("Multiplication: ");
            division("Division: ");
        }
        public static void addition(String name){
            double add = (realA + realC) + i*(imagineB + imagineD);
            System.out.println(name + add);
        }
        public static void subtraction(String name){
            double subtract = (realA - realC) + i*(imagineB - imagineD);
            System.out.println(name + subtract);
        }
        public static void multiplication(String name){
            double multiply = ((realA * realC) - (imagineB * imagineD)) + i*((imagineB * realC)
                    + (realA * imagineD));
            System.out.println(name + multiply);
        }
        public static void division(String name){
          //  double imaginaryOne = Math.pow( imagineB , 2);
          //  double imaginaryTwo = Math.pow(imagineD , 2);
     
                double division = ((realA + (imagineB * i)) / (realC + (imagineD * i)));
                System.out.println(name + division);
        }

    I still don't think I'm doing something right though by using -1^(1/2) as the value of i. Is this what the professor is asking for?

    "Create a test program that asks for the real and imaginary parts of two complex numbers from the user, and displays the results of the four operations, writing the formula as shown above, and replacing the a1, a2, b1 and b2 with the numbers entered by the user."

    This is what our professor had to say for the assignment...I just want to make sure I'm comprehending this correctly. I'm concerned bc my code looks very different than others I have looked at online dealing with complex numbers.

    PleaseHELP!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Complex Number Calculations

    I don't know much about complex numbers, so I'll leave comments on that to someone that does.

    Comments on the code:
    Passing the name of the method to a method seems silly. When would you ever use another name for the method: subtraction("Subtraction: ");
    Normally the parameters passed to a method are something that can change from one call to another.

    This assignment should use a class for complex numbers instead of 4 variables. Then a call to a method would pass two instances of the class and return an instance of the class.

    The names of methods are normally verbs like doAddition() that say what the method will do.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    javaStooge (January 25th, 2014)

  6. #5
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Complex Number Calculations

    You are right, it is redundant to state the obvious with a method that is meant to perform only one function in the program. I think I was just playing around and glad
    that it worked when I tried it! ha.

    I know what you mean, when there should be a class for it, but there is nothing remotely similar to this concept in our text and its thrown through a loop.

    Nevertheless, I've looked through our textbook and surfed the web for information on complex numbers, but my search has not provided a lot. If anybody has a good link on this concept let me know. I've seen a few different programs dealing with " Complex() " but no topic discussing the purpose and use of the function.

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Complex Number Calculations

    I'm not sure what your remaining questions are, but just looking at your latest code I see that you're not quite getting it. The result of the complex number mathematical operations should result in a real and complex component. Your current code prints the result as a single number, but all results should be in the form:

    a + b*i

    where the values a and b are calculated from the equations given in your first post, though I'm not sure what you meant by mentioning that the professor gave the wrong equation. Was that straightened out? You can refer to this page if you're not sure and for examples of what the results of the operations should look like.

    Hope this helps, but ask if you still have questions.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    javaStooge (January 26th, 2014)

  9. #7
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Complex Number Calculations

    So, I should just use a character variable i instead of assigning i a value of sqrt(-1)? Given that, the code still seems much easier than what I've seen elsewhere on the web. I think I am missing something that deals with Complex()...does that sound correct? I guess I'm justing having a hard time figuring out the purpose of this program---in other words, a basic calculation and that is way too easy.

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Complex Number Calculations

    Yes, but to be clear, let's take addition as an example:

    For z1 = a1 + i*b1, and z2 = a2 + i*b2 where

    a1 = 1
    b1 = 2
    a2 = 3
    a4 = 4

    so:

    z1 = 1 + 2i
    z2 = 3 + 4i

    then

    z1 + z2 = 4 + 6i

    That's how the answer should look, 4 + 6i, or 4 + 6*i, or whatever variation of that you prefer, but the answer should be an expression with a real and imaginary part.

    I can't comment on how your code compares to other code you've seen on the Internet. You haven't shown us what you're talking about, and I've no way to know whether the code you've seen is relevant to your assignment.

    As you said, your assignment is pretty simple: get 4 numbers from the user, program the equations correctly, and output the results. How you present or return those results from the methods you've written may be the complex (pun convenient) part of the assignment you haven't conquered yet. You can't return two values from the methods, so you'll have to wrap them in a single object, perhaps the Complex object you've seen elsewhere, an array, or simply output the result from each method. I don't see that it matters based on the assignment and may depend on what you're able to do.

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    javaStooge (January 26th, 2014)

  12. #9
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Complex Number Calculations

    Great! Thank you for the detailed response Mr. Brannon. That really helped to clear things up for me. I was at a cross roads with the output, but I think I can figure it out from here.

Similar Threads

  1. complex input parsing
    By Wolfone in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: September 3rd, 2013, 12:56 AM
  2. Complex numbers need help finishing
    By therealvasile in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 1st, 2013, 12:26 PM
  3. Math calculations
    By mikem1034 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 16th, 2013, 10:13 AM
  4. Complex Numbers in Java
    By JavaNovice03355 in forum Member Introductions
    Replies: 1
    Last Post: May 18th, 2011, 07:30 PM
  5. How do you design a complex program
    By mydarkpassenger in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2010, 06:52 PM

Tags for this Thread