Feb. 28
Examples from Feb. 28
On this day we only discussed one example: the methods we added to class Puzzle to decorate the grid.
The following methods were used from class Grid
public Grid(int rows, int cols) // the constructor
public void color(int row, int col, String newColor); // paints one box
public int firstRow() // the index of the first row of the grid
public int lastRow() // the index of hte last row of the grid
public int firstColumn() // the index of the first column of the grid
public int lastColumn() // the index of hte last column of the grid
The goal is to add methods to class Puzzle to decorate this grid:
public void paintRow(int row, String newColor)
public void paintCol(int col, String newColor)
public void paintX(String newColor)
public void paintTablecloth(String newColor)
public void paintCheckerboard(String newColor)
You need a number of files for this. Here are all of the files, including
the default version of
Puzzle.java that we later modified. Puzzle.java is the only one you need to
change:
Canvas.java Square.java
Grid.java Puzzle.java
Here is the version of Puzzle.java after we modified it in class. I have
given it a different name
(Puzzle.InClass.java) so it can be in the
same directory as the others. You will have to change its
name back to Puzzle.java to use it.
PrimeList.java: This is an example of loops. It prints a list of all prime numbers up to value N, which is defined in the constructor for the class.
Average.java and SimpleInput.java : This is a simple example of a program that uses David Barnes' SimpleInput class, which is described in our text. The program reads a list of integers from the user and prints their average. The list is terminated by value 0.
PrimeCheck.java and SimpleInput.java: This is another program that uses the SimpleInput class. This takes the isPrime() method from program PrimeList and uses it to check for primality values that are given by the user. Again, the input loop terminates when it gets value 0.