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`.

• `firstGeneration` takes an integer parameter `n` and creates a boolean array representing a line of `n` cells, initializing it with exactly one live cell in the center. It returns the array.
• `rule` takes three `boolean` parameters: the value of a cell, the value of the cell's left neighbor and the value of the cell's right neighbor. It returns a `boolean` value indicating whether the cell will be alive in the next generation.
• `nextGeneration` takes a boolean array representing a line of cells and creates a new boolean array of the same size representing the next generation of the automaton. The values of the elements in the new array are determined by passing values from the input array to `rule`.
• `stringOfGeneration` takes a boolean array representing a line of cells and returns a `String` indicating the current state of the automaton. Each cell should be represented by an `'#'` if it is alive and a `'.'` if it is dead.
• `main` creates a boolean array by calling `firstGeneration`. It then enters a loop: it displays the current generation of the automaton (using the result of `stringOfGeneration`), including the number of generations that have elapsed since the beginning of the program; it displays a dialog box asking the user whether she would like to continue or quit; if the user chooses to continue, it calls `nextGeneration` to compute the next state of the automaton and repeats; if the user chooses to quit, the loop terminates.

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:

• `nextGeneration` should not assume the array will have exactly 47 elements. Use the `length` property of the array in your implementation, so that the method would work properly with an array of any size.
• You can use the following method call to ask the user whether to continue:
```public static boolean userWantsToContinue() {
int option = JOptionPane.showConfirmDialog(null,    // parent window
"Would you like to continue?", // window message
"Automaton application",       // window title
JOptionPane.YES_NO_OPTION);    // option type
return (option == JOptionPane.YES_OPTION);
}
```
This will display a Yes/No option window and return `true` if the user clicks Yes.

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:

• You should use comments throughout the source code to explain key steps and calculations
• Choose intuitive names for variables
• You should use proper indentation (2-4 spaces) and blank lines to make your program easy to read.
• Do not mix tab and space characters in your indentation—configure your editor to replace tabs with spaces. (Instructions here.)
• Do not exceed 80 characters per line.

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

• Name your .java file `LastnameExtra.java`. For example: `ConwayExtra.java`.
• Attach your .java file—and only your .java file—to an email with the subject line "Extra Credit Homework".
• Each .java file you submit should begin with a comment of the form:
```/**********************************
* [NAME OF YOUR PROGRAM AND A SHORT DESCRIPTION OF ITS PURPOSE]