Starting from:

$30

Project 5 CMPT220L The Complex Class

Project 5
CMPT220L

Points: 100
The Complex Class
Problem Description
A complex number is a number of the form a+bi, where a and b are real numbers and i is √
−1. The numbers
a and b are known as the real part and imaginary part of the complex number, respectively. You can perform
addition, subtraction, multiplication, and division for complex numbers using the following formula:
a + bi + c + di = (a + c) + (b + d)i
a + bi − (c + di) = (a − c) + (b − d)i
(a + bi) ∗ (c + di) = (ac − bd) + (bc + ad)i
(a + bi)/(c + di) = (ac + bd)/(c
2 + d
2
) + (bc − ad)i/(c
2 + d
2
)
You can also obtain the absolute value for a complex number using the following formula:
|a + bi| =
p
a
2 + b
2
Deliverables
1. Create a UML diagram of a class named Complex that extends the Number class for representing
complex numbers. 15 Points
2. Implement the class in Java (Complex.java) and add the following methods:
(a) add, subtract, multiply, divide, abs for performing complex-number operations. 15 Points
(b) Override toString method for returning a string representation for a complex number. The
toString method returns a + bi as a string. If b is 0, it simply returns a. 15 Points
3. Provide three constructors Complex(a,b), Complex(a), and Complex(). Complex() creates a Complex
object for number 0 and Complex(a) creates a Complex object with 0 for b. 15 Points
4. Provide the getRealPart() and getImaginaryPart() methods for returning the real and imaginary
part of the complex number, respectively. 15 Points
5. Implement the Cloneable and Comparable interfaces.
6. Create Java program called Project4.java that prompts the user to enter two complex numbers and
display the result of their addition, subtraction, multiplication, and division. 15 Points
Here is a sample run:
Enter the first complex number: 3.5 5.5
Enter the second complex number: -3.5 1
(3.5 + 5.5i) + (-3.5 + 1.0i) = 0.0 + 6.5i
(3.5 + 5.5i) - (-3.5 + 1.0i) = 7.0 + 4.5i
1
(3.5 + 5.5i) * (-3.5 + 1.0i) = -17.75 + -15.75i
(3.5 + 5.5i) / (-3.5 + 1.0i) = -0.5094 + -1.7i
|3.5 + 5.5i| = 6.519202405202649
7. Comment your code. 10 Points
Extra Credit (Optional) 30 points
If z1, z2, z3 are three noncollinear points in the complex space, then the directed or signed area A(z1, z2, z3)
of the triangle with these vertices is given by
A(z1, z2, z3) = i
4
 
 
 
 
 
 
z1 z1 1
z2 z2 1
z3 z3 1
 
 
 
 
 
 
(1)
Write a program that takes three points specified by their x and y coordinates and calculates the area
using complex numbers. Assume that zn = xn + yni. Compare your result with the formula we used
previously in class to calculate the area of a triangle. Call your program Extra.java. Note: Use https:
//en.wikipedia.org/wiki/Determinant to solve equation (1).
Submission
Submit the following items:
1. Create a PDF file with the UML diagram and submit to GitHub.
2. Compile, test, and submit your Java program to GitHub (you must submit the program regardless
whether it’s complete or incomplete, correct or incorrect)
Place your .java file under the corresponding folder in your local copy of the GitHub repository,
commit and push it to the remote repository. Make sure that the professor has access to the repository
(jfac65-marist).
cmpt220lastname\
prj\
5\
Project5.pdf
Project5.java
Complex.java
2

More products