Project 3

Due Thursday 3/11 at 3pm

- Writing instance methods
- Writing your own class
- Using a for-loop
- Writing client code to make use of a class
- Running Java applications

`q1q5.txt`

. You must use the identifiers (variable names, class names, etc.) exactly as we specify, including capitalization. Use good programming style and observe the style guidelines that are given in the Project 1/2 Grading Guides. As you finish the project you should have a much better understanding of classes and Java applications.
Using conventional real numbers, we cannot take the square root of any negative numbers. As it turns out, taking the square root of negative numbers actually happens quite often in higher mathematics and has real physical implications. Sufficed to say, mathematicians needed some way to conceive of square roots of negative numbers. Complex numbers were born!

Complex numbers take the form `a + b`

, where *i*

is the square root of -1. Notice that a complex number is comprised of two components, a real part *i*`a`

and an imaginary part `b`

. To get some intuition, consider the quadratic formula:

For an equationConsider the equation`, the solution for`

ax^{2}+bx +c= 0`x`

is given by:

x = -b± sqrt(b^2 -4ac)2 a

`x`^{2} - 3x + 3

. Solving for x results in `3 ± sqrt(-3)`

which is not a real number. We can represent this result as an imaginary number, `3 ± `*i**sqrt(3)

.
You can do the same basic arithmetic with imaginary numbers as you could with real numbers. In fact, much of the math is done as if you were treating

as a variable. Operations include:
*i*

- Addition:
`(a + b`

*i*) + (c + d*i*) = (a + c) + (b + d)*i* - Subtraction:
`(a + b`

*i*) - (c + d*i*) = (a-c) + (b-d)*i* - Multiplication:
`(a + b`

*i*) * (c + d*i*) = (ac - bd) + (ad + bc)*i* - Division:
`(a + b`

*i*) / (c + d*i*) = (ac + bd) / (c^{2}+ d^{2}) + (bc - ad)*i*/ (c^{2}+ d^{2})

In addition, we can perform these additional operations:

- Magnitude:
`abs(a + b`

*i*) = sqrt(a^{2}+ b^{2}) - Conjugate:
`conjugate(a + b`

*i*) = a - b*i*

Your task is to write a class called ComplexNumber to represent a complex number. Remember that a complex number consists of a real part and an imaginary part - you should have class variables

`a`

and `b`

to represent these values. Think about this `q1q5.txt`

.
- Should the variables
`a`

and`b`

be instance or static? Why? - In order to match other pre-defined Java classes representing mathematical quantities, we want to make it possible to write the following statements in a client class that uses class
`ComplexNumber`

:

`ComplexNumber cn = new ComplexNumber(5, 2);`

double x= cn.a + cn.b;

What modifier should you place in the declaration of the variables in the class to make this possible? - If your class is called ComplexNumber, what should the java file be called?
- Is the ComplexNumber class an application? Why or why not?

`a`

and `b`

representing the real and imaginary parts of a complex number.
Next, write a constructor for a complex number. This constructor should take two parameters, the real and the imaginary components, and set the instance variables to these parameters. Once you have finished the constructor, you can test it in Dr. Java. Create a new instance of a ComplexNumber and then access the `a`

and `b`

fields. Are they what you expect?

Now that you have your constructor, you should implement the methods listed below.In the Interactions Pane

>ComplexNumber c = new ComplexNumber(5, 4);

>c.a

5.0

>c.b

4.0

`public void add(ComplexNumber cn)`

This method adds the parameter`cn`

to the complex number represented by`this`

instance.`public void subtract(ComplexNumber cn)`

This method subtracts the parameter`cn`

to the complex number represented by`this`

instance.`public void multiply(ComplexNumber cn)`

This method multiplies the complex number represented by`this`

instance by the parameter`cn`

.`public void divide(ComplexNumber cn)`

This method divides the complex number represented by`this`

instance by the parameter`cn`

.`public double magnitude()`

This method returns the magnitude of`this`

complex number.`public ComplexNumber conjugate()`

This method returns a new instance of ComplexNumber that is the conjugate of`this`

complex number

In addition, write the following methods for convenience:

`public boolean isComplex()`

This method returns true if and only if the imaginary component of`this`

complex number is non-zero`public String toString()`

This method returns a String representation of`this`

