Lorimer Chapel in Winter
CS 231: Data Structures and Algorithms
(Fall 2013)

Syllabus

Teachers
Lanya
Tyler
Hieu
Bilal
Scott
Terrence
Charles
Matt


Assignments


Other Pages

Project 4:
Animating Simulations


Assigned: Mon Sep 30 2013
Due: 11:59:59 PM on Mon Oct 07 2013
Team Size: 1
Language: Java


Lab:

The goal of this lab period is to get you started on the current assignment, give you an introduction to an Abstract class and go over how to remove an item from a Linked List.

Documentation for Java 1.6 is located at: Java 1.6 SE API


Tasks

  1. Update your Linked List class to have a remove method. It should remove the Node containing the given object from the list. (If there is more than one instance of the object, it can either remove all of them or only the first one.) Put a test of your remove method in the main function of your LinkedList class.

    public void remove(T thing) - removes the Node containing thing from the list.

  2. How would you write the remove method for your LinkedListIterator? What information would need to be stored in the LinkedListIterator class?
  3. Create an abstract class Animal.

    public abstract class Animal

    It should have a field for the animal's name and accessors to get and set the value. Give the class an abstract method, speak, that returns a string representation of the sound the animal makes.

    public abstract String speak()

  4. Create several derived classes of the Animal parent class that each implement the speak method. In the derived classes, the speak method will not be abstract.
  5. Create a test class with a main function that creates an ArrayList<Animal>. Then add different types of animals to the list. Finally, go through the list and have each animal speak.

Assignment:

This week we're going to add a visual component to our simulations. We'll also re-organize the code to make use of an abstract class to represent the parent of different Cell types.

Setup

Download LandscapeDisplay.java. This file implements a simple window into which you can draw visual representations of cells.

The overall organization will be similar to project 3: a Simulation has a Landscape, a Landscape has a LinkedList of Cell Objects, and Cell objects are in charge of updating their own state. This week, however, the basic Cell class will become an abstract class, and each simulation will extend the Cell class in a different way. Using this organization, we'll recreate the simulations of the past two projects (including the Game of Life) but display them visually in a window so you can see the simulation occur in real time on a single window.

