Classes & Forms - The Gizmo Factory

bullet

One form that creates new Gizmo's on the bottom of the screen with each button click

wpe9.jpg (11114 bytes)

bullet

Each Gizmo has several properties:

bullet

length, width, and height - all integers

bullet

status - either asleep or awake

bullet

The gizmoFactory form will display a message when a 6th Gizmo object is added

bullet

An about button displays a simple messageBox with who-did-this information

bullet

The following program is raw - no use of static or final when defining any of the members of the class.

//Gizmo.java

public class Gizmo
{
private String manufacturer = "Gizmo Products, Inc.";
private int gizmosInService = 0;
private int length;
private int width;
private int depth;
public Gizmo (int len, int wid, int dep)
{
gizmosInService = gizmosInService + 1;
length = len;
width = wid;
depth = dep;
       
}
public int getInServiceCount ()
{
return gizmosInService;
}

public String getManufacturer ()
{
return manufacturer;
}

    public int awake = 0;
    public int asleep = 1;
    private int status = asleep;

    public void setStatus (int stat)
    {
        status = stat;
    }

    public int getStatus ()
    {
        return status;
    }
}

// GizmoFactory.java

import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;

/**
* This class can take a variable number of parameters on the command
* line. Program execution begins with the main() method. The class
* constructor is not invoked unless an object of type 'GizmoFactory'
* created in the main() method.
*/
public class GizmoFactory extends Form
{
private void aboutGizmosBtn_click(Object source, Event e)
{
MessageBox.show("Gizmos are proudly manufactured by "+
gizmo1.getManufacturer());
}

private Gizmo gizmo1;
private Gizmo gizmo2;
private Gizmo gizmo3;
private Gizmo gizmo4;
private Gizmo gizmo5;

   
private void makeAGizmoBtn_click(Object source, Event e)
{
int gizmoStatus = 0;
if (gizmo1 == null)
{
gizmo1 = new Gizmo(1,2,3);
gizmoIcon1Pic.setVisible(true);
           
}
else if (gizmo2 == null)
{
gizmo2 = new Gizmo(2,9,7);
gizmoIcon2Pic.setVisible(true);
           
}
else if (gizmo3 == null)
{
gizmo3 = new Gizmo(5,6,1);
gizmoIcon3Pic.setVisible(true);
           
}
else if (gizmo4 == null)
{
gizmo4 = new Gizmo(1,7,4);
gizmoIcon4Pic.setVisible(true);
           
}
else if (gizmo5 == null)
{
gizmo5 = new Gizmo(8,1,7);
gizmoIcon5Pic.setVisible(true);
           
}
else
{
MessageBox.show("Your Gizmo Pen is full! No more Gizmos can be created!",
"No Room",
MessageBox.ICONEXCLAMATION+MessageBox.OK);   
}
gizmoInfoLbl.setText("There are currently "+
gizmo1.getInServiceCount()+
" gizmos in service");
}

    public GizmoFactory()
    {
        // Required for Visual J++ Form Designer support
        initForm();        

        // TODO: Add any constructor code after initForm call
    }

    /**
    * The main entry point for the application.
    *
    * @param args Array of parameters passed to the application
    * via the command line.
    */
    public static void main(String args[])
    {
        Application.run(new GizmoFactory());
    }

bullet

Upon examination, try to figure out where to modify the code so that --

bullet

gizmosInService will accurately reflect the total number of Gizmo objects being used

bullet

the Gizmo constructor can be used to initialize status

bullet

the form can display the status in a text box underneath each displayed Gizmo ... and is expressed with the corresponding word asleep or awake

bullet

manufacturer becomes a shared constant (a class or static member of the Gizmo class)

bullet

The solution is downloadable by clicking here (I hope!)

Last Updated by DrB on 18 February 2009