|
Creating a Board object |
|
To create a Board object, we will use the same operator as we did for the Scanner class – the new operator. A Board object
requires us to specify the number of columns and rows within the
parentheses. The Java statement to
create a board with 5 columns and 3 rows would be: // you
need to put this statement inside a program for it to work Board
grid = new Board(5, 3); When this
statement executes, a window should appear on your screen.
The size of
a Board object is initially maximized and will fill up most of the
screen, but by resizing the window it will resize automatically. When dealing with a board, it’s important to
be able to refer to particular cells.
The Board class uses a coordinate system with the origin – coordinate
(0,0) – being
the cell in the top left-hand corner.
The cell in the bottom right-hand corner has (4,2)
for a coordinate. It’s easy to be tempted
to refer to it as cell (5,3), which will cause an
error when you run your program (a runtime error). As in mathematics, we will use the terms
x-axis and y-axis to refer to rows and columns, respectively. Further, a board with n columns and m rows
will be termed an nxm board. Let’s go
back briefly to the Java statement.
The variable grid is a reference to the Board object that
we’ve created, or simply an object reference. Using the object reference, we will invoke
methods on the Board object to access and manipulate its cells. |