CS111 Spring 2014

Lab 12 Array Manipulations

Wed Apr 23 and Thur Apr 24

2D arrays: Creating a Checkerboard class of your own

Today we'll be creating from scratch a new class called Checkerboard that represents text checkerboards of various sizes. These checkerboards can be displayed on the console (standard output window). Here are two sample checkerboards:
#   #
  #   #
#   #
  #   #
sharps
4x4
checkerboard
*   *   *   *   
  *   *   *   * 
*   *   *   *   
  *   *   *   * 
*   *   *   *   
  *   *   *   * 
*   *   *   *   
  *   *   *   * 
stars
8x8
checkerboard
There are no files to download, since you'll be writing all the code from scratch. :-)

Start by defining the class Checkerboard in a new file named Checkerboard.java. Fill it in according to the following specifications.


Instance variable(s) of the Checkerboard class

In this class, you'll need an instance variable that is the representation of the checkerboard. This should be a regular 2D array of strings. For example, the sharps checkerboard above can be represented as a 2D array with 4 rows and 4 columns:

Here's an object diagram illustrating the structure of this 2D array. Note that blank cells of the checkerboard are represented by strings with a single space:

It's also a good idea (but not absolutely necessary) to have instances variables for the number of rows and number of columns in the checkerboard.

All of your instance variables should be private!


Constructor methods of the Checkerboard class

You will write two constructor methods for the Checkerboard class:
  1. The first constructor takes two inputs. The first is a string that fills the nonempty cells of the checkerboard, and the second is the side length of a square board. Here are two examples, in which the toString() instance method is used to display the contents of the checkerboard. In these examples, a space (" ") is printed after each item in the checkerboard and that allows for cleaner formatting.
    > Checkerboard money = new Checkerboard("$",4);
    > System.out.println(money); 
    $   $ 
      $   $
    $   $ 
      $   $
    
    > Checkerboard elle = new Checkerboard("L",8);
    > System.out.println(elle);  
    L   L   L   L   
      L   L   L   L 
    L   L   L   L   
      L   L   L   L 
    L   L   L   L   
      L   L   L   L 
    L   L   L   L   
      L   L   L   L 
    

    Note: the nonempty cells are those in which the sum of the row and column indices is even (where indices start at 0).

  2. The second constructor takes a single string input that fills the nonempty cells of the checkerboard and creates an 8x8 checkerboard with this string. For example:
    > Checkerboard stars = new Checkerboard("*"); 
    > System.out.println(stars); 
    *   *   *   *   
      *   *   *   * 
    *   *   *   *   
      *   *   *   * 
    *   *   *   *   
      *   *   *   * 
    *   *   *   *   
      *   *   *   * 
    

    Note: Rather than duplicating the code from the first constructor, you can instead invoke the first constructor from the second constructor by using this() as a constructor name!


Instance methods of the Checkerboard class

