Step 4 - Code

  • imports
  • class NumTextField extends java.awt.TextField ...
    • Need to understand TextField</FONT></B> </LI>
  • class Hops extends java.util.Observable ...
    • Need to understand Observable and Observer
  • public class HopsDemo extends java.applet.Applet implements java.util.Observer ...
    • Need to understand Applet

imports

  • Specify dependencies between this class and other classes
  • Note the awt.* – bring in everything in awt.
import java.awt.*;
import java.applet.Applet;
import java.util.Observable;
import java.util.Observer;

class NumTextField extends TextField

class NumTextField extends java.awt.TextField {
    public int getIntValue() {
        int v;
        v= Integer.valueOf( getText() ).intValue();
        return v;
    }

    public double getDoubleValue() {
        double d;
        d= Double.valueOf( getText() ).doubleValue();
        return d;
    }

    public void setValue( int v ) {
        setText( String.valueOf( v ) );
    }

    public void setValue( double v ) {
        setText( String.valueOf( v ) );
    }

}

class Hops extends Observable

// local storage
private double theHBU;
private double theAlpha;
private double theWeight;

// class-level constants -- what did the user change?
private static final int HBU= 1;
private static final int ALPHA= 2;
private static final int WEIGHT= 3;

// state of the updates
private int lastSet= 0;
private int priorSet= 0;

// class Hops, private state change method

private void set( int item ) {
    if( item != priorSet || item != lastSet ) {
        priorSet= lastSet; lastSet= item;
    }
    if( priorSet != 0 &amp;&amp; lastSet != 0 ) {
        if( priorSet != WEIGHT &amp;&amp; lastSet != WEIGHT )
            theWeight= theHBU / theAlpha;
        if( priorSet != HBU &amp;&amp; lastSet != HBU )
            theHBU= theAlpha * theWeight;
        if( priorSet != ALPHA &amp;&amp; lastSet != ALPHA )
            theAlpha= theHBU / theWeight;
        setChanged(); notifyObservers();
    }
}

// class Hops, public interface

public void setHBU( int hbu ) {
    theHBU= (double)hbu; set( HBU ); }

public void setAlpha( double alpha ) {
    theAlpha= alpha; set( ALPHA ); }

public void setWeight( double weight ) {
    theWeight= weight; set( WEIGHT ); }

public int getHBU() { return (int)theHBU; }

public double getAlpha() { return theAlpha; }

public double getWeight() { return theWeight; }

public class HopsDemo extends java.applet.Applet implements java.util.Observer

  • Global declarations for all methods of the Applet
// the hops model
Hops theHops;

// useful view items
NumTextField hbu, alpha, ounces;

// class HopsDemo

public void init() {
    setLayout( new GridLayout(0,2) );
    add( new Label(&quot;HBU's&quot;,Label.RIGHT) );
    hbu= new NumTextField();
    add( hbu );
    add( new Label(&quot;Alpha Acid %&quot;,Label.RIGHT) );
    alpha= new NumTextField();
    add( alpha );
    add( new Label(&quot;Ounces&quot;,Label.RIGHT) );
    ounces= new NumTextField();
    add( ounces );
    theHops= new Hops();
    theHops.addObserver( this );
}

// class HopsDemo

public boolean action(Event evt,Object what) {

    //System.err.println(evt); // debugging option
    showStatus(&quot;&quot;);

    if( evt.target == hbu ) {
        try{
            theHops.setHBU( hbu.getIntValue() );
        }
        catch( NumberFormatException e ) {
            showStatus(&quot;HBU's invalid&quot;);
            hbu.selectAll();
            hbu.requestFocus();
        }
    }

    // same for other two fields
    // some obvious redundancy here, looks like a better design is required

    return false;
}

// class HopsDemo

public void update( Observable obj, Object what ) {
    hbu.setValue( theHops.getHBU() );
    alpha.setValue( theHops.getAlpha() );
    ounces.setValue( theHops.getWeight() );
}

Table Of Contents

Previous topic

Step 3 - Detailed Design

Next topic

3. Improvements