CSE1030Z Lab 03

Thu Jan 24 2:30-4:00 PM

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. This lab will be graded for style and correctness.

There is one question to this lab. You need to submit your solution by the end of the lab session.

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 a + b i + c + d i = a + c + b + d i 3 + 3 i + 5 - 5 i = 8 - 2 i
multiplication a + b i × c + d i = a c - b d + b c + a d i 1 + 3 i × 2 + 2 i = 1 · 2 - 3 · 2 + 3 · 2 + 1 · 2 i = - 4 + 8 i
magnitude a + b i = a 2 + b 2 1 + 2 i = 1 2 + 2 2 = 5

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

A JUnit tester for your class is available here. Instructions for using JUnit in Eclipse are here. Instructions for using JUnit from the command line are here. 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).

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 this Java program that computes and draws a Mandelbrot set. You need to add the jar file

/cse/dept/www/course/classpath/1030Z/princeton_introcs.jar

to your project in Eclipse to run this program; if you are compiling from the command line then the jar file is already in your classpath. You should get the following output:

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

submit 1030Z L3R Complex.java

Note: valueOf is probably the most challenging method to implement in this lab. If you found it easy to implement, then consider implementing a version of valueOf that is insensitive to the number of spaces surrounding the sign separating the real and imaginary parts; your method should be able to produce complex numbers even where there are no spaces surrounding the sign. This is harder than it sounds...