You'll need to define the following instance methods:
  1. public int getRows() returns number of rows in the checkerboard.
  2. public int getColumns() returns the number of columns in the checkerboard.
  3. public String getElement (int row, int col) returns the string in row row and column col in the checkerboard. Both of these are 0-indexed, so getElement(0,0) returns the string in the upper left corner of the checkerboard.
  4. public String toString() returns a String representation of the checkerboard. As illustrated in the above examples, the contents of each checkerboard cell should be followed by a space (" ") to separate it from the contents of the next cell. Here's an example in which the some cells contain a string whose length is greater than one:
    > Checkerboard brackets = new Checkerboard("[]",20);
    > System.out.println(brackets);
    []   []   []   []   []   []   []   []   []   []   
      []   []   []   []   []   []   []   []   []   [] 
    []   []   []   []   []   []   []   []   []   []   
      []   []   []   []   []   []   []   []   []   [] 
    []   []   []   []   []   []   []   []   []   []   
      []   []   []   []   []   []   []   []   []   [] 
    []   []   []   []   []   []   []   []   []   []   
      []   []   []   []   []   []   []   []   []   [] 
    []   []   []   []   []   []   []   []   []   []   
      []   []   []   []   []   []   []   []   []   [] 
    []   []   []   []   []   []   []   []   []   []   
      []   []   []   []   []   []   []   []   []   [] 
    []   []   []   []   []   []   []   []   []   []   
      []   []   []   []   []   []   []   []   []   [] 
    []   []   []   []   []   []   []   []   []   []   
      []   []   []   []   []   []   []   []   []   [] 
    []   []   []   []   []   []   []   []   []   []   
      []   []   []   []   []   []   []   []   []   [] 
    []   []   []   []   []   []   []   []   []   []   
      []   []   []   []   []   []   []   []   []   [] 
    
  5. public void insertDiagonal(String symbol) changes the receiver checkerboard by drawing a backward diagonal using the given symbol. Note that this effect is accomplished by changing the contents of some of the slots in the 2D array representing the checkerboard. For example:
    > Checkerboard plusses = new Checkerboard("+",10); 
    > System.out.println(plusses);
    +   +   +   +   +   
      +   +   +   +   +   
    +   +   +   +   +   
      +   +   +   +   +   
    +   +   +   +   +   
      +   +   +   +   +   
    +   +   +   +   +   
      +   +   +   +   +   
    +   +   +   +   +   
      +   +   +   +   +   
    
    > plusses.insertDiagonal("@");
    > System.out.println(plusses);
    @   +   +   +   +   
      @   +   +   +   +   
    +   @   +   +   +   
      +   @   +   +   +   
    +   +   @   +   +   
      +   +   @   +   +   
    +   +   +   @   +   
      +   +   +   @   +   
    +   +   +   +   @   
      +   +   +   +   @   
    
  6. public void doubleWidth() doubles the width of the Checkerboard by repeating the Checkerboard pattern twice. As with the method above, doubleWidth() actually changes the checkerboard object that invokes it. Here is an example:
    > Checkerboard bangs = new Checkerboard("!");
    > System.out.println(bangs);
    !   !   !   !
      !   !   !   !
    !   !   !   !
      !   !   !   !
    !   !   !   !
      !   !   !   !
    !   !   !   !
      !   !   !   !
    
    > bangs.insertDiagonal("&"); 
    > System.out.println(bangs); 
    &   !   !   !
      &   !   !   !
    !   &   !   !
      !   &   !   !
    !   !   &   !
      !   !   &   !
    !   !   !   &
      !   !   !   &
    
    > bangs.doubleWidth();
    > System.out.println(bangs);  // bangs is now 8x16
    &   !   !   !   &   !   !   !   
      &   !   !   !   &   !   !   !
    !   &   !   !   !   &   !   !
      !   &   !   !   !   &   !   !
    !   !   &   !   !   !   &   !
      !   !   &   !   !   !   &   !
    !   !   !   &   !   !   !   &
      !   !   !   &   !   !   !   &
    
    > bangs.getRows()
    8
    
    > bangs.getColumns()
    16
    
    > bangs.doubleWidth();
    > System.out.println(bangs);  // bangs is now 8x32
    &   !   !   !   &   !   !   !   &   !   !   !   &   !   !   !   
      &   !   !   !   &   !   !   !   &   !   !   !   &   !   !   !
    !   &   !   !   !   &   !   !   !   &   !   !   !   &   !   !
      !   &   !   !   !   &   !   !   !   &   !   !   !   &   !   !
    !   !   &   !   !   !   &   !   !   !   &   !   !   !   &   !
      !   !   &   !   !   !   &   !    !   !   &   !   !   !   &   !
    !   !   !   &   !   !   !   &    !   !   !   &   !   !   !   &
      !   !   !   &   !   !   !   &    !   !   !   &   !   !   !   &
    
    > bangs.getRows()
    8
    
    > bangs.getColumns()
    32
    
  7. public void overlay (Checkerboard other) changes the receiver checkerboard so that the parameter checkerboard other (which is assumed to be smaller) is overlaid on top of the receiver checkerboard, anchored in the upper left hand corner. For example:
    > Checkerboard ats = new Checkerboard("@", 10);
    > System.out.println(ats);
    @   @   @   @   @   
      @   @   @   @   @ 
    @   @   @   @   @   
      @   @   @   @   @ 
    @   @   @   @   @   
      @   @   @   @   @ 
    @   @   @   @   @   
      @   @   @   @   @ 
    @   @   @   @   @   
      @   @   @   @   @
    
    > Checkerboard tildes = new Checkerboard("~", 4);
    > System.out.println(tildes);
    ~   ~   
      ~   ~ 
    ~   ~   
      ~   ~ 
    
    > ats.overlay(tildes);
    > System.out.println(ats);
    ~   ~   @   @   @   
      ~   ~   @   @   @ 
    ~   ~   @   @   @   
      ~   ~   @   @   @ 
    @   @   @   @   @   
      @   @   @   @   @ 
    @   @   @   @   @   
      @   @   @   @   @ 
    @   @   @   @   @   
      @   @   @   @   @ 
    
  8. public void shiftDown() changes the receiver checkerboard by shifting each row down by one, except the last row, which becomes the first row. For example:
    > Checkerboard zeroes = new Checkerboard("0");
    > zeroes.insertDiagonal("1"); 
    > System.out.println(zeroes);
    1   0   0   0
      1   0   0   0
    0   1   0   0
      0   1   0   0
    0   0   1   0
      0   0   1   0
    0   0   0   1
      0   0   0   1
    
    > zeroes.shiftDown();
    > System.out.println(zeroes);
      0   0   0   1
    1   0   0   0
      1   0   0   0
    0   1   0   0
      0   1   0   0
    0   0   1   0
      0   0   1   0
    0   0   0   1
    
    > zeroes.shiftDown();
    > System.out.println(zeroes);
    0   0   0   1
      0   0   0   1
    1   0   0   0
      1   0   0   0
    0   1   0   0
      0   1   0   0
    0   0   1   0
      0   0   1   0
    
    > zeroes.shiftDown();
    > System.out.println(zeroes);
      0   0   1   0
    0   0   0   1
      0   0   0   1
    1   0   0   0
      1   0   0   0
    0   1   0   0
      0   1   0   0
    0   0   1   0
    
    > zeroes.insertDiagonal("2");
    > System.out.println(zeroes);
    2 0   0   1   0
    0 2 0   0   1
      0 2 0   0   1
    1   0 2 0   0
      1   0 2 0   0
    0   1   0 2 0
      0   1   0 2 0
    0   0   1   0 2
    

