| CPSC 124 | Introduction to Programming | Spring 2008 |
In this lab, you'll continue writing object-oriented programs. For more information on classes and objects, check out Chapter 5 of the book (sections 5.1-5.4). If you don't have your book with you, the book can be found online at http://math.hws.edu/javanotes5/.
Create a lab09 directory in your cs124 directory to hold the files for this lab. Copy the files from /classes/s08/cs124/labs/lab09/ to your lab09 directory.
Here are the exercises for this week's lab, due next Thursday. Note: the third exercise is for extra credit only.
In the file Game.java (in the lab09 directory), you will find a class Game. This class is one part of a very simplistic game. In the game, there is a single player who can basically wander through various rooms. Both the player and the rooms are represented as objects. The Game class is the main class for running the program.
Here are some guidelines for this game. A player can be in only one room at a time. If the room has a connecting room then the player can move to that room. A room may or may not have connecting rooms in the north side, south side, east side, or the west side. Each player and room has a name ("marc" or "closet", respectively), and each room also has a description ("a dusty closet"). There is no end goal in this game. A player simply moves from room to room. For extra credit, you can create a game with a goal, which is described in the third exercise. The code for setting up the rooms, prompting the user, and moving the user between rooms is provided for you in the main() method in the Game class. You must complete the Player and Room classes (note: until you have finished writing these classes, Game.java will not compile).
In a file called Room.java, you should write the Room class. The Room class should contain a constructor for setting the name and description. The room should contain variables representing the name (a string), description (a string), room to the north (a room object), room to the south (a room object), room to the east (a room object), and room to the west (a room object). If there is no connecting room in some direction (e.g., north), then that object should be null. Otherwise, it should be set to the appropriate object. These variables should not be directly accessible from outside the class. There should be the following getter and setter methods for reading and writing these variables:
getName() getDescription() getNorthRoom() getSouthRoom() getEastRoom() getWestRoom() /** * These setter methods take as input a variable of type Room * and sets the respective room to that room. */ setNorthRoom(..) setSouthRoom(..) setEastRoom(..) setWestRoom(..)
Note: inside the Room class, you will need Room objects to point to the various connecting rooms: the north room, the south room, the east room, and the west room. It may at first seem confusing to be using a Room object inside the Room class, but this is a reasonable (and useful) thing to do. But be careful, you cannot create new Room objects within the Room constructor or in the initialization of any global variables within the Room class. This would lead to an infinite creation of Room objects (similar to an infinite loop).
The Player class should contain a constructor for setting the name and the current room. It should contain variables representing the name (a string) and the current room (a room object). These variables should not be directly accessible from outside the class. There should be the following getter and setter methods for reading and writing these variables:
getName() getCurrentRoom()
In addition, there should be the following methods for moving a player to a new room:
goNorth() goSouth() goWest() goEast()
These methods should move the player only if there is a connecting room (i.e., the connecting room is not null). Otherwise, it should not move the player. The methods should return a boolean indicating whether the player was moved or not. Note: to test if a room object is null, you can do the following:
if (room == null) {
// do something...
}
else {
// do something else...
}
When you're finished implementing Room and Player, you should run the program and make sure the program works correctly.
Finally, you should modify the main() method in the Game class and add some new rooms (at least three). These rooms must be connected in some way to the other rooms. In other words, it should be possible from one room to get to any other room (with perhaps multiple moves). Run the program again and move the player around these rooms, verifying the program works correctly. Comment the new code so I can clearly see what you have added.
Create a new file called ComplexTest.java, which we will use for this exercise involving complex numbers. In mathematics, a complex number is a number of the form a+bi where a and b are both real numbers and i is the imaginary number that represents the square root of -1 (i.e., i*i=-1). (Note: the term "imaginary" is a misnomer since complex numbers often arise in the real world.) a is called the real component of the complex number and b is called the imaginary component of the complex number. The complex numbers have many nice properties. For example, unlike the real numbers there is a solution to every polynomial algebraic equation. They also are useful in many fields including electrical engineering and quantum mechanics.
Just like real numbers, complex numbers can be added, subtracted, multiplied, and divided. Here is how each of these four operations works for two complex numbers a+bi and c+di (note: these can easily be worked out algebraically):
In this exercise, we are going to write a class called ComplexNumber for representing complex numbers and performing operations (+, -, *, /) on complex numbers. Don't worry if you do not deeply understand complex numbers, it won't be critical for this exercise. You will need to understand only how to represent a complex number and how to implement an add, subtract, multiply, and divide method using the formulas above. In particular, your class should include the following:
You should then write a class called ComplexTest, which contains just the main() method. In this method, you should write code for testing your ComplexNumber class. In particular, you should write the following test code:
Note: because of the way computers handle real numbers, your results may vary by a few decimal points.
You should start by copying the contents of Game.java to Game2.java. You should then designate some of the rooms as losing rooms and one room as the winning room. If the user moves into a losing room then they will lose the game (perhaps, because the room contains a ferocious dog!). If the user moves into the winning room then they win the game. It will work a lot like a text-based version of Minesweeper. You might, however, add a story line. For example, you could create a story line where the player is trying to escape from a mental hospital where he or she have been placed against their will.
Verify that your lab09 folder contains all of the files you created or modified for this lab, then copy your entire lab09 folder to the handin directory ~mcorliss/handin/cs124/username (where username is replaced with your username).