Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to print out the complex numbers in java? I tried every possible way I can think of doing it, but unable to do so.

+14 votes
asked Aug 7 by npatel300 (1,440 points)
edited Aug 8 by npatel300
public class Main

{

public static void main(String[] args)

{

    a = 5;

    b = 4;

    c = 2;

    d = 3;

        double n = 5.0;

        System.out.println"(a + b) + (c + d)" + (a + b) + " + " + (c + d) + " i ");

        System.out.println(a + b); " - " (c + d) + " = " + a.sub(b));

        System.out.println(a + b); " * " (c + d) + " = " + a.mul(b));

        System.out.println(a + b); " / " (c + d) + " = " + a.div(b));

        System.out.println(a + b); " + " + (5.0) + " = " + a.add(i));

        System.out.println(a + b); " - " + (5.0) + " = " + a.sub(i));

        System.out.println(a + b); " * " + (5.0) + " = " + a.mul(i));

        System.out.println(a + b); " / " + (5.0) + " = " + a.div(i));

}

}

/* Complex.java */

public class complex_number

{

    public complex_number()

    {

        this(0);

    }

public complex_number(int n)

{

    this(n, 1);

}

public complex_number(int n)

{

    this.n = n;

}

public complex_number add(complex_number o)

{

   return new complex_number(n + o.n);

}

public complex_number add(int n)

{

   return new complex_number(n + n);

}

public complex_number div(complex_number o)

{

    return new complex_number(n / o.n);

}

public complex_number mul(complex_number o)

{

   return new complex_number(n * o.n);

}

public complex_number mul(int n)

{

   return new complex_number(n * n);

}

public complex_number sub(complex_number o)

{

   return new complex_number(n - o.n);

}

public complex_number sub(int n)

{

   return new complex_number(n - n);

}

public String toString()

{

    return "(" + n + " / "  + ")";

}

private int n;

}

1 Answer

+1 vote
answered Aug 9 by Peter Minarik (32,240 points)
edited Aug 16 by Peter Minarik

Same problem as before with your other codes. You do not know what a complex number is.

"(a + b)" is just a regular real number. It's not a complex number.

Look at my last comment here: http://question.onlinegdb.com/10571/error-complex_number-private-within-context-code-what-wrong

Java does not have any support for complex numbers. You have to write your own class. You must have a real and an imaginary part. This means at least two variables. You only have one (n). So your code will never work until you have both of the components present in your class.

commented Aug 9 by npatel300 (1,440 points)
Okay, I now have real and imaginary part in my code. But not able to figure out the error. Please tell me where I am wrong. Thank you
https://onlinegdb.com/KYRC3Cz64
commented Aug 9 by Peter Minarik (32,240 points)
For some reason I cannot run the code (OnlineGDB) keep hanging for me.

However, just by the look of it:

In "main()", you calculate the value of "temp" over and over, then you keep printing it in various contexts, as if it would be the result of various calculations, but in fact, the value of "temp" is already set and it does not change between the print functions.

public complex_number(int real, double imag)
{
    this.real = real;
    this.imag = imag;
}

I think you cannot use the "this" pointer in the constructor. Call your arguments something else so you don't have to use the "this" pointer. E.g:

public complex_number(int real_, double imag_)
{
    real = real_;
    imag = imag_;
}


public static Complex_number mul(n1, n2);
{
    Complex_number temp = new Complex_number();

    temp.real = n1.real * n2.real;
    temp.imag = n1.imag * n2.imag;

    return(temp);
}

This is not how you do multiplication with complex numbers. Your division is also wrong.

Check online how to multiply (and divide) complex numbers. (Do you know matrices?)

I've been linking you the Wikipedia on Complex Numbers. Please, read it. Pay attention at the multiplication and division part: https://en.wikipedia.org/wiki/Complex_number#Multiplication_and_square


 if (imag <  0) return real + " - " + (-imag) + "i";

