CS220 Mandelbrot Assignment 3.6

Mandelbrot visualizer Assignment 3.6

In its mathematical description, the Mandelbrot set is defined in terms of convergence and divergence — to decide whether c is in the set depends on whether the sequence |fci(0)| is bounded over all i. But on a computer, we cannot check for all values of i. Instead, we pick a large boundary N, and check whether |fci(0)| exceeds N within some large number of iterations K.

First, we should modify the Function1Iterator to have a second constructor which takes a third argument maxIterations:

public Function1Iterator(final Function1<X> f,
                         final X initial,
                         final int maxIterations) {
  // ...

A Function1Iterator object instantiated with this constructor should provide values for only the first maxIterations calls to next(). After maxIterations calls to next(), hasNext() should return false, and subsequent calls to next() should throw a NoSuchElementException (which is a standard Java exception in java.util).

Extend your test suite in Function1Iterator for the new behavior, keeping the old tests to make sure the other constructor continues to operate properly.

Second, complete the abstract class

/** Counts the number of times the result of an iterator's =next=
 *  method satisfies the method testItem.
 */
public abstract class IterationCounter<X> {
  /** Initialize the counter for a particular way of building Iterators
   *
   * @param factory A factory which produces an Iterator given some
   * seed value.
   * @param maximum The maximum number of calls to next() made on
   * behalf of one call to countPassing().
   */
  public IterationCounter(final Factory<Iterator<X>, X> factory,
                          final int maximum) {
    // ...
  }
  /** Template method which expresses the condition which each
   * result of next() might satisfy.
   */
  public abstract boolean testItem(X item);
  /** For an Iterator received from the factory for the given value
   * seed, count the number of times its next() method returns a value
   * which satisfies testItem().
   */
  public int countPassingIterations(X seed) {
    // ...
  }
}

This class uses the Template Method pattern which we discussed in lecture earlier this semester: the countPassingIterations method sets higher-level behavior, for which testIteration provides functionality for a particular scenario. We also use the Factory Method pattern. For this project the interface Factory is

public interface Factory<Product, Ingredient> {
  public Product build(Ingredient item);
}

Write tests for your class in a source file IterationCounterTests.java, and your completed implementation in IterationCounter.java.

Third, create a class MandelbrotIteratorFactory which implements Factory<Iterator<Complex>,Complex>. MandelbrotIteratorFactory should be monomorphic: it must not take type arguments. For a Complex instance c, and int value maximumIterations, the result of

new MandelbrotIteratorFactory(maximumIterations).build(c)

should return an iterator for the values of fci(0) for values of i from 0 to maximumIterations (inclusively, for a total of 1+maximumIterations results). Test your implementation using a class MandelbrotIteratorFactoryTests.

Fourth and finally, implement a class MandelbrotIterationCounter which extends IterationCounter for use in our Mandelbrot set visualizer. MandelbrotIterationCounter should also be monomorphic. Its constructor should begin:

public MandelbrotIterationCounter(final double divergenceMagnitude,
                                  final int maximum)

where divergenceMagnitude is a (usually large) constant which we use to approximate divergence. Note that reaching the divergenceMagnitude, not just exceeding it, should indicate divergence. Use a MandelbrotIteratorFactory to create the first argument to the call to MandelbrotIterationCounter‘s superclass constructor call. Test your implementation using a class MandelbrotIterationCounterTests.

Submission instructions. Your classes should be submitted as a ZIP file of eight plain-text Java source files contained in a directory src to the AutoLab submission point for this assignment. This assignment is due to the AutoLab server by 8:30am on November 6. You should not submit classes/interfaces Factory, Complex, Function1 or MandelbrotBase; reference copies will be provided by the autograder. Make sure that your code and tests use only the specified methods for these classes; if you rely on your own additional methods, your code may fail to compile under test.

Avoid plagiarism.

See also: Assignment 3.x general information, CS220 homepage