# CSE1030 Lab 02

Week of Jan 20, 2014
Due: Mon Jan 27 before 11:59PM

## Introduction

The purpose of this lab is to implement an immutable class, which includes adding documentation in the form of Javadoc comments and generating APIs using the Javadoc tool, as well as testing against a set of unit tests.

## Question 1: Complex Numbers

Complex numbers arise when you try to compute a root of a negative number. For example, the square root of `-1` is not a real number; however, mathematicians have found that it is very useful to say that there exists some number (not real) that is the square root of `-1`. Historically, such a number was called "fictitious" or "imaginary", and is now written as the symbol i. $- 1 = i$

A complex number is a number that can be written as $a + b i$ where $a$ and $b$ are real numbers. The $a$ is called the real part of the complex number, and the $b$ is called the imaginary part of the complex number.

Complex numbers turn out to be very useful in mathematics (complex analysis, for example), physics (in the study of waves, electromagnetism, and quantum mechanics, for example), the study of signals (signal and image processing, for example), ...

Here are some elementary operations that you can do with complex numbers:

Operation Definition Example
addition $\left(a+bi\right)+\left(c+di\right)=\left(a+c\right)+\left(b+d\right)i$ $\left(3+3i\right)+\left(5-5i\right)=8-2i$
multiplication $\left(a+bi\right)×\left(c+di\right)=\left(ac-bd\right)+\left(bc+ad\right)i$ $\left(1+3i\right)×\left(2+2i\right)=\left(1·2-3·2\right)+\left(3·2+1·2\right)i=-4+8i$
magnitude $\left|\left(a+bi\right)\right|=\sqrt{{a}^{2}+{b}^{2}}$ $\left|\left(1+2i\right)\right|=\sqrt{{1}^{2}+{2}^{2}}=\sqrt{5}$

Implement a class named `Complex` in the package `cse1030.math` that represents immutable complex numbers. Some resources you will need are listed below:

Also, here is some starter code for `Complex`:

```package cse1030.math;

/**
* A class that represents immutable complex numbers.
*
* @author CSE1030_W14
*
*/
public final class Complex {

private final double re;    // the real part of this complex number
private final double im;    // the imaginary part of this complex number

/**
* Create a complex number equal to `0 + 0i`.
*
*/
public Complex() {
this.re = 0.0;
this.im = 0.0;
}

}
```

• Eclipse will generate acceptable versions of `hashCode` and `equals`
• If you cannot complete one or more of the methods, at least make sure that it returns some value of the correct type; this will allow the tester to run, and it will make it much easier to evaluate your code. For example, if you are having difficulty with the method `fromPolar` then make sure that it returns a `Complex` number created using the default constructor.

## The test program

Perhaps the most widely seen visualization based on complex numbers is the Mandelbrot set. Once you have implemented and tested the `Complex` class, you can run the Java program `Mandelbrot` that computes and draws a Mandelbrot set. You should get the following output:

## Submit

Submit your work using the following command in the directory where your `Complex.java` file is saved.

```submit 1030 L2 Complex.java
```

or use the web-based submit https://webapp.eecs.yorku.ca/submit/