Instructions: 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,
speak() method is invoked size times.
If all that you wanted to do was to write the phrase six
times, the first version of the program is probably better than this
3. Here is a modification of the program.
Now six Vesta objects are created, and
each one uses its
speak() method once.
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.
new operator is used to create each object and
each newly created object's
speak() method is invoked.
In spite of its sophistication,
this version of the program is about as impractical as the previous version.
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
main() is now a very simple program.
main() is expected to be become longer as more and more things are
added to it, the
Vesta object might be sensible.
But for the simple task as stated, the first program is still the best.
6. In this version the class definition includes a constructor that initializes the message to be printed:
Again, whether the
Vesta class is sensible depends on the task.
If the task is complicated, and the above program is just a small step toward
a complete solution, then maybe the
Vesta class is a good design.
7. Further sophistication is to include the iteration limit in the constructor.
This version of the
Vesta class is more flexible than the previous version.
It is probably better since it can be used for more situations than the previous.
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 further increases the flexibility of the class.
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
main() method should not have to say so.
Classes can have several constructors that each take different parameters
Point class.) In the following version of the program
there are two constructors, one automatically initializes the message to "Hello Vesta"
and the other initializes the message to the requested message.
10. A further improvement to the
Vesta class is
to include a method
that changes the message string.
This is NOT a constructor---it is a method that is part of an
existing object and changes that object's message.
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.