CSCI 120 Lab 3


The purpose of this lab is to write and test a class definition given a complete specification of the class, in terms of the method headers and what they are supposed to do. The particular class we will write is quite simple (as simple as, in fact almost identical to, the Counter class at first). The main things to remember in implementing a class definition are very similar to what you need to remember when developing every program (to take small steps, and not to proceed until the previous step is working). Here this process translates to the following: "Tested" means that you really have invoked the method, either via a graphical object in BlueJ or by running a program that invokes the method. And you can't do that unless the program compiles. So this process entails many edit/re-compile cycles. It may seem silly to do some of these re-compilations, but trust me...you need to do them all, not just in this lab but also in "real life" programming! This will all go smoothly (and quickly!) if you read every step carefully below, and do one step at a time. Do not proceed to a higher numbered exercise until you have completed all the lower ones.

So let's get to work. The class is named Elevator, and an Elevator object represents... well, an elevator. The methods model the fact that an elevator can go up by a given number of floors, and that it can go down by a given number of floors. We will name the corresponding methods goUp() and goDown(). We also will have a method for finding out what floor we are on ("getFloor()") and a toString() method to represent the current floor in a "user-friendly" way (more details below). There will also be a "driver program" to test the Elevator, in a class called Operator. The Operator class just has a main method that creates an Elevator object and performs some of the basic operations on it.

Activities

  1. Use BlueJ to create a project for this lab, and name it lab3. All files will be stored in this directory.
  2. Download the files Elevator.java and Operator.java into your lab3 folder. Add them to the project as usual. Take a careful look at the code in Elevator.java. The comments show where code needs to be added and/or modified.
  3. Compile Operator.java and run it. Note that since it depends on Elevator.java, both files are compiled.
  4. The answers aren't that interesting, because right now all the methods in Elevator are empty! We need to start filling in the class definition.
  5. Start with the instance variable. Clearly we need one, for the current floor we are on. What type should it be? What visibility modifier should we use for it? (That last one is intended to be a loaded question.)
  6. Add the declaration for the instance variable to the file Elevator.java.
  7. Compile the new file Elevator.java. If there are syntax errors, fix them. DO NOT proceed to the next step until this is done (and similarly for all similar steps below).
  8. Compile and run Operator.java again. Nothing interesting again, since although we've added an instance variable, the Elevator methods are still empty!
  9. The first method we write in Elevator will, alas, still have no visible effect on the running of the program, but we have to do it. It is the constructor. This is the method with the same name as the name of the class, and with no return type. The code in the constructor should simply set the instance variable to a reasonable initial value. The initial value of the floor we are on should be 1. Write the assignment statement for this in the constructor.
  10. Compile both files and run Operator. If there are syntax mistakes, fix them. When you are done, everything should still run as at the beginning.
  11. Let's finally do something that will make a difference: In order to see if our Elevator is behaving properly, we need to fill in the accessor method getFloor(). The method simply returns the value of the instance variable that contains the current floor. Make that change to the return statement in getFloor().
  12. Compile, check for syntax errors and fix them.
  13. Use BlueJ to create a graphical Elevator object. That is, right click on the Elevator box and invoke the constructor. When the object has been created, right click on the object to invoke the getFloor() method. BlueJ will pop up a window displaying the returned value. You can also double-click on the object to look at its internal state, but that's not too interesting at this point since there are no methods to change the state.
  14. Now run Operator. Can you explain the resulting output?
  15. Write the goUp() method. For now, write it so that it simply increments the current floor by the amount given in the parameter (which is named floors in the code you are given). Just put in a single assignment that does the increment.
  16. Compile. Test the goUp() method exactly the way you tested the getFloor() in step 13, by creating a graphical object in BlueJ and invoking the method by right-clicking on it. Note that in this case you have to provide a parameter. BlueJ will ask for it in a window. Put in a value such as 4 or 5 and observe how the state of the object changes by inspecting it.
  17. Run Operator again. If you have written the method correctly, you should get a sensible output, at least for going up.
  18. Repeat steps 15, 16 and 17, but now for the goDown() method, and replacing the word "increment" with "decrement".
  19. One method is still empty, namely the toString() method, so let's write it. We would like the toString() method to work as follows. Let's say, for example, that the elevator is on level 3. Then the toString() method should return the string,
    "This is level 3! Watch your step getting out!".
    There is more than one way to do this. You could put an appropriate string expression in the return statement, or you could define a local String variable (named announcment perhaps?) that is assigned that expression, and then returned. Either way, it's a fairly simple matter of concatenating a couple of strings with the instance variable that contains the current floor. Give it a try.
  20. Compile, fix any possible syntax errors, and run. The final output statement in the program should at last give the right sort of output.

Congratulations! You have just coded and tested your first Java class.

Coming attractions: Now, no doubt you may have realized that there are problems with our code, since a client can send an Elevator "through the roof" by plugging in any int value for floors in the goUp() method. For example, you could plug in the value 1000000. But how many buildings do you know of with a million floors? Our building ought to have a finite number of floors, which we should represent by an additional instance variable. Similarly, it is possible to "descend past the basement," by going down a negative amount. The only way to fix this is with if-statements; we will study them at the beginning of Chapter 5, so we'll fix this problem in a future lab. All the more reason to be sure to save your work in this lab!

When you are done, print out both files, submit your lab3 folder, and demonstrate the program to the instructor.



Back to CS 120 Home Page