complex number. For example:- A complex number with real part 4 and imaginary part -5 should result in a String "4.0 - 5.0i"
- A complex number with real part 3 and imaginary part 6 should result in a String "3.0 + 6.0i"
- A complex number with real part 2 and no imaginary part (i.e., the imaginary part has the value zero) should result in a String "2.0"
- A complex number with no real part and imaginary part -3 should result in a String "-3.0i"
- A complex number with no real part or imaginary part (i.e. 0) should result in the String "0.0"

You do not have to control the number of decimal places printed.

Notice that the magnitude of both complex numbers is equal to 1. (Recall that

`abs(a + b`*i*) = sqrt(a^{2} + b^{2}).)

In fact, every complex number that lies on the displayed circle has a magnitude of 1, i.e., everyone complex number that lies on a circle of radius *r*cosθ + *r*sinθ

```
```

where *r* is the magnitude of the complex number and θ ranges from 0 to 360 degrees (or 0 to 2π radians). Write another method in ComplexNumber called `complexCircle`

. This method should display the complex numbers corresponding to θ = 0, 45, 90, 135, 180, 225, 270, and 315 degrees where *r* is the magnitude of `this`

instance. For example, given the complex number 1 + 1*i*, we have the following 'complex circle':

θ (in degrees) Complex Number
0 sqrt(2)
45 1 + 1*i*
90 sqrt(2)*i*
135 -1 + 1*i*
180 -sqrt(2)
225 -1 - 1*i*
270 -sqrt(2)*i*
315 1 - 1*i*

**Note** that Java's `Math.sin`

and `Math.cos`

take radians, **not** degrees! To convert from degrees to radians: *radians* = π * *degrees* / 180

*(Hint) Figure out on paper what the various θ angles are when expressed in radians.*
####
4. Client Code

Download `Project3.java`

from the *Projects* page. This file contains skeleton code for a Java application. The `main`

method provides the user with a menu in order to manipulate complex numbers. In addition, there are several other methods in the class. You will need to complete two of the methods. **Read the methods and their comments and make sure you understand what they do. Be sure that you understand the provided code before you start filling in the missing parts.** Notice that the instance methods you have written in class `ComplexNumber`

are called by the "Op methods" (see method `addOp`

for example). You will finish two incomplete methods: `promptForComplexNumber`

and the `main`

method.

- In the
`promptForComplexNumber`

method, you should prompt the user for the real and imaginary parts of a complex number and create an instance of a ComplexNumber representing the input values. Use `JLiveRead`

for your input (see lesson 1-5 of the ProgramLiveCD for more information).
- In the
`main`

method, the skeleton code displays a menu for manipulating complex numbers and asks the user for their choice, which is then stored in the `int`

variable `choice`

. Write a series of conditional statements that executes the appropriate Op method based on the choice

Complete these methods - but do *not* change any of the other skeleton code.
#### 5. Testing your code

Once you complete `Project3`

and `ComplexNumber`

, you can try it out in the Interactions Pane. Make sure you have Project3, ComplexNumber, and JLiveRead open. Compile the three files and then type:
>java Project3

in the Interactions Pane. This will run your Java application! Try all the operations. Then answer the following questions in `q1q5.txt`

:
- Run your application and display the 'complex circle' for the complex number 1 + i. Explain why the results displayed do not match up with the answers from the example given above
- Usually, a
`for`

-loop header contains an update that is an increment (or decrement) statement. The `for`

-loop header in the `main`

method in `Project3.java`

does not have an update, yet the `for`

-loop terminates eventually. Why? The update in this `for`

-loop is not an increment statement, what is it?

#### 6. Bonus!

Control the print format so that *one decimal place* is shown in the real and imaginary parts of a complex number. *Hint:* check out the `DecimalFormat`

class in the API.
#### 7. Submitting Your Work

Submit your `Project3`

and `ComplexNumber`

files on-line using **CMS** (Course Management System) before the project deadline. **Make sure you are submitting the correct, up to date, .java files (***not* .class or .java~). We will not accept any files after the deadline for any reason (except for documented medical reasons). See the **CMS** link on the web page for instructions on using CMS. If necessary, turn off the DrJava feature that saves the **.java~** files (see course webpage announcement on 2/17 for instructions).