Course: Introduction to Computers & Programming (JAVA), Summer 2009
Instructor: Christopher Conway

Extra Credit Homework

Due: Sunday, 28 June 2009 at 11:59 PM

NOTE: NO WORK CAN BE ACCEPTED AFTER THIS DEADLINE.

This homework should be submitted via email to cconway (AT) cs (DOT) nyu (DOT) edu

In this assignment you will use arrays to explore a very simple model of computation.

A cellular automaton consists of a line of cells, each of which is either “alive” or “dead”. The automaton evolves in a series of steps we call “generations”. In each generation, every cell is updated according to a definite rule. The rule tells us the state of the cell in the next generation according to the current state of the cell and the state of its immediate left and right neighbors.

The cellular automaton you will be implementing has the following rule: if the cell is alive, it will be alive in the next generation unless both of its neighbors are also alive (it is “overcrowded”); if the cell and both of its neighbors are dead, the cell will be dead in the next generation; if the cell is dead and one or both of its neighbors are alive, it will be alive in the next generation (it is “reborn”).

The diagram below demonstrates each of the eight cases in the cellular automaton. The top row represents the state of the cell and its left and right neighbors in the current generation. The bottom row represents the state of the cell in the next generation. We use a # to represent a live cell and a . to represent a dead cell:

Current generation:
Next generation:
###
 . 
##.
 # 
#.#
 # 
#..
 # 
.##
 # 
.#.
 # 
..#
 # 
...
 . 

You will write a program simulating a cellular automaton with 47 cells. In your program, you should represent the line of cells as a boolean array—live cells will have the value true and dead cells will have the value false. The line of cells should be “circular”: the leftmost cell will have the rightmost cell as its left neighbor and the rightmost cell will have the leftmost cell as its right neighbor. Your will have to write five methods: firstGeneration, rule, nextGeneration, stringOfGeneration, and main.

Sample output:

.......................#....................... 0 
......................###...................... 1 
.....................##.##..................... 2 
....................#######.................... 3 
...................##.....##................... 4 
..................####...####.................. 5 
.................##..##.##..##................. 6 
................###############................ 7 
...............##.............##............... 8 
..............####...........####.............. 9 
.............##..##.........##..##............. 10 
............########.......########............ 11 
...........##......##.....##......##........... 12 
..........####....####...####....####.......... 13 
.........##..##..##..##.##..##..##..##......... 14 
........###############################........ 15 
.......##.............................##....... 16 
......####...........................####...... 17 
.....##..##.........................##..##..... 18 
....########.......................########.... 19 
...##......##.....................##......##... 20 
..####....####...................####....####.. 21 
.##..##..##..##.................##..##..##..##. 22 
################...............################ 23 
...............##.............##............... 24 
..............####...........####.............. 25 
.............##..##.........##..##............. 26 
............########.......########............ 27 
...........##......##.....##......##........... 28 
..........####....####...####....####.......... 29 
.........##..##..##..##.##..##..##..##......... 30 
........###############################........ 31 
.......##.............................##....... 32 
User quits.

Hints:

Name your program LastnameExtra.java and submit via email to cconway (AT) cs (DOT) nyu (DOT) edu.


Extra extra credit (5 points):

Allow the user to give the command line argument “--random”. If this argument is present, instead of initializing the row of cells to have one live cell, intialize each cell in the row with a random true/false value. You will have to change firstGeneration to have a second parameter (a boolean) indicating whether the cells should be initialized randomly.

Sample output:

#.##...#.#####.#..#.....#...#.#.#.####.##.##..# 0 
#####.####...#######...###.########..########## 1 
....###..##.##.....##.##.###......####......... 2 
...##.#########...########.##....##..##........ 3 
..#####.......##.##......#####..########....... 4 
.##...##.....#######....##...####......##...... 5 
####.####...##.....##..####.##..##....####..... 6 
#..###..##.####...######..#########..##..##...# 7 
####.#######..##.##....####.......##########.## 8 
...###.....#########..##..##.....##........###. 9 
..##.##...##.......##########...####......##.## 10 
########.####.....##........##.##..##....###### 11 
.......###..##...####......###########..##..... 12 
......##.######.##..##....##.........######.... 13 
.....#####....#########..####.......##....##... 14 
....##...##..##.......####..##.....####..####.. 15 
...####.########.....##..######...##..####..##. 16 
..##..###......##...######....##.######..###### 17 
#######.##....####.##....##..#####....####....# 18 
......#####..##..#####..######...##..##..##..## 19 
#....##...########...####....##.############### 20 
##..####.##......##.##..##..#####.............. 21 
#####..#####....#############...##............# 22 
....####...##..##...........##.####..........## 23 
#..##..##.########.........#####..##........### 24 
###########......##.......##...######......##.. 25 
#.........##....####.....####.##....##....##### 26 
##.......####..##..##...##..#####..####..##.... 27 
###.....##..##########.######...####..######..# 28 
..##...######........###....##.##..####....#### 29 
#####.##....##......##.##..#########..##..##..# 30 
....#####..####....#########.......############ 31 
#..##...####..##..##.......##.....##..........# 32 
User quits.

Extra extra extra credit (5 points):

Embed your automaton in an applet. You may use the following code template and HTML stub as the basis for your applet:

AutomatonApplet.java
applet.html

You will need to copy your firstGeneration, rule, nextGeneration, stringOfGeneration methods into the template. (The template assumes that firstGeneration takes a boolean parameter, so you will have to have completed the first extra credit in order to complete this one.) You will also need to define the init method for the applet. The init method should call startApplet (defined in AutomatonApplet).

Sample screenshot:


Style counts:

Your assignment should be submitted BY EMAIL to: cconway (AT) cs (DOT) nyu (DOT) edu.

Last modified: 23 Jun 2009