The Mandelbrot Set in Java

The Mandelbrot Set is defined by a simple iterative function:

zt+1 = zt2 + c
z0 = 0

This is an iterative equation because the value of zt depends on the value of zt-1. Given the values of z0 it seems trivial to write a computer program that calculates zt for arbitrary t. In fact matters are a little more complicated than they seem at first. In particular, both z and c are complex numbers with both real and imaginary parts. Furthermore, depending on the value of c, this equation may exhibit sensitive dependence on initial conditions and become chaotic.

The Mandelbrot set is defined as those points in the complex plane for which this iteration does not diverge (reach infinity; i.e. grow arbitrarily large). It can be shown that if the absolute value of this iteration exceeds 2, then it diverges.

Background Reading

Complex Arithmetic

Book coverComplex Variables
by Murray R. Spiegel
Complex Variables and Applications
by James Ward Brown and Ruel V. Churchill

Mandelbrot Set

Computer Recreations, by A. K. Dewdney
sci.fractals FAQ - The Mandelbrot Set (http://www.faqs.org/faqs/fractal-faq/section-6.html)

You may also want to search the Web for more info. There are many existing Mandelbrot set programs on the Web, many available with source, including at least one by your professor. While it may be educational to investigate these, I very much suspect that none of them would actually be an answer to the specific problems described here.

Homework

Note that this is not written in stone and details will change. Furthermore, this may not be your only homework for a given class.

Problem 1:

Write a program that prints 100 iterations of the Mandelbrot equation for a fixed value of c (e.g. -0.2 + 0.8i). Also print (at the beginning of the output) the values used for c.

Use full-length variable names where plausible.

Indent code with spaces rather than tabs.

Do not overdo it. This is a straight-forward and simple problem that requires a straight-forward and simple answer. It can be solved in a single method, without defining more than one class, using arrays or exceptions, or accessing any parts of the class library we have not yet discussed.

Include your name, email address, homework number (1), and a brief description of the problem in header comments. Include other comments as necessary.

Problem 2:

Write a program that reads c and the number of generations from the command line, and prints that many iterations of the Mandelbrot equation. Also print (at the beginning of the output) the value used for c.

Be sure to handle all user errors (e.g. not providing two values on the command line) to the extent possible. Check the ranges of the data the user enters (e.g. don't let the number of generations be less than 0). Print an error message and exit if a value outside the allowed range is entered. Provide a default value if none is given.

Problem 3:

Write a program that reads c and the number of generations from the command line, and prints that many iterations of the Mandelbrot equation. Also print (at the beginning of the output) the value used for c.

Be sure to handle all user errors (e.g. not providing two values on the command line) to the extent possible. Check the ranges of the data the user enters (e.g. don't let the number of generations be less than 0). Print an error message and exit if a value outside the allowed range is entered. Provide a default value if none is given.

Detect if divergence has occurred (absolute value greater than 2) and stop output if so. Note that this test does not require you to know about or use any square root, power, exponent, or other mathematical functions that Java may or may not have.

Problem 4:

Write a ComplexNumber class which provides:

Make the interface to this class as natural and similar to the existing numeric primitive data types as possible. In particular, you may want to investigate the interfaces of the java.math.BigInteger and java.math.BigDecimal classes.

Rewrite Problem 3 using this ComplexNumber class.

Problem 5:

Write a MandelbrotUtilities class with two methods:

Write a simple command line user interface for this class in a different class. This should allow the user to input a value of c and a number of generations. It should tell the user whether or not the equation diverged, and if it did diverge, how many generations were required to prove this.

Problem 6:

Add equals(), hashCode(), and toString() methods to the ComplexNumber class. Be sure to think carefully about what it means for two complex numbers to be equal and document and justify your choice.

Problem 7:

Add a method that returns the absolute value of a complex number to the ComplexNumber class.

Rewrite Problem 5 to take advantage of this new method.

Problem 8:

Make the ComplexNumber class cloneable.

Problem 9:

Place the ComplexNumber class in a suitably named package. Then rewrite Problem 5 to use this new class. However, leave the Problem 5 classes in the default package.

Problem 10:

Write a version of the ComplexNumber class that uses java.math.BigDecimal instead of doubles internally. (Does it make sense to provide overloaded versions of the public methods that take big decimals or strings as arguments instead of doubles?) Place it in the same package used above. Then rewrite Problem 5 to use this new class. However, leave the problem 5 classes in the default package.

Problem 11:

Write an abstract superclass for complex numbers of which the two previous problem classes can be subclasses. Make them subclasses of this new superclass. Put it in the same package as above. Then rewrite the problem 5 classes to use this new class with a user option to choose double or arbitrary precision.

Problem 12:

Rewrite the user interface class(es) from previous problems to properly handle the case where the user enters a value for c or the number of generations that is not recognizable as a number.

Problem 13:

To the complex number class add a valueOf() method modeled after the valueOf() methods of the java.lang.Number subclasses such as java.lang.Double that converts a String to a ComplexNumber. Begin by writing at least ten different examples of various forms you will recognize as a complex number and at least ten different examples of things you will not recognize as a complex number.

Rewrite your user interface class to take advantage of this method when parsing the command line arguments.

Problem 14:

Using the MandelbrotUtilities class from problem 5, write an applet that draws a graph of the absolute value of |z(n)| vs. n where z(n) is the nth iteration of the Mandelbrot equation for a fixed c. (You only need to draw the graph. You do not need to draw axes or legends or anything of that nature.) Let the y axis run from 0 to 2.

Use PARAM tags to specify the c and the number of iterations to track.

Handle all exceptions reasonably. (Printing an error message on System.out and exiting is not reasonable.) Assume one generation per horizontal pixel. Make this applet reasonably quick.

Make the applet available on a Web page. Include at least three instances of the applet on the page: one with a converging c, one with a diverging c, and one with a c that neither converges nor diverges.

Problem 15:

Write an applet that draws a scatter plot of successive positions of z(n) where z(n) is the nth iteration of the Mandelbrot equation for a fixed c. (You only need to draw the graph. You do not need to draw axes or legends or anything of that nature.) Let the y axis run from -2 to 2 and the x-axis from -2 to 2.

Use PARAM tags to specify c and the number of iterations to track.

Handle all exceptions reasonably. (Printing an error message on System.out and exiting is not reasonable.) Assume one generation per horizontal pixel. Make this applet reasonably quick.

Make the applet available on a Web page. Include at least three instances of the applet on the page: one with a converging c, one with a diverging c, and one with a c that nither converges nor diverges.

Problem 16:

To the applets in Problems 13 and 14, add the necessary user interface components to enable the user to set c and the number of generations.

Problem 17:

Use the necessary layout managers to arrange the applet.

Problem 18:

Write a Mandelbrot image producer. Use this to write an applet that draws the entire Mandelbrot set.

Problem 19:

Write a subclass of either Canvas or Component that draws the Mandelbrot set using a Mandelbrot image producer. Rewrite Problem 17 to use this class instead of drawing the graph directly.

Problem 20:

Add text fields that allow the user to set the x range and the y range.

Problem 21:

Make last class's applet a panel. Write an applet that uses the panel. Make the panel and the applet that contains it reasonably attractive by using a LayoutManager.

Then write a frame based stand-alone application that uses frames, each containing an instance of the above panel. Don't do anything that would prevent the program from being expanded to a multiple window application.

Hand in the application as a runnable jar archive on a 3.5in floppy disk. Please put the floppy in an envelope and tape the envelope to the print out of your source code and screen shots of the running program. Label the floppy disk and make sure the label is firmly affixed to the floppy and will not get caught in the grader's floppy disk drive.

Problem 22:

Add alert dialogs that warn the user when they're attempting to set an invalid value.

Problem 23:

Add File and Edit menus to the frame of last week's program. The File menu should contain New, Open, Close, Save, Save As, Page Setup, Print, and Exit menu items. The Edit Menu should contain Undo, Cut, Copy, Paste, and Clear menu items. All menu items should have the standard menu shortcuts. The New, Close, and Exit menu items should work. The rest should be disabled. Use menu separators and ... where customary.

Problem 24:

Make Save, Save As, and Open all work. You will need to design a file format for the data.

Problem 25:

Write a thread safe version of the complex number class.

Problem 26:

Use a thread pool with ten threads to calculate multiple points simultaneously.

Problem 27:

Make printing work.

Problem 28:

Make Copy and Paste work.

Final Exam

Expand this entire assignment so it works for arbitrary Julia sets.

Related questions


Java Course Notes | Cafe au Lait
Last Modified October 3, 2002
Copyright 1999-2002 Elliotte Rusty Harold
elharo@metalab.unc.edu