Вы находитесь на странице: 1из 3

Lab Solutions

4CCS1PRA
Programming Applications

Lab 4: Code Structure


Exercise 1
Here is the new main method:
public static void main(String[] args) {
Maze aMaze = new Maze();
Room r1 = new Room (1);
Room r2 = new Room (2);
Room r3 = new Room (3);
Room r4 = new Room (4);
DoorWall d = new DoorWall (r1, r2);
DoorWall d2 = new DoorWall (r3, r1);
DoorWall d3 = new DoorWall (r4, r3);
DoorWall d4 = new DoorWall (r4, r2);
aMaze.addRoom(r1);
aMaze.addRoom(r2);
aMaze.addRoom(r3);
aMaze.addRoom(r4);
r1.setSide(Room.NORTH,
r1.setSide(Room.EAST,
r1.setSide(Room.SOUTH,
r1.setSide(Room.WEST,

new Wall());
d);
d2);
new Wall());

r2.setSide(Room.NORTH,
r2.setSide(Room.EAST,
r2.setSide(Room.SOUTH,
r2.setSide(Room.WEST,

new Wall());
new Wall());
d4);
d);

r3.setSide(Room.NORTH,
r3.setSide(Room.EAST,
r3.setSide(Room.SOUTH,
r3.setSide(Room.WEST,

d2);
d3);
new Wall());
new Wall());

r4.setSide(Room.NORTH,
r4.setSide(Room.EAST,
r4.setSide(Room.SOUTH,
r4.setSide(Room.WEST,

d4);
new Wall());
new Wall());
d3);

aMaze.print();
}
The original lines have been given a grey background, so all lines with a white background are
either new or modified. All told, we have touched 17 lines of code for this change. An easy
mistake to make is to forget to update the walls of rooms 1 and 2 in correspondence to the
new doors introduced by rooms 3 and 4.

Exercise 2
This version of the code has reduced code replication by encapsulating the repeated code into
a parameterised method createWallsFor(). We would expect this to mean we need to write
fewer lines of code to make our change. We will still need to watch out to update the walls of
existing rooms appropriately.

Last changed: 08 January 2014

Here is the new main method:


public static void main(String[] args) {
Maze aMaze = new Maze();
Room r1 = new Room(1);
Room r2 = new Room(2);
Room r3 = new Room(3);
Room r4 = new Room(4);
DoorWall d = new DoorWall(r1, r2);
DoorWall d2 = new DoorWall (r3, r1);
DoorWall d3 = new DoorWall (r4, r3);
DoorWall d4 = new DoorWall (r4, r2);
aMaze.addRoom(r1);
aMaze.addRoom(r2);
aMaze.addRoom(r3);
aMaze.addRoom(r4);
setWallsFor(r1,
setWallsFor(r2,
setWallsFor(r3,
setWallsFor(r4,

new Wall(), d,
new Wall(), new Wall(),
d2,
d3,
d4,
new Wall(),

d2,
new Wall());
d4,
d);
new Wall(), new Wall());
new Wall(), d3);

aMaze.print();
}
Notice how it is shorter overall. Also, there are fewer changed or added lines (highlighted as
before): 11 lines had to be touched rather than the 17 from Exercise 1. Note that we still need
to make sure to remember to update the walls of existing rooms.

Exercise 3
This is even shorter still! Here is the new main method:
public static void main(String[] args) {
Maze aMaze = new Maze();
Room r1 = new Room (1);
Room r2 = new Room (2);
Room r3 = new Room (3);
Room r4 = new Room (4);
addDoor (r1, r2, Room.EAST);
addDoor (r3, r1, Room.NORTH);
addDoor (r4, r3, Room.WEST);
addDoor (r4, r2, Room.NORTH);
aMaze.addRoom(r1);
aMaze.addRoom(r2);
aMaze.addRoom(r3);
aMaze.addRoom(r4);
aMaze.print();
}
This time, we only had to add 7 lines of code. We did not have to change any existing code,
because updating corresponding walls is automatically taken care of by the addDoor method.
This should reduce the potential for making errors, especially as our maze grows larger.
Can you encapsulate even more repetition or implicit constraints into explicit methods?

Last changed: 08 January 2014

Exercise 4
Compare your notes from the previous three exercises: Which version was most easy to change?
Why?
We have largely discussed this above already. Each version is easier to modify and maintain
than the previous one. Two things have contributed to this:
1. The second version reduced code repetition by encapsulating it in a separate method,
using method parameters to allow for slight variations between the repeated code
snippets.
2. The third version further improved comprehensibility and maintainability by reorganising the code according to the concerns it addressed, ensuring clear responsibilities for
each method. This also resulted in explicit support for the previously hidden constraint, which further improved comprehensibility and maintainability of the code.

Last changed: 08 January 2014

Вам также может понравиться