# A4 complex solved

Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form

realPart + imaginaryPart * i

where i is square root of -1

Use floating-point variables to represent the private data of the class.  Provide a constructor that enables an object of this class to be initialized when it is declared.

Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform the following operations:

a) Add two Complex numbers: The real parts are added together and the imaginary parts are added together.   So, if we have (a + bi) + (c + di)), the result should be (a + c) + (b + d) i.

b) Subtract two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.  So, if we have (a + bi) - (c + di)), the result should be (a - c) + (b - d) i.

c) Multiply two Complex numbers: The real part of the result is the real part of the right operand multiplies the real part of the left operand minus the imaginary part of the right operand multiply the imaginary part of the left operand. The imaginary part of the result is the real part of the left operand multiply the imaginary part of the left operand plus the imaginary part of the left operand multiply the real part of the right operand.  So, if we have (a + bi) * (c + di)), the result should be (ac - bd) + (ad + bc) i.

d) Division two Complex numbers: We set the value of square real part of the denominator plus square imaginary part of the denominator is A.  The real part of the result is the real part of the numerator multiplies the real part of the denominator plus the imaginary part of the numerator multiply the imaginary part of the denominator and divided by A. The imaginary part of the result is the real part of the left operand multiply the imaginary part of the left operand plus the imaginary part of the left operand multiply the real part of the right operand.  So, if we have (a + bi) / (c + di)), the result should be (ac+bd)+i(bc-ad))/(c2+d2).

e) Print Complex numbers in the form (a, b), where a is the real part and b is the imaginary part.

A public class (Complex as in this assignment’s demo programs) should be implemented to do complex number calculation.

In the main function (ComplexTest as in the demo) you will instantiate two Complex class objects, call Complex class’s Addition, Subtraction, Multiply, Division function and print out the result of the calculations.

The following are the specifications for this assignment.

Complex.java

There are two private double data members (real and imaginary). The real stores the real part of a complex number and the imaginary stores the imaginary part of a complex number.

There are seven public functions (two Complex, add, subtract, multiply, division, and toString) in this classes.

The first Complex function does not take any data. (or call no argument constructor)  It initializes 0s to the private data members.
The second Complex function takes two double numbers. (or call two-argument constructor)  It assigns the two numbers to the private data members.
The add function takes one Complex object and returns one Complex object.  The function does two complex numbers addition.
The subtract function takes one Complex object and returns one Complex object.  The function does two complex numbers subtraction.
The multiply function takes one Complex object and returns one Complex object.  The function does two complex numbers multiplication.
The division function takes one Complex object and returns one Complex object.  The function does two complex numbers division.
The toString function returns a String object but does not take any data.  The function returns a parenthesis string which represents complex number.  The numbers are in floating point format with only one digit after decimal point.

ComplexTest.java

It contains main function (driver) for this assignment.  In the main function you have to do

Create two Complex objects with value of (9.5, 7.7) and (1.2, 3.1).
Print a proper formatted headline “A complex number in the form (x, y) is equal to x + yi, where i is square root of -1.”
Print a second headline "*-Complex numbers calculations-*".
If an object contains (9.5, 7.7 ) and b object contains ( 1.2, 3.1 ).  The program will use System.out.printf  function to display information by calling a.toString(), b.toString(), a.add( b ).toString(), a.subtract( b ).toString(),a.multiply( b ).toString(), and a.division( b ).toString() as arguments.
You can fine the needed techniques in chapter 1~ 10, 23 and 24.  You can also use more advance skills to do this assignment.

This assignment comes with a CISP401V9A4.zip file.  It includes two files (ComplexTest.class and Complex.class.)  Both of the ComplexTest.class and Complex.class are executable files.  You can place them in a sub folder (directory).  Bring up a command prompt and go to the sub directory, type “java ComplexTest“ in the sub directory and hit “Enter” key.  You should see the program run and get to the result as the following picture.  That should also be expecting result of this assignment.

Please document the files properly and zip your ComplexTest.java and Complex.java files into a proper named zip file for an assignment (refer to the assignment section of the class syllabus) and submit it to the A4 dropbox of the D2L Website.

Worth 150 points

Create a class called ComplexBase to store two sets of complex numbers. It is the base class for ComplexAdd, ComplexSubtract, ComplexMultiply, and ComplexDivision classes.  Complex numbers have the form

realPart + imaginaryPart * i

