![]() | |
creation: 8/13/99; edited: 05/05/03 Fill in the BlanksInstructions: This is an ungraded fill-in-the-blank exercise. Each question consists of a sentence with one or two words left out. A button represents the missing word(s). For each question, think of the word or phrase that should fill each blank, then click on the buttons to see if you are correct. No grade is calculated for this exercise. Often students get stuck in writing a program because there seem to be too many ways to do something and it is not clear which one is correct. This exercise several ways in which a program could be written to accomplish the same task. The programs are increasingly complicated, and probably increasingly bad program designs for the problem as stated. 1. Fill in the blanks so that the following program writes "Hello Vesta" six times to the monitor. This program shows little of the object oriented style of programming. For the task at hand it is probably the best of all ten programs since it accomplishes the task in a simple and clear manner. But let us look at some other programs. 2. Now write the program so that it makes use of a Vesta object that writes "Hello Vesta" to the monitor.
In this program one Vesta object is created,
whose
3. Here is a modification of the program.
Now six Vesta objects are created, and
each one uses its This version of the program is less efficient than the previous program because this version creates six objects all of which do the same thing, and each of which immediately becomes garbage after it is used. This is a poor programming style. 4. Here is a further modification of the program. Again six Vesta objects are created, but now each one is a temporary, unnamed object.
The 5. Now the Vesta class is modified so a Vesta object writes out its message six times:
This version has the advantage that only one object is created, and has the
further advantage that 6. In this version the class definition includes a constructor that initializes the message to be printed:
Again, whether the 7. Further sophistication is to include the iteration limit in the constructor.
This version of the
8. On further thought (which should have been done before
any coding was done) it seems more sensible that the number of times to write
the message is better specified as a parameter to the This version seem more logical, as well as being more versatile. The number of times a message should be repeated should be a characteristic of the request, not an inherent characteristic of the object.
9. Mostly a Vesta object says "Hello Vesta" each time it is used.
If that is what we want, the
10. A further improvement to the Notice carefully the difference between a constructor and a method: a constructor initializes data in a object when the object is being created. Every use of a constructor creates a new object. A method is part of an object. It changes the data of its object. Because the data in Vesta objects can be changed, they are NOT immutable. End of the Exercise. If you want to do it again, click on "Refresh" in your browser window. Click here to go back to the main menu.
|