CMPS 144 Spring 2022
Prog. Assg. #1: Complex Numbers
Due: 11:59pm, February 11

For this assignment, you are to complete the ComplexNumber class, several methods (and one constructor) of which are "stubbed" (i.e., left for you to provide the bodies). As the name of the class suggests, an instance thereof represents a complex number. Such numbers are usually expressed in the form a + bi, where a and b are real numbers and i denotes the square root of −1. The two terms, a and bi, are referred to, respectively, as the real and imaginary parts of the complex number.

The only really interesting methods are the mutators by which an instance of the class can be modified either via addition, subtraction, multiplication, or division with another complex number.

The rules governing these operations are as follows:

Addition: (a + bi) + (c + di)  =  (a+c) + (b+d)i
Subtraction: (a + bi) − (c + di)  =  (a−c) + (b−d)i
Multiplication: (a + bi) × (c + di)  =  (ac − bd) + (ad + bc)i
Division: (a + bi) / (c + di)  =  (ac + bd)/(c2 + d2) + ((bc − ad)/(c2 + d2))i

Provided is the Java application ComplexNumTester for testing your work. A sample user/program dialog is shown below, with user input shown in boldface.

Welcome to the Complex Number Tester Program.

> 2 6
2 + 6i

> + 3 -4
5 + 2i

> * 5 8
9 + 50i

> / 5 8
5 + 2i

> - 2.5 7.3
2.5 + -5.3i

> * 0 1
5.3 + 2.5i

> H
Examples of commands:
Q        (quit)
H        (print this help info)
C        (print current complex number)
4.5 -2.8 (set current complex number to 4.5 - 2.8i)
+ 1 5    (add 1 + 5i to current complex number)
- 7 4    (subtract 7 + 4i from current complex number)
* 2 9    (multiply current complex number by 2 + 9i)
/ 11 -4  (divide current complex number by 11 - 4i)

> C
5.3 + 2.5i

> Q
Goodbye.

Program Submission

Using the file submission system (see link on course web page), you are to submit the ComplexNumber.java file to the prog1_dir folder.