I believe there's no need for this. I think negative numbers will be handled just fine.
commented Aug 9 by npatel300 (1,440 points)
I just fixed what you suggested. But this is the error that I get : Main.java:53: error:  expected
public Complex_number add(n1, n2);
Besides on multiply and divide complex numbers, is everything else correct? And how would I print out complex numbers in java.


import java.util.Objects;

public class Main
{
public static void main(String[] args)
{
    complex_number n1 = new complex_number(2, 4.5),
                   n2 = new Complex_number(3, 5.0),
                   temp;

    temp = add(n1, n2);
    temp = sub(n1, n2);
    temp = mul(n1, n2);
    temp = div(n1, n2);

    System.out.printf("complex number = %1f + %.1fi", temp.real, temp.imag);
    System.out.printf("complex number = %.1f - %.1fi", temp.real, temp.imag);
    System.out.printf("complex number = %.1f * %.1fi", temp.real, temp.imag);
    System.out.printf("complex number = %.1f / %.1fi", temp.real, temp.imag);
    
    
    System.out.printf("complex number = %.1f + %.1fi", temp.real, temp.imag);
    System.out.printf("complex number = %.1f + %.1fi", temp.real, temp.imag);
    System.out.printf("complex number = %.1f + %.1fi", temp.real, temp.imag);
    System.out.printf("complex number = %.1f + %.1fi", temp.real, temp.imag);
}

   
}

/* complex_number.java */
public class complex_number
{
    int real;
    double imag;
    
public complex_number(int real_, double imag_)
{
    real = real_;
    imag = imag_;
}

public Complex_number add(n1, n2);
{
    Complex_number temp = new Complex_number();

    temp.real = n1.real + n2.real;
    temp.imag = n1.imag + n2.imag;

    return(temp);
}

public static Complex_number sub(n1, n2);
{
    Complex_number temp = new Complex_number();

    temp.real = n1.real - n2.real;
    temp.imag = n1.imag - n2.imag;

    return(temp);
}

public static Complex_number mul(n1, n2);
{
    Complex_number temp = new Complex_number();

    temp.real = n1.real * n2.real;
    temp.imag = n1.imag * n2.imag;

    return(temp);
}

public static Complex_number div(n1, n2);
{
    Complex_number temp = new Complex_number();

    temp.real = n1.real / n2.real;
    temp.imag = n1.imag / n2.imag;

    return(temp);
}

public String toString()
{
    if (imag == 0) return real + "";
    if (real == 0) return imag + "i";
    return real + " + " + imag + "i";
}

private int n;

}
commented Aug 9 by Peter Minarik (32,240 points)
edited Aug 9 by Peter Minarik
Complex_number add(n1, n2);

n1 and n2 have no types associated with them. In Java, C, C++, C# and other strongly typed languages you must explicitly specify the type of a variable (unlike in Python, where the type is implicitly applied).

I've created a Java "Complex Starter Pack" for you: https://onlinegdb.com/0_cvh0TSL

All you need to do is implement the
- subtract()
- multiply()
- divide()

functions. I've already done add() just to demonstrate how it could work.

Read https://en.wikipedia.org/wiki/Complex_number#Multiplication_and_square on how to do the multiplication and division. It's NOT just simply multiplying the real part with the real part and the imaginary with the imaginary part!
commented Aug 9 by npatel300 (1,440 points)
I worked on multiply and divide, but I am sure it is wrong. Do you mind me to help fix it? And in the main how to print out the other complex numbers, it just has the add.  Please help me out. Thank you.
https://onlinegdb.com/AWoPz_on_
commented Aug 10 by npatel300 (1,440 points)
Hi, this is the fixed code. But I do get few errors. Please tell me how should I fix?
https://onlinegdb.com/0ymiZVhJJ
commented Aug 10 by Peter Minarik (32,240 points)
edited Aug 10 by Peter Minarik
1. Indentation.

