Reading Class Documentation and Writing Client Code


Object-oriented programs are made up of classes and objects. A class is like a blueprint for objects that you can create in the program. An object is an entity that has state (information associated with the object) and a set of operations (actions that the object can do or that can be done to the object). A program consists of objects interacting by invoking operations on each other. Some operations get information from another object, some change the state of an object, and others change the state of the outside environment. For example, in a program that simulates fish swimming in an aquarium, one operation might provide information about the location of a fish in the aquarium, another might cause a fish to move, and a third might change the way the aquarium is displayed to the user. In a program that does calculations based on data from a scientific instrument, there might be operations that look at the data, operations that archive old data and then remove them from the current set used for ongoing calculations, and operations that adjust the scientific instrument.

[Talk about the role and contents of a class vs. the role and contents of client code.]



Populating the Aquarium

Introduction

Exercise

  • Read the initial version of the main method in AquaSimApplication.java. This method has three sections. The first constructs the objects needed to run the simulation. The second actually runs it (or will, when we have added some more functionality to it). The third section wraps up the program, reminding the user how to quit.

  • Research the abbreviated description of (or specification for) the AquaFish class to discover how to construct a fish. Edit the main method in AquaSimApplication.java to declare and construct three fish variables at the end of the first section. Give them Intention Revealing Names. Using blank lines, separate this sequence of new statements, which together perform a single function, from the existing code around them. Add a single comment preceding them that describes the purpose of the sequence.

  • Test your modified program. Does it look any different? You constructed three fish, but the userInterface is only drawing the aquarium, not the fish.



Let's Move!

Introduction

Now the aquarium has three fish, but we can't see them. And even if we could see them, it wouldn't be very interesting because they aren't doing anything. We need to Read the [AquaSimGUI] Interface for Methods to learn how to view fish objects and Read the [AquaFish] Interface for Methods to learn how to get our fish to do something interesting (like move).

Exercise

  • Research the abbreviated specification for the AquaSimGUI class to discover how to view a fish. Modify the main method to display the three fish you constructed. You want to draw the fish after drawing the aquarium (or else the blue water in the aquarium will hide the fish), but before repainting the user interface. Test your modified program.
  • Now it is time to start running the aquarium simulation. Research the abbreviated specification for the AquaFish class to discover how to make a fish move forward. Add statements to the main method to move your three fish one step forward. (Where should these statements be added?) Using blank lines, separate this sequence of new statements, which together perform a single function, from the existing code around them. Add a single comment preceding them that describes the purpose of the sequence. Then add code (and comments) to redraw the fish.
  • Test your modified program. If you don't see the fish move, you probably forgot to repaint the user interface to make the changes visible. You may also see two copies (or one "blurred" copy) of each fish -- the fish in its old location and in its new location. If you see this, you forgot to redraw the aquarium to "erase" (or cover up) all the fish in their old locations before displaying the fish in their new locations. Add a line to the beginning of the second block of display statements to redraw the aquarium. You may even occasionally only see two fish (or even one!). This can happen when a larger fish overlaps and hides a smaller fish. Run your program a number of times to verify that you usually see three fish.
  • Now when you run your program you should no longer have two copies of each fish, but you probably can't really see the fish move because it happens too quickly to see. Research the abbreviated specification for the AquaSimGUI class to discover how to ask the user interface to pause after each set of display statements so that you can see the fish before and after they move.
  • Update the program documentation at the top of the file to reflect your modifications.