where i is square root of -1

Use floating-point variables to represent the private data of the class.  Provide a constructor that enables an object of this class to be initialized when it is declared.

Create ComplexAdd class to do addition for two complex numbers and store the result.  It also provides a function to return a string statement regarding the addition of two complex numbers.  The following is a general guide line for adding two complex numbers.  The real parts are added together and the imaginary parts are added together.   So, if we have (a + bi) + (c + di)), the result should be (a + c) + (b + d) i.

Create ComplexSubtract class to do subtraction for two complex numbers and store the result.  It also provides a function to return a string statement regarding the subtraction of two complex numbers.  The following is a general guide line for subtracting two complex numbers.  The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.  So, if we have (a + bi) - (c + di)), the result should be (a - c) + (b - d) i.

Create ComplexMultiply class to do multiplication for two complex numbers and store the result.  It also provides a function to return a string statement regarding the multiplication of two complex numbers.  The following is a general guide line for multiplying two complex numbers.  The real part of the result is the real part of the right operand multiplies the real part of the left operand minus the imaginary part of the right operand multiply the imaginary part of the left operand. The imaginary part of the result is the real part of the left operand multiply the imaginary part of the left operand plus the imaginary part of the left operand multiply the real part of the right operand.  So, if we have (a + bi) * (c + di)), the result should be (ac - bd) + (ad + bc) i.

Create ComplexDivision class to do division for two complex numbers and store the result.  It also provides a function to return a string statement regarding the division of two complex numbers.  The following is a general guide line for dividing two complex numbers.  We set the value of square real part of the denominator plus square imaginary part of the denominator is A.  The real part of the result is the real part of the numerator multiplies the real part of the denominator plus the imaginary part of the numerator multiply the imaginary part of the denominator and divided by A. The imaginary part of the result is the real part of the left operand multiply the imaginary part of the left operand plus the imaginary part of the left operand multiply the real part of the right operand.  So, if we have (a + bi) / (c + di)), the result should be (ac+bd)+i(bc-ad))/(c2+d2).

Create ComplexMathTest class to create 14 elements ComplexBase array to store complex numbers calculations and display the results.

The following are the specifications for this assignment.

ComplexBase.java

There are four private double data members (real1, imaginary1, real2 and imaginary2). The real stores the real part of a complex number and the imaginary stores the imaginary part of a complex number.

There are six public functions (two ComplexBase, getFirstReal, getFirstImaginary, getSecondReal, and getSecondImaginary) in this class.

public double getFirstReal( )

The first ComplexBase function does not take any data. (or call no argument constructor)  It initializes 0s to the private data members.
The second ComplexBase function takes four double numbers. (or call four-argument constructor)  It assigns the four numbers to the private data members.
The getFirstReal function returns one double number but does not take any data.  It returns the real1.
The getFirstImaginary function returns one double number but does not take any data.  It returns the imaginary1.
The getSecondReal function returns one double number but does not take any data.  It returns the real2.
The getSecondImaginary function returns one double number but does not take any data.  It returns the imaginary2.

There are two private double data members (realA and imaginaryA). The realA stores the real part of addition of two complex numbers and the imaginaryA stores the imaginary part of the addition of two complex numbers.

There are three public functions (two ComplexAdd and toString) in this class.

The first ComplexAdd function does not take any data. (or call no argument constructor)  It initializes 0s to its super class’s and this class’s private data members.
The second ComplexAdd function takes four double numbers. (or call four-argument constructor)  It assigns the four numbers to its super calss’s private data members. It also adds  the numbers and gets a result.  It assigns the real part of the result to the realA and assigns the imaginary part of the result to the imaginaryA.
The toString function returns one String object but does not take any data.  It returns a String object which is an equation of Complex numbers addition.

ComplexSubtract.java

There are two private double data members (realS and imaginaryS). The realS stores the real part of subtraction of two complex numbers and the imaginaryS stores the imaginary part of the subtraction of two complex numbers.

There are three public functions (two ComplexSubtract and toString) in this class.

The first ComplexSubtract function does not take any data. (or call no argument constructor)  It initializes 0s to its super class’s and this class’s private data members.
The second ComplexSubtract function takes four double numbers. (or call four-argument constructor)  It assigns the four numbers to its super class’s private data members. It also does subtraction on the numbers and get a result.  It assigns the real part of the result to the realS and assigns the imaginary part of the result to the imaginaryS.
The toString function returns one String object but does not take any data.  It returns a String object which is an equation of Complex numbers subtraction.

