EECS2030 Lab 03

Due before 11:59PM on Friday October 11, 2019.

Introduction

The purpose of this lab is to implement an immutable class having primitive fields. The class includes overridden versions of the equals, hashCode, and toString methods, as well as several static factory methods.

You may, and are encouraged to, work in groups of up to 3 students from the same lab section.

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\). $$\sqrt{-1} = i$$

A complex number is a number that can be written as $$a + bi$$ 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), and other fields of science and engineering.

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

Operation Definition Example
addition \((a + bi) + (c + di) = (a + c) + (b + d)i\) \((3 + 3i) + (5 - 5i) = 8 - 2i\)
multiplication \((a + bi) \times (c + di) = ((a \times c) - (b \times d)) + ((b \times c) + (a \times d))i\) \((1 + 3i) \times (2 + 2i) = -4 + 8i\)
magnitude \(|(a + bi)| = \sqrt{a \times a + b \times b}\) \(|(1 + 2i)| = \sqrt{1 \times 1 + 2 \times 2} = \sqrt{5}\)

Getting started

  1. Download this zip file containing the Lab 3 eclipse project.
  2. Import the project into eclipse by doing the following:
    1. Under the File menu choose Import...
    2. Under General choose Existing Projects into Workspace and press Next
    3. Click the Select archive file radio button, and click the Browse... button.
    4. In the file browser that appears, navigate to your download directory (exactly where this is depends on what computer you working on; on the lab computers the file will probably appear in your home directory)
    5. Select the file lab3.zip and click OK
    6. Click Finish.

Implement a class named Complex in the package eecs2030.lab3 that represents immutable complex numbers. Your class must provide the API shown here.

A JUnit tester for your class is available in the project that you downloaded. Note that the tester is not very thorough, and it may not catch all errors that you might make. Also note that passing all of the tests in this tester does not guarantee a good solution (in other words, you should think critically about your implementation for each method).

Question 2: Visualize the Mandelbrot set

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 use your newly created class to compute the Mandelbrot set.

Consider the sequence defined by the function $$z_{n + 1} = z_n^2 + c$$ where \(z_n\) and \(c\) are both complex numbers.

Suppose that we choose \(z_0 = 0 + 0i\); then we have $$z_1 = z_0^2 + c = (0 + 0i)^2 + c = c$$

Having computed \(z_1\) we can now compute \(z_2\): $$z_2 = z_1^2 + c = c^2 + c$$

Having computed \(z_2\) we can now compute \(z_3\), and so on. If we repeat the process an infinite number of times, what happens to the magnitude of \(z_{n + 1}\)? The answer depends on the value of \(c\).

The Mandelbrot set is defined as the set of complex values \(c\) for which the magnitude of \(z_{n + 1}\) remains less than or equal to \(2\) as \(n \rightarrow \infty\); we say that the magnitude of \(z_{n + 1}\) remains bounded for elements of the Mandelbrot set. The Mandelbrot set is most often visualized as a picture similar to one shown below.



In the picture, the horizontal axis represents the real part of a complex number and the vertical axis represents the imaginary part of a complex number. Black areas of the picture correspond to complex numbers that are elements of the Mandelbrot set. The non-black areas of the picture are shaded in a color that depends on the value of \(n\) when the magnitude of \(z_{n+1}\) becomes greater than \(2\).

When computing the Mandelbrot set, we can't actually iterate the function \(z_{n + 1} = z_n^2 + c\) an infinite number of times. Instead, we choose some maximum number of times that we are willing to perform the iteration and assume that the final result for \(z_{n+1}\) is a reasonable approximation to the actual value when \(n \rightarrow \infty\).

Implement a class named MandelbrotUtil in the package eecs2030.lab3 that has a single method that counts the number of iterations of the function \(z_{n + 1} = z_n^2 + c\) that are needed to determine if \(z_{n + 1}\) is bounded for some input complex number \(c\) (counts the number of times that you can iterate the function before the magnitude of \(z_{n + 1}\) exceeds \(2\)). Your class must provide the API shown here.

A JUnit tester for your class is available in the project that you downloaded. Note that the tester is not very thorough, and it may not catch all errors that you might make. Also note that passing all of the tests in this tester does not guarantee a good solution (in other words, you should think critically about your implementation for each method).

Once you are satisfied that your Complex and MandelbrotUtil classes are implemented correctly, you can try running the DrawMandelbrot program included in the project that you downloaded. It should produce an image similar to the one shown above. Note that the program takes several seconds to compute and display the picture.

See the API for DrawMandelbrot for instructions on how to change the viewing parameters of the program. If you want to make bigger or smaller pictures, you can change the constant N in the main method of the program.

Submit for students NOT working in a group

If you are not working in a group, submit your solution using the submit command. Remember that you first need to find your workspace directory, then you need to find your project directory. In your project directory, your files will be located in the directory src/eecs2030/lab3

submit 2030 lab3 Complex.java MandelbrotUtil.java

Submit for students working in a group

Students working in a group should submit only one submission for the entire group. If you are working in a group, create a plain text file named group.txt. You can do this in eclipse using the menu File -> New -> File. Type your login names into the file with each login name on its own line. For example, if the students with login names rey, finn, and dameronp, worked in a group the contents of group.txt would be:

rey
finn
dameronp

Submit your solution using the submit command. Remember that you first need to find your workspace directory, then you need to find your project directory. In your project directory, your files will be located in the directory src/eecs2030/lab3

submit 2030 lab3 Complex.java MandelbrotUtil.java group.txt

Submit your work from outside the Prism lab

It is possible to submit work from outside the Prism lab, but the process is not trivial; do not attempt to do so at the last minute if the process is new to you. The process for submitting from outside of the Prism lab involves the following steps:

  1. transfer the files from your computer to the undergraduate EECS server red.eecs.yorku.ca
  2. remotely log in to your EECS account
  3. submit your newly transferred files in your remote login session using the instructions for submitting from within the lab
  4. repeat Steps 1 and 3 as required

Windows users will likely need to install additional software first. Mac users have all of the required software as part of MacOS.

Detailed instructions are here.