# Java and Math, yikes!

• November 22nd, 2011, 09:15 AM
Tfence
Java and Math, yikes!
Create a class called Complex for performing arithmetic with complex numbers.
Complex numbers have the form
realPart + imaginaryPart * i (e.g., a + bi)
where i is : square root(1)

The class should contain:
• Two double data fields x and y that represent the real and imaginary part of a complex number.
• A no-arg constructor that creates a complex number (0 + 0i).
• A constructor that creates a Complex object with specified real and imaginary part of the complex number.
• Two get methods for data fields x and y, respectively.
• A method named add that returns the addition of this complex number with another. Complex numbers are added by adding the real and
imaginary parts of the summands. That is to say:
(a+bi) + (c+di) = (a+c) + (b+d)i
The method is: public Complex add(Complex c) {}
• A method named subtract that returns the subtraction of this complex
number with another. Subtraction is defined by:
(a+bi) + (c+di) = (a-c) + (b-d)i
The method is: public Complex subtract(Complex c) {}
• A method named multiply that returns the multiplication of this complex
with another. The multiplication of two complex numbers is defined by
the following formula:
( a+bi )( c+di ) = ( ac – bd ) + ( bc + ad )i
The method is: public Complex multiply(Complex c) {}
• AtoString() method that prints a Complex object in the form (a)+(b)i, where a is the real part and b is the imaginary part.

I don't even know how to do this is in math class, yet alone java, can someone point me in the right direction?
• November 22nd, 2011, 10:11 AM
JavaPF
Re: Java and Math, yikes!
Hello and welcome to the forums.

The instructions are laid out infront of you..

You can at least start by creating a class called Complex and add the parts you understand.
Even beginners should know how to add two double variables and a method etc. This stuff is basics.

Please post your attempt at the code and we can take it from there.
• November 22nd, 2011, 12:44 PM
Tfence
Re: Java and Math, yikes!
Quote:

Originally Posted by JavaPF
Hello and welcome to the forums.

The instructions are laid out infront of you..

You can at least start by creating a class called Complex and add the parts you understand.
Even beginners should know how to add two double variables and a method etc. This stuff is basics.

Please post your attempt at the code and we can take it from there.

I'm studying for a chem exam atm, but I shall try the methods later and get back to ya.

The second and third bullet points confuse me though. We really never talked about "constructors"

Also, for the get methods, do I just do something like this:
Program program = new Program();
Class progClass = program.getClass();
Method Getx = progClass.getX (or Method GetY = progClass.getY
• November 22nd, 2011, 02:24 PM
copeg
Re: Java and Math, yikes!
Quote:

Originally Posted by Tfence
We really never talked about "constructors"

That shouldn't preclude you from trying to learn about them yourself...google the term and you will find ample descriptions
• November 22nd, 2011, 02:53 PM
Tfence
Re: Java and Math, yikes!
Quote:

Originally Posted by copeg
That shouldn't preclude you from trying to learn about them yourself...google the term and you will find ample descriptions

Thank you.