Thu Jan 24 2:30-4:00 PM
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.
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.
A complex number is a number that can be written as where and are real numbers. The is called the real part of the complex number, and the 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 | ||
multiplication | ||
magnitude |
Implement a class named Complex
in the package cse1030
that represents immutable complex numbers. Your class must provide
the API shown here.
private final
attributes of type double
to store the
real and imaginary parts.hashCode
and equals
valueOf
then make sure that the method returns a Complex
number created using the default constructor.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...