CSC 101 Programming Assignment # 5

Program:

Your task is to create a class that will model a complex number. In much the way that our example class in chapter 7 was a class that modeled a Fraction.

A complex number is a number of the form a + bi, where a is the “real” part and b is the “imaginary” part. It is based on the mathematical premise that i2 = -1. So like the fraction class, you will define two data members for real and imaginary.

Your class will contain the following 17 methods:

-A constructor that takes no arguments and assigns all values to 0.

-A constructor that takes one double and assigns it to the “real” portion, 0 to the other.

-A constructor that takes two doubles, and assigns them to real, then imaginary.

– 2 accessors

– 2 mutators

-A toString( ) method, outputting a String neatly in the form a + bi

Mathematical Methods- Instance methods

-2 different add methods, first takes one complex object as a parameter, the second takes a double (which is treated as the real portion of a complex only)

-2 subtract methods

-2 multiply methods

-2 divide methods

-An equals( ) method that takes a complex number as a parameter type and returns a boolean of true if the object is equal to the parameter.

These mathematical methods will be called like this:

complex_object1.add(complex_object2); OR

complex_object1.add(double_value);

NOTES:You will also be required to create a tester file that successfully tests all your class methods. Remember: All of these mathematical methods will have only one complex number passed in as a parameter, the other is the object that is calling the method. If you get it to work but you are passing two complex objects, you are doing it wrong!!!!!!!!!

Each of these programs must be done in a separate file. Name them Complex.java, ComplexTester.java. Name the class in each file these names as well. If you do not name these files correctly you will lose points.

Hand in electronically – (NOT E-mail!!!)

In S-drive CSC 101 folder:

1. Create folder called projectfive_firstname_lastname

2. Place two files named above in folder. (NOTHING ELSE)

Complex Math: A + Bi refers to A = real portion; B = imaginary portion

Adding:resulting complex number

A + Bi plus a real number C => (A + C) + (B)i

A + Bi plus C + Di => (A + C) + (B + D)i

Subtracting:resulting complex number

A + Bi minus a real number C => (A – C) + (B)i

A + Bi minus C + Di => (A – C) + (B – D)i

Multiplying:resulting complex number

A + Bi times a real number C => (A * C) + (B * C)i

A + Bi times C + Di => (AC – BD) + (AD + BC)i

Dividing:resulting complex number

A + Bi divided by a real number C => (A / C) + (B / C)i

A + Bi divided by C + Di => (AC+BD)/(C2 + D2) + (BC-AD)/(C2 + D2)i

Note: make sure C2 + D2 is not equal to zero

Equals:

means real portions of each must be the same and imaginary portions must be the same