ComplexMultiply.java

There are two private double data members (realM and imaginaryM). The realM stores the real part of multiplication of two complex numbers and the imaginaryM stores the imaginary part of the multiplication of two complex numbers.

There are three public functions (two ComplexMultiply and toString) in this class.

The first ComplexMultiply function does not take any data. (or call no argument constructor)  It initializes 0s to its super class’s and this class’s private data members.
The second ComplexMultiply function takes four double numbers. (or call four-argument constructor)  It assigns the four numbers to its super class’s private data members. It also multiplies the numbers and gets a result.  It assigns the real part of the result to the realM and assigns the imaginary part of the result to the imaginaryM.
The toString function returns one String object but does not take any data.  It returns a String object which is an equation of Complex numbers multiplication.
Complex Division.java

There are two private double data members (realD and imaginaryMD. The realD stores the real part of division of two complex numbers and the imaginaryD stores the imaginary part of the division of two complex numbers.

There are three public functions (two Complex Division and toString) in this class.

The first Complex Division function does not take any data. (or call no argument constructor)  It initializes 0s to its super class’s and this class’s private data members.
The second Complex Division function takes four double numbers. (or call four-argument constructor)  It assigns the four numbers to its super class’s private data members. It also divides the numbers and gets a result.  It assigns the real part of the result to the realD and assigns the imaginary part of the result to the imaginaryD.
The toString function returns one String object but does not take any data.  It returns a String object which is an equation of Complex numbers division.

ComplexMathTest.java

It contains main function (driver) for this assignment.  In the main function you have to do

Create 14 elements ComplexBase objects array. It name is complex.
Create a ComplexAdd object with (9.5, 7.7, 1.2, 3.1) as arguments and assign to the first element of the complex array.
Create a ComplexSubtract object with (9.5, 7.7, 1.2, 3.1) as arguments and assign to the second element of the complex array.
Create a ComplexMultiply object with (9.5, 7.7, 1.2, 3.1) as arguments and assign to the third element of the complex array.
Create a ComplexDivision object with (9.5, 7.7, 1.2, 3.1) as arguments and assign to the fourth element of the complex array.
Create a ComplexAdd object with (-6.3, 5.2, 3.4, -2.8) as arguments and assign to the fifth element of the complex array.
Create a ComplexSubtract object with (-6.3, 5.2, 3.4, -2.8) as arguments and assign to the sixth element of the complex array.
Create a ComplexMultiply object with (-6.3, 5.2, 3.4, -2.8) as arguments and assign to the seventh element of the complex array.
Create a ComplexDivision object with (-6.3, 5.2, 3.4, -2.8) as arguments and assign to the eighth element of the complex array.
Create a ComplexDivision object with (-6.3, 5.2, 0.0, 0.0) as arguments and assign to the ninth element of the complex array.
Create a ComplexAdd object without any argument and assign to the tenth element of the complex array.
Create a ComplexSubtract object without any argument and assign to the eleventh element of the complex array.
Create a ComplexMultiply object without any argument and assign to the twelfth element of the complex array.
Create a ComplexDivision object without any argument and assign to the thirteenth element of the complex array.
Create a ComplexDivision object with (0, 0, 0, 0.1) as arguments and assign to the fourteenth element of the complex array.
Print a proper formatted headline “A complex number in the form (x, y) is equal to x + yi, where i is square root of -1.”
17.  Print a second headline "* ~~~~~~~********--------Complex numbers calculations--------********~~~~~~~”

18.  Use the following statement to print out all the complex number equations and results.

for (ComplexBase currentComplex : complex)

System.out.println( currentComplex);

You can fine the needed techniques in chapter 1~ 10, 23 and 24.  You can also use more advance skills to do this assignment.

This assignment comes with a CISP401V9AD4.zip file.  It includes six files (ComplexBase.class, ComplexAdd.class, ComplexSubtract.class, ComplexMultiply.class, ComplexDivision.class, and ComplexMathTest.class).  All of those files are executable files.  You can place them in a sub folder (directory).  Bring up a command prompt and go to the sub directory, type “java ComplexMathTest“ in the sub directory and hit “Enter” key.  You should see the program run and get to the result as the following picture.  That should also be expecting result of this assignment.