Lab 02: Dancing Stick Figure

Goals: To write a class that inherits from another class

Overview: In this lab, there are three classes that will work together to animate a stick figure. The main applet (DancingStickFigureApplet) "has-a" DancingStickFigure which "is-a" StateStickFigure. Both the applet and StateStickFigure are provided.  They are attached below.

StateStickFigure is similar to the StickFigurePrimitive class used earlier in the year, except that the stick figure can be put into different states. Each arm and each leg can be set either up or down. When the stick figure is drawn, it will then appear with the arms and legs up or down as they have been set.

You will be writing the DancingStickFigure class. This class inherits from StateStickFigure and, among other things, provides the new method nextState() that puts the stick figure in the next state (changes arms and legs to be up or down).

The main applet will call nextState() and redraw the figure about three times per seocond.

What to do: Write a Java class named DancingStickFigure.java that inherits from the StateStickFigure class. This class should provide four constructors that mirror the constructors of the super class as well as the new method public void nextState(). This method will cause the stick figure to go from state 1 (for example, arms up and legs down) to state 2 (for example arms down and legs down), etc. If there are 5 states, the sixth call to nextState() should return the stick figure to the first state.

Note: Consider adding a new instance variable to keep track of the current state.

Sample Output: When you have the class completed, download DancingStickFigureApplet.java and run it. If the DancingStickFigure class has been written properly, something like this should appear:

A Dancing Stick Figure!


Starting Code: Here is a shell of the program to get you started.


public class DancingStickFigure extends StateStickFigure
{

    private int myState;

    public DancingStickFigure()
    {
        //your code here
    }

    public DancingStickFigure(double scale)
    {
        //your code here
    }

    public DancingStickFigure (int x)
    {
        //your code here
    }

    public DancingStickFigure (int x, double scale)
    {
        //your code here
    }

    //This state increments myState by one (or resets it to zero) and
    //then, depending on the value of myState, puts the arms and legs
    //up or down.
    public void nextState()
    {
        //your code here
    }
}

What to submit: Show me your program when completed.

© Daubenmier/Ehrenfried 2005
Subpages (1): applet files
ċ
DancingStickFigureApplet.java
(1k)
Mark Daubenmier,
Nov 30, 2011, 7:44 PM
ċ
StateStickFigure.java
(2k)
Mark Daubenmier,
Nov 30, 2011, 7:45 PM
Comments