CMPS 12A Introduction to Programming Programming Assignment 6

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (5 votes)

In this programming assignment you will complete the Complex class discussed in class that represents
complex numbers as a pair of double values. Recall that a complex number z is an expression of the form
z  a bi
where a and b are real numbers, and i is the so-called imaginary unit having the property that
1
2
i   . Complex numbers are used in many branches of physics and engineering to solve real world
problems, in spite of their seemingly abstract nature. Complex numbers have a simple geometric
interpretation as vectors in a 2-dimensional plane.
For this project we are concerned mainly with the arithmetic and algebraic properties of complex numbers.
Several common operations are defined below. Let
z  a bi
and
w c  di
be particular complex
numbers.
Add: z  w  (a  c)  (b  d)i
Subtract:
z  w  (a  c)  (b  d)i
Multiply:
zw  (ac  bd)  (ad  bc)i
Reciprocal: i
a b
b
a b
a
z








 







2 2 2 2
1
Divide: i
a b
ad bc
a b
ac bd
z
w








 








2 2 2 2
Conjugate:
z  a bi
Real and Imaginary parts: Re(z)  a
and
Im(z)  b
Absolute value (also called modulus):
2 2
z  a  b
Argument:
arg(z)  the angle in the range -π  θ  π that z makes with the positive real axis z  a bi z  a bi
imaginary axis
real axis
a
b
2
Be in class (or see the notes and webcast from) Tuesday 3-7-17 for a discussion of the preceding definitions.
Some of these calculations can be simplified by the fact that
2 2
zz  a  b
. For instance
 
1/ 2
z  zz ,
zz
z
z

1
and
zz
wz
z
w
 .
Your task in this project will be to fill in the template Complex.java posted in the Examples section of the
course website in the folder pa6. The template includes stubs for the above complex arithmetic functions,
along with some other required functions listed and described below.
Complex(String s){}
This is perhaps the most difficult function to write. It is a constructor for the Complex class that reads a
string s and parses it as a complex number. It should accept strings of the form ” 47i
“, ” 3.58.2i
“, ”
5.1
“, ” 17i
“, etc. It will throw a NumberFormatException if the input string s cannot be parsed. See
the example ComplexParser.java also posted on the website for help in doing this.
public String toString()
This function overrides Object’s toString() method and returns the string representation of a complex
number. The strings returned will be exactly those accepted by the above constructor.
public boolean equals(Object obj)
This function also overrides it’s namesake in the Object superclass. It will return true if and only if this
Complex and (Complex)obj have the same real and imaginary parts.
static Complex valueOf(double a, double b)
static Complex valueOf(double a)
static Complex valueOf(String s)
These functions mimic the valueOf() methods appearing in the Java wrapper classes. Each of them
returns a new Complex object obtained by calling an appropriate constructor.
What to turn in
A file called ComplexTest.java is posted on the webpage that exercises the functions you define in class
Complex. Sample input and output for ComplexTest will also be posted. You are to submit
ComplexTest.java (with no changes of any kind) with this project. A Makefile for the project is also posted.
This Makefile compiles both ComplexTest.java and Complex.java and places them in an executable Jar file
called ComplexTest. The Makefile also includes phony targets called clean, spotless and submit, that
remove all .class files, remove the executable Jar file and submit the project (respectively). Submit the
three files: Makefile, ComplexTest.java and Complex.java to the assignment name pa6 by the due date.
This project is considerably easier than the two most recent programs, but it still takes some time to
complete, so do not wait until the last minute to start.