Everything in the class should be indented. The class has an opening and closing brace too, so indentation should apply to everything inside.

This is not an *error*, but it makes reading the code harder than it should be.

2. multiple returns

public static complex_number add(complex_number a, complex_number b)
{
    return new complex_number(a._real + b._real, a._imag + b._imag);
    return null;
}

The second return "return null;" will never be called. So it is confusing why it is there in the first place? Same applies to other functions with multiple returns.

3. subtraction

public static complex_number subtract(complex_number a, complex_number b)
{
    return new complex_number(b._real - a._real, b._imag - a._imag);
    return null;
}

If I would say "complex_number.subtract(a, b)", then I would expect to calculate a - b, but you're calculating b - a. Again, this is not an *error* per say, but it goes against natural logic. I'd suggest changing it to follow the standard and calculate a - b instead.

4. Multiplication

public complex_number multiply(complex_number other)
{
    _real = _real * other._real;
    _imag = _imag * other._imag;
    return this;
}

Let me quote from my previous comment: "Read https://en.wikipedia.org/wiki/Complex_number#Multiplication_and_square on how to do the multiplication and division. It's NOT just simply multiplying the real part with the real part and the imaginary with the imaginary part!"

I emphasized complex multiplication is NOT simply just multiplying real with real and imaginary with imaginary. Read the link how to calculate the multiplication.

a * b = (a.real + a.imag * i) * (b.real + b.imag * i) = a.real * b.real - a.imag * b.imag + (a.real * b.image + a.imag * b.real) * i

It's written right there on the site I've linked in (x + yi) * (u + vi) format. You just need to read what's written there.

5. Division:

Again, it's on the site I've linked in (x + yi) / (u + vi) format:

a / b = ((a.real * b.real + a.imag * b.imag) + (b.imag * a.real - b.real * a.imag) * i) / (a.real ^ 2 + a.imag ^ 2)

Now you separate what is real and what is imaginary (has an i multiplier) and you're good to go.
commented Aug 10 by Peter Minarik (32,240 points)
What I wrote before wasn't a proper code, more like a mathematical formula using the variables from the existing code.

The part without the "i" is the real component. Any part that contains the "i" is the imaginary component.

It's almost 1 a.m. here so I will just write this without testing as I really should be sleeping now:


public complex_number multiply(complex_number other)
{
        _real = _real * other._real - _imag * other._imag;
        _imag = _real * other.imag + _imag * other._real;
        return this;
}

public static complex_number multiply(complex_number a, complex_number b)
{
    return new complex_number(a._real * b._real - a._imag * b._imag, a._real * b._imag + a._imag * b._real);
}

public complex_number divide(complex_number other)
{
    double denominator = a._real * a._real + a._imag * a._imag;
    _real = (_real * other._real + _imag * other._imag) / denominator;
    _imag = (other._imag * _real - other._real * _imag) / denominator;
    return this;
}

public static complex_number divide(complex_number a, complex_number b)
{
    double denominator = a._real * a._real + a._imag * a._imag;
    return new complex_number((a._real * b._real + a._imag * b._imag) / denominator, (b._imag * a._real - b._real * a._imag) / denominator);
}

Note: you really have a long road ahead of you if you cannot take a mathematical formula and turn it into a C/Java/etc code. I am fairly sure you still do not understand how complex numbers work. It usually doesn't end well if you try to write a program for something that you do not understand. And I'm not saying this to hurt you. I'm telling you this so you know what you need to focus on if you want to get anywhere with programming. You should show more initiative, do your own research, be persistent and don't give up. And learn to understand mathematics.
commented Aug 10 by npatel300 (1,440 points)
I understand what the complex number is but I was struggling it in java, as I don't have more knowledge of java. I had researched about it as well, but the errors were not able to figure out to me. I also looked up for the errors and understood it and tried to fix it. But, anyway thanks for helping out in my codes. I appreciate it.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...