Abcol Logo

You have now reached Chrissie's web pages for

Java Programming

Javopoly - Tutorial 2. Working with Arrays.

The second tutorial deals with Array structures, which you should have already encountered in Pascal or Visual Basic. I will start this tutorial by recapping the benefits (yes, there are some), of using an array.

Array structures are very useful where you have several items of data which are of the same type, and have the same behaviour. Simple integers, reals or even Records in Pascal are often held in arrays. In Java, however, you can also hold similar objects in an array.

If we look at Player in the first version of the Javopoly game, you will notice that we created four similar objects -

    Player player0 = new Player();
    Player player1 = new Player();
    Player player2 = new Player();
    Player player3 = new Player();

As these are all of the same data type, have the same attributes and methods, and behave in the same way, we can put them in a Player array. This makes our resulting code a lot neater and more efficient. Instead of typing the same code four times, we only have to write it once and use it in conjunction with a fixed iteration loop.

An array in Java is also an object in it's own right. Setting up objects in an array in Java is therefore a two-step process-

Player [] players = new Player [4];

This sets up the array to hold the Player objects which will be created later. Like other languages, the array size must be declared when it is created (hence the [4]). This size is statically bound - i.e. it cannot subsequently be changed. (If you want to use a dynamically shrinking/growing array, use a Vector.) Note that this line of code sets up the array only - not the individual Player objects that we are going to put in the array. You do this like so-

for (int i = 0; i < players.length; i++);
{
 players[i] = new Player();
}

Note the players.length in the for loop. This is a useful device when dealing with array structures in Java. You can use the .length to return the length of any array. It kind of acts like a constant - if you want to redefine the length of an array in a new version, you just have to change the number once, which saves you messing about with code all the way down!

Back to TOP

I am now going to turn my attention to the Board. Our existing Board class is not nearly detailed enough for our requirements, so I am going to refine it further.

We can tell that the board is composed of twelve Square objects. These objects have minor variations, but the two things they all do have is common are-

Some of them also have purchase prices, rents payable etc., but we will deal with these in a later tutorial. In the meantime, I am going to define a very simple Square, and set up an array of 12 of them, similar to the players array.

Playing Board with 12 squares

Because the square objects are part of the board, we are describing a whole-part or composition relationship. The UML diagram for this is shown below -

When instantiating the square objects, we don't do this in the TestDriver class, as they are not part of it - remember they are part of the board object. So we instantiate both the array and the square objects in the Board class.

public class Board
{
 private Square[] squares = new Square[12];
 //array of Square objects to make up the board

 //and so on...

If you try to reference squares[i] from the top level class, the compiler will complain at you, because the TestDriver does not know what squares[i] means - that is solely contained in Board, and can only be accessed from there. This demonstrates one of the main principles of programming with Object Technology - encapsulation.

The new Board, Square and TestDriver classes are contained in this zip file.

Tutorial 3 will show you how to further refine Square, and move the players round the board. Or to go back to Tutorial 1, click here.

Architecture Home Pascal Home Web Design Home Other Stuff Home Other Stuff Home

Comments? c.nyssen@abcol.ac.uk
Last Updated - 17/11/03

Best viewed at 800x600 in 16-bit high color with Trebuchet MS installed. If you do not have this font, click here