CS 051 | Fall 2011 |
We haven't officially covered inheritance yet, but the idea is straight forward. One class can
inherit characteristics (i.e., methods, instance variables, etc.) from another class by using
the extends keyword. (We'll see more about inheritance very soon.)
We used inheritance to write very clean, small versions of our laundry items. An implementation
class implemented methods that are common to all of our laundry items, such as move,
contains, and setColor. Then, our specific laundry item classes, like
pants and Tshirt need only define what DrawableInterface items are needed
to create the item.
The implementation class used an array to hold the correct number of drawing objects. The
common methods simply walk the array to do their work. For example, The move method
walks the array, and calls the move method of each item in the array.
See class demo ArrayLaundry
So, can we create an array of arrays? The answer is yes. As an example, we'll look at the syntax
to declare and create a 2-dimensional array.
     int [][] a = new int[2][3];
We can think of a 2-dimensional array as a table, where the cells in the table can be referenced
for our above example as follows:
               column
         0       1       2
row 0 a[0][0] a[0][1] a[0][2]
    1 a[1][0] a[1][1] a[1][2]
Notice that the first number in our instantiation represents the number of rows, and the
second number represents the number of columns in the array.
To demonstrate 2-dimensional arrays, we looked at the class example
Tic-Tac-Toe and started
to examine another example
MagicSquares.