Java Tutorial Lesson 07: Matrices

From Erlands Wiki

Jump to: navigation, search

You have this class:

public class Room {
    private int roomId;
    private static final int ROOM_WIDTH=10;
    private static final int ROOM_HEIGHT=5;
    public Room(int roomId) {
        this.roomId = roomId;
    }
    public char[][] getData() {
        char [][] data = new char[ROOM_HEIGHT][ROOM_WIDTH];
        data[0] = new String("----------").toCharArray();
        data[1] = new String("|        |").toCharArray();
        data[2] = new String("|    "+roomId+"   |").toCharArray();
        data[3] = new String("|        |").toCharArray();
        data[4] = new String("----------").toCharArray();
        return data;
    }
}

The getData method will return a matrix with chars which is the layout of the room. Your job is to create a “Game” which uses the getData method so it can be called as follows:

java Game 3

And the output shall be:

Lesson 7
 
----------
|        |
|    1   |
|        |
----------
----------
|        |
|    2   |
|        |
----------
----------
|        |
|    3   |
|        |
----------

The parameter “3″ in the sample above specifies how many rooms that shall be created and printed.

Some optional things to try:

  1. Try to change the Game class so it prints the room horizontally instead of vertically
  2. Try to change the Room class so it can take integers with more than one digit and still print the room so the walls look nice


Personal tools