A main method for testing

Part I

As usual, create a main() method for creating instances of your Checkerboard class and testing out all of the instance methods that you wrote above.

After you have tested all your methods and are confident that they work correctly, comment out your testing code.

Part II

Now you're going to write your main method so that different command line arguments can be used.

How your Checkerboard program should work:

Here is some sample output:
> java Checkerboard *
*   *   *   *   
  *   *   *   * 
*   *   *   *   
  *   *   *   * 
*   *   *   *   
  *   *   *   * 
*   *   *   *   
  *   *   *   * 

> java Checkerboard $ 4
$   $   
  $   $ 
$   $   
  $   $ 

> java Checkerboard foo 6
foo   foo   foo   
  foo   foo   foo 
foo   foo   foo   
  foo   foo   foo 
foo   foo   foo   
  foo   foo   foo 

> java Checkerboard @ 10 ~ 4
~   ~   @   @   @   
  ~   ~   @   @   @ 
~   ~   @   @   @   
  ~   ~   @   @   @ 
@   @   @   @   @   
  @   @   @   @   @ 
@   @   @   @   @   
  @   @   @   @   @ 
@   @   @   @   @   
  @   @   @   @   @ 

> java Checkerboard
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 
8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 
7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 
6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 
5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 
4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 
3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 
2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 

> java Checkerboard @ ~ 4
Checkerboard does not handle the case of 3 arguments.
Please try one of the following:
  java Checkerboard
  java Checkerboard <symbol>
  java Checkerboard <symbol> <number>
  java Checkerboard <symbol> <number> <symbol> <number>