# Programming Assignment 6 Solution

\$30.00

Category:

## Description

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 i 2 = -1 . 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.

imaginary axis

b

z = a +bi

 a real axis

• = a bi

For this project we are concerned mainly with the Several common operations are defined below. numbers.

arithmetic and algebraic properties of complex numbers. Let  z = a +bi  and  w = c + di  be particular complex

Subtract:

Multiply:

w = (a + c) + (b + d )i

• w = (a c) + (b d )i zw = (ac bd) + (ad + bc)i

Reciprocal:

 Divide: w = z

• =

z

• ac

ç 2 è a

a

ç

è a2 +

• bd ö

÷

• b2 ø

 b 2 +

 ö + æ – b ÷ ç 2 + b 2 ø è a æ ad – bc ö ç ÷ è a 2 + b 2 i ø

ö

÷i

ø

Conjugate:    z = a bi

Real and Imaginary parts:

Re(z) = a

and
Im( z) = b

 Absolute value (also called modulus):  z = a2 + b2 Argument:  arg( z) = the angle q in the range -π < θ £ π  that z makes with the positive real axis

Be in class (or see the notes from) Wednesday 3-2-16 for a discussion of the preceding definitions.  Some

of these calculations can be simplified by the fact that zz = a2 + b2 .  For instance

 w = w z . z z z

z

• (zz )1/ 2

,    1z = zzz  and

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.

2

error: Content is protected !!