Tasks

  1. Copy over your Cell.java file from last week. Make Cell an abstract class and give it the following abstract functions.
    • public abstract boolean isNeighbor( Cell cell );
    • public abstract void updateState( Landscape scape );
    • public abstract void draw(Graphics g, int x, int y, int scale);

    You will need to import the java.awt.Graphics package.

    [Optionally, add a field to represent the type of the Cell and modify the constructor arguments to include an integer type and create accessors for the Type field. This field should receive a unique number from each child class.]

  2. Create a class ClumpingCell that extends Cell. It should have a constructor with two arguments (x and y location) that calls the parent constructor. The ClumpingCell class also needs to implement the three abstract functions.
    • public boolean isNeighbor( Cell cell ) - returns true if the argument cell is within some radius of this cell. Use something between 2.0 and 5.0 to start.
    • public void updateState( Landscape scape ) - updates the state of the ClumpingCell. This should be the same rule as the Cell class from project 3. Have the Cell move no more than +/- 3 to start. Note that this means the Cell's motion can be any continuous value between +3 and -3.
    • public void draw(Graphics g, int x0, int y0, int scale) - use the following definition to start. You are free to change it to other shapes and colors, as you wish.
          public void draw(Graphics g, int x0, int y0, int scale) {
              int x = x0 + (int)(this.getX() * scale);
              int y = y0 + (int)(this.getY() * scale);
      
              g.setColor(new Color(0.2f, 0.6f, 0.3f));
              g.fillRect(x, y, scale, scale);
      
              return;
          }
      
    • Optionally, add a toString method to the ClumpingCell class.
  3. Update your Landscape class.
    • Make your width and height fields be type double. Update your current constructor and also create a Constructor that takes two double arguments. Then modify your getRows and getColumns to return the height and width rounded to integers.
    • public double getHeight() - returns the height as a double.
    • public double getWidth() - returns the width as a double.
    • public ArrayList getNeighbors(Cell cell) - returns the list of neighbors of cell. The new getNeighbors function should make use of the isNeighbor method of a Cell. It should implement the following algorithm.
      Create an ArrayList of Cells to hold the neighbors
      
      For each Cell, other, in the agents list
        if other is a neighbor of cell
          add other to the list of neighbors
      
      return the list of neighbors
      
    • Update your main function so it adds ClumpingCell agents to the landscape. Run the simulation to test it.
  4. Compile and run the LandscapeDisplay class. Do you see the same behavior?
  5. Create a new Simulation.java class that follows the main function of LandscapeDisplay but uses the save method of the LandscapeDisplay class to save a series of images of the simulation. You will want to save the images with names so they appear in order when you type ls. I suggest using .png as the suffix of the filename. Then use the convert command (on the lab machines) to create an animated GIF of the simulation.

    $ convert -delay 60 *.png mysim.gif

    If you want to use convert on your own machine, you'll need to download ImageMagick. (One student found this ImageMagick installer for Mac.)

    Create a movie of 50 steps of the ClumpingCell simulation.

  6. Create a new child class of Cell called PreferenceCell. Use it to implement the same simulation as CategorizedCell from project 3, but use three categories. In your draw method, draw each category as a different color.
  7. What do you notice about the isNeighbor method of both your Cells? ...... That's right, they look the same! What can we do so this is only implemented in one place? ...... Good plan, let's move it to the abstract class! Change the Cell's isNeighbor so that it is no longer abstract and has the implementation. Then delete the method (completely) from the ClumpingCell and PreferenceCell. Nice job refactoring! :)
  8. Update your Simulation class so that you can control from the command line which type of Cell gets added to the Landscape and simulated.

    Create a movie of 50 steps of the PreferenceCell simulation.

  9. Create a new child class of Cell called LifeCell. Use it to implement the following update rules.
    • If a LifeCell has at least 3 neighbors, it is alive and refreshed, which means it has exactly three lives.
    • If a LifeCell has 5 or 6 neighbors, it can create a new LifeCell nearby, which should be added to the list of agents.
    • If the LifeCell has fewer than 3 neighbors, then it loses one of its lives. A LifeCell with zero lives should be removed from the list of agents.
    • A LifeCell that has between 3, 4, or 5 neighbors has a 1% chance of moving each round. A LifeCell with fewer than 3 or more 5 will move.

    You may want to remove cells outside the viewing window or your simulation will likely grind to a halt. You will also want to add a removeAgent method to your Landscape. The experiment is a bit more interesting if you make the chance of moving more like 10%.

  10. Update your Simulation to incorporate the LifeCell option.

    Create a movie of 50 steps of the simulation.


Extensions

  1. Make another type of Simulation. Can you implement the Game of Life in this organization? (Hint: you can, but you will need to be able to clone a Landscape.)
  2. Experiment with the rules for LifeCell. Can you make a simulation that seems stable?
  3. Make more interesting visualizations of your Cells.
  4. Use command line arguments to control more aspects of the simulation parameters.

Handin

Before handing in your code, double check that it is well-styled:

  • All variable names and function names use camelCase.
  • All class names are in PascalCase.
  • All public classes and methods use complete JavaDoc.
  • All private class members (fields and methods) have their own descriptive comment (but not JavaDoc).
  • Comments are added to explain complicated code blocks.
  • All variable and function names are appropriately descriptive.
  • All indenting and squigly-braces are properly placed.
  • Each concrete (non-abstract) class should have a unit test (main method) that calls and tests all methods in the class.

Make your writeup for the project a wiki page in your personal space. If you have questions about making a wiki page, stop by my office or ask in lab.

Your writeup should have a simple format.

  • A brief description of the overall task, in your own words.
  • As explanation of your solution, focusing on the interesting bits. The interesting bits here are how you modified the basic design and implemented different simulations.
  • Printouts, pictures, or results to show what you did. For this assignment, include your animated GIF movies.
  • Other results to demonstrate extensions you undertook.
  • A brief conclusion and description of what you learned.

Once you have written up your assignment, give the page the label:

cs231f13project4

You can give any page a label when you're editing it using the label field at the bottom of the page.

Do not put code on your writeup page or anywhere it can be publicly accessed. To hand in code, upload it to the Courses server.