Preparation
Do the following before arriving at lab:
- Read chapter 7.1 - 7.7
- Complete the homework asignment to design your own game scenario and map
- Bring your textbook to lab
Objectives
After completing this lab, you should be able to:
- refactor code to improve coupling and cohesion
- use a HashMap
Exercises
Chapter 7 is an extended case study to improve a working application that is poorly designed. Follow the exercises carefully step by step to discover a better design.
- Open project Zuul-bad in chapter 7.
- Exercise 7.1 - Explore the Zuul application. Briefly read the source code for each class.
- Exercise 7.5 - Implement and use a new printLocationInfo( ) method in the Game class as outlined in section 7.4. Test your changes before moving forward.
- Exercise 7.6 - Make several changes to the Room class as described in section 7.6. Make the instance variables private. Does the Game class still compile? If not, complete the next exercise and try again
- Add a getExit (String) method as shown in code 7.4. Make the necessary changes to the Game class. Do both classes compile now?
- Exercise 7.7 - Create a new getExitString() method in the Room class that returns a String containing all valid exits. Replace the necessary code in Game's goRoom() method.
Now that Game and Room are loosely coupled, you can make the following changes to Room with minimal changes to Game.
- Replace the four Room variables with a HashMap as described in section 7.6.1.
- Create a setExit(String, Room) method as described in section 7.6.1. Make the necessary changes to the Game class. Do both classes still compile? Does the application still work?
- Remove the existing setExits(Room, Room, Room, Room) from the Game class in createRooms( ).
- Exercise 7.9 - Look up the keySet method in the documentation of HashMap. What does it do?
- Improve the getExitString() method in the Room class that returns a String containing all valid exits. The improved method will retrun the contents of the HashMap (see Code 7.7 in the book).
The results so far are an improved design with a better separation between Room and Game. Room can now add new directions of any kind without any changes to the code!
- Update the Game createRooms() method to reflect your own game map.
Grading Criteria
This lab is worth a possible 10 points. Turn in a printout of the Game and Room source code.
- methods (8 pts)
- coding style (2 pts)
|