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:
|
|
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 theCheckerboard
class:
- 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).
- 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:- public int getRows() returns number of rows in the checkerboard.
- public int getColumns() returns the number of columns in the checkerboard.
- public String getElement (int row, int col)
returns the string in row
row
and columncol
in the checkerboard. Both of these are 0-indexed, sogetElement(0,0)
returns the string in the upper left corner of the checkerboard. - 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); [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
- 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); @ + + + + @ + + + + + @ + + + + @ + + + + + @ + + + + @ + + + + + @ + + + + @ + + + + + @ + + + + @
- 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
- 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); ~ ~ @ @ @ ~ ~ @ @ @ ~ ~ @ @ @ ~ ~ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
- 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:
-
java Checkerboard sym
displays an 8x8 (default size) checkerboard using the symbol sym. -
java Checkerboard sym n
displays an nxn checkerboard using the symbol sym. -
java Checkerboard s m t n
displays an mxm board using the symbol s that has overlayed on top of it an nxn board using the symbol t. -
java Checkerboard
uses Checkerboard methods to create and display a checkerboard with the following pattern: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
- All other invocations of the
Checkerboard
program should display a polite error message informing the user that the invocation is incorrect and indicating what correct invocations look like.
> 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>