This Magercise focuses on design issues. The primary task is to add a state variable, surprised, to the StickFigure Bean, plus access/control methods for turning the surprise state on and off. Secondary tasks include building, packaging, and loading the Bean into a Bean-aware tool for verification.
Magercise 4 Prerequisites
Skeleton Code
Tasks
The first step is to create a working directory somewhere on your filesystem, hereafter referred to as workingdir.
Next, create the directory stickfigure (all lowercase characters) directly under workingdir.
Next, (1) save all the skeleton files, except the *.java files, within workingdir and (2) save the *.java skeleton files within stickfigure, that is, within workingdir/stickfigure.
The next, and primary, task is to add surprise functionality to the StickFigure Bean. You must implement the addSurprise() and removeSurprise() control methods, as well as the boolean variable surprised (note the "d" ending, for the variable only), which is manipulated by these two methods. This variable should be initialized to false.
The surprise state logic is that if a StickFigure Bean instance is in the surprised state, the drawing operations will draw the mouth differently. Note that the control methods must invoke repaint() after setting the surprised variable. Do not substitute a setSurprise() method for these two control methods--the surprise state is not a property.
The logic for handling the surprised state is already incorporated into the Bean's drawing operations--for this reason, you must use the variable name surprised. The point of this Magercise is to add the control methods for externally manipulating the state of surprise, and then connect these methods to external controls (command buttons) from a development tool.
Compile all JavaTM source files, *.java, from workingdir by including the stickfigure directory in the path specification.
Build StickFigure.jar from workingdir by executing the file MakeStickFigureJar.bat (Windows) or MakeStickFigureJar.sh (UNIX).
Next, load the StickFigure Bean into the BeanBox (or a comparable tool) using "LoadJar..." from the BeanBox "File" menu.
The next task is to configure surprise and no-surprise controls for a StickFigure Bean. First, select and place a StickFigure and two OurButton Beans on the worksheet.
Next, change the label for either OurButton Bean to "Surprise Me" and connect its action-performed functionality to the StickFigure Bean's add-surprise functionality.
Next, change the label for the other OurButton Bean to "No More Surprises" and connect its action-performed functionality to the StickFigure Bean's remove-surprise functionality. After setting up these connections, you will be able to use the two command buttons to control the stick figure's state of surprise.
Where help exists, the task numbers above are linked to the step-by-step
help page.
Note that the "mood" property displays the integer value -1. The set of possible values includes -1, -2, and -3. The meaningless magic numbers, of -1, -2, and -3, are unacceptable for Beans designed to be used by other programmers; hence, in an upcoming Magercise, you can add a custom property editor that displays meaningful string values.
Solution Source
Demonstration
After loading the StickFigure Bean, it should appear in the palette, from where it can be selected and instantiated on the worksheet:
-
Magercise 5
-
Short Course
-
About This Short Course
Copyright © 1998-1999
MageLang Institute.
All Rights Reserved.