- construction of large program from reusable module (subprogram, subroutine, function, procedure, CLASS)
- the Average applet
// filename: Average.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Average extends Applet implements ActionListener
{
public TextField input1, input2, display;
public Label labelTitle, label1input, label2input, label3display;
public Button b1;
public int num1, num2;
public double avg;
public void init( )
{
input1 = new TextField(12);
input2 = new TextField(12);
display = new TextField(12);
input1.setText("0");
input2.setText("0");
display.setText("");
display.setEditable(false);
labelTitle = new Label("Average Calculator");
label1input = new Label("First Number");
label2input = new Label("Second Number");
label3display = new Label("Computed Average");
b1 = new Button("Push to Compute Average");
b1.addActionListener(this);
add(labelTitle); add(label1input);
add(input1); add(label2input);
add(input2); add(b1);
add(label3display); add(display);
}
public void actionPerformed(ActionEvent e)
{
num1 = Integer.parseInt(input1.getText());
num2 = Integer.parseInt(input2.getText());
avg = ((double)num1 + num2) / 2 ;
display.setText(""+avg);
}
}
- the CustomAverage class
// filename: CustomAverage.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CustomAverage
{
/* average is an instance variable -- it is called
an "instance" variable since each instance or object
of this class will have ts own copy of this variable */
public double average;
// The constructor is given below it is just initializing
// the variable average to zero
public CustomAverage( )
{
average = 0.0;
}
// The following getAverage method is the only
// other method in the class
// it must have two integer values provided by
// the client class so it can sum them and compute
// the average. The (double) in front of the
// variable num1 is called a cast it forces Java
// to treat num1 as a double so the result of the
// entire expression will be a double.
// the type of the method is double not void this
// means that the method must return a double
// to the client class.
public double getAverage(int num1, int num2)
{
average = ( (double)num1 + num2) / 2;
return average;
}
}
- the Averaging Applet uses CustomAverage class
// filename: AverageApplet.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class AverageApplet extends Applet implements ActionListener
{
public CustomAverage average; // instantiate a CustomAverage object "average"
public TextField input1, input2, display;
public Label labelTitle, label1input, label2input, label3display;
public Button b1;
public int num1, num2;
public void init( )
{
average =new CustomAverage( ); // new the average object
input1 = new TextField(12);
input2 = new TextField(12);
display = new TextField(12);
input1.setText("0"); input2.setText("0");
display.setText(""); display.setEditable(false);
labelTitle = new Label("Average Calculator");
label1input = new Label("First Number");
label2input = new Label("Second Number");
label3display = new Label("Computed Average");
b1 = new Button("Push to Compute Average");
b1.addActionListener(this);
add(labelTitle); add(label1input);
add(input1); add(label2input);
add(input2); add(b1);
add(label3display); add(display);
}
public void actionPerformed(ActionEvent e)
{
// the next line uses the getAverage method
// in the CustomAverage class to
// compute the average. The two parameters of the
// method calls are actually
// method calls to the Integer class that
// will return the integer values the
// user entered into the two TextFields
display.setText
(""+average.getAverage(Integer.parseInt(input1.getText()),
Integer.parseInt(input2.getText()) ));
}
}