
ScreenSaverApp and write the methods described below. Make sure to test each method carefully before moving on to the next one.
Use nested loops in the methods you write.
int[][] myImage = ... ; canvas.drawImage( center-x-of-image, center-y-of-image, myImage );
mysteryScreenSaver( width, height, startCol )
This method returns a 2D array of integers of the given dimensions. The cells of the 2D array are set to the following values:
startCol of the top/first row is set to 1 (the rest of the cells on the top row remain set to 0, so only the given cell is changed in the top row)
int[][] image = mysteryScreenSaver(7, 7, 3);
printTable( image ); // displays
// 0 0 0 1 0 0 0
// 0 0 1 0 1 0 0
// 0 1 0 0 0 1 0
// 0 0 1 0 1 0 0
// 0 1 0 0 0 1 0
// 0 0 1 0 1 0 0
// 0 1 0 0 0 1 0
int[][] image = mysteryScreenSaver(201, 401, 100);
canvas.drawImage( 150, 250, image );
showMysteryScreenSaver( width, height, n )
This method shows an animation of n full cycles of the mystery screen saver centered in the middle of the screen.
A full cycle consists of generating the mystery screen saver with all possible columns on the first row one at a time (i.e. one image when cell 0 on top row is set, another image when cell 1 on top row is set, and so on, until the last cell in top row is set).
The process is then repeated. This should give the illusion of the image sliding across the screen from the left end of the screen to the right end of the screen and then starting to slide again from the left.
Use 2 nested loops (one for the cycles and one for the choice of columns for each cycle).
Make sure to pause briefly after each drawing and to clear the canvas.
We will use this game to generate screen saver patterns. The game is played on a rectangular board whose cells are either dead or alive (represented by the integers 0 and 1, respectively). The state of the board is updated according to the following rules:
1. for each cell in the table:
2. count the number of live neighbors of the cell
3. update the current cell as follows:
- alive cell with 2 or 3 live neighbors remains alive
- any other alive cell dies since it is lonely or overcrowded
- dead cell with exactly 3 neighbors become alive
Write the following methods:
countNeighbors( world, row, col )
This method takes a 2D array of integers representing the world and the coordinates of a cell and returns how many of the 8 neighbors around the given cell are alive.
You may assume that the given coordinates are valid (ensured in the next method, since first/last row/column are not processed).
Use a pair of loops to go through the 9 squares defined by the cell, making sure to not include the cell in the overall count. One option is to subtract its value at the end.
For example:
int[][] world = {
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0} };
int alive = countNeighbors(world, 2, 1);
System.out.println("live neighbors for cell [2][1] = " + alive); // cell [2][1] has only 1 live neighbors
alive = countNeighbors(world, 2, 2);
System.out.println("live neighbors for cell [2][2] = " + alive); // cell [2][2] has 2 live neighbors
evolveWorld( world )
This method takes a 2D array of integers representing a world of live or dead cells (only 0s and 1s will be in the table) and returns a brand new 2D array that represents the new state of the world based on the rules above.
Set up the loops, so that the first and last row and the first and last column are not processed, i.e. those cells will be dead in the next world.
For example:
int[][] world = {
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0} };
world = evolveWorld(world); // evolve the world and replace it with new state
printTable(world); // displays 0 0 0 0 0 0 0
0 0 1 1 1 0 0
0 0 1 1 1 0 0
0 0 1 1 1 0 0
0 0 0 0 0 0 0
world = evolveWorld(world); // evolve the world again
printTable(world); // displays 0 0 0 0 0 0 0
0 0 1 0 1 0 0
0 1 0 0 0 1 0
0 0 1 0 1 0 0
0 0 0 0 0 0 0
generateWorld( width, height, population )
This method generates a 2D array of the given dimensions. All cells in the 2D array are set to 0 (done by default, so there is nothing for you to do), except for a single horizontal segment of the given population number of live cells in the middle of the 2D array.
For example:
int[][] world = generateWorld( 7, 5, 3 );
printTable(world); // displays 0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 1 1 1 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
int[][] world = generateWorld( 7, 5, 5 );
printTable(world); // displays 0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 1 1 1 1 1 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
showLifeScreenSaver( width, height, population, n )
This method shows a screen saver based on the Game of Life. It first generates a 2D array of the given dimensions of integers representing the initial state of the world. Then it repeatedly changes the world by evolving it for the given number of cycles n.
For each evolution step the world is drawn in the middle of the canvas (pause briefly, e.g. 0.1 seconds, and clear the canvas before the next evolution of the world).
Here is how to compare two strings:String choice = canvas.readSelection( some-prompt, the, list, of, string, choices ); int value = canvas.readSelection( some-prompt, the, list, of, int, choices ); double value = canvas.readSelection( some-prompt, the, list, of, double, choices );
Here is how to convertstring1.equals( string2 )
double to int:
Write the following method:int width = (int) canvas.getWidth();
showScreenSaver()
This method shows a screen saver and works as follows:
"Mystery", "Game of Life""Mystery" selection show the animated mystery screen saver with dimensions equal to the phone dimensions and centered on the screen; you may use a fixed number for the number of cycles"Game of Life" selection ask the user to enter a number for the initial population size and then show an animated screen saver based on the Game of Life; the dimensions of the screen saver are equal to the dimensions of the phone; you may use a fixed number of your choice for the number of cycles