# The Probability/Statistics Object Library - Hello World!

Author(s):
Kyle Siegrist

Our goal in this section is the construction of a very simple applet from components in the library. To work through this example yourself, you will need the Java Software Development Kit (available at the Sun Java site), a text editor (such as Windows Notepad), and your Java-enabled browser.

The Hello World! applet requires two objects from the library: the Experiment object, which provides a basic shell for a random experiment, and the Coin object. We will need to modify the objects, so we will need the source files. First create a new empty folder. Click on the two links to go to the appropriate pages of the library, and download the source ZIP files into your folder.

Next extract the files in these two ZIP archives into your folder, but do not preserve the library folder names when you do the extraction. This will simplify the naming of our objects and consequently the folder structure that we must use. At this point, you should have the following essential files in your folder (as well as some other files that we will not need):

• Experiment.java
• Coin.java
• step.gif
• run.gif
• stop.gif
• reset.gif

The first two are the Java source files for our Experiment and Coin objects, and the next four are tiny GIF files for the buttons on the main tool bar.

Now open the file Experiment.java with your text editor, and remove the first programming line:

  package edu.uah.math.experiments;


Again, this step merely simplifies the names of the objects and allows us to put all of our files in one folder. Similarly, open the file Coin.java and remove the first programming line:

  package edu.uah.math.devices;


Next, create a new file called HelloWorld.java, type (or copy and paste) the following lines, and save the file to your folder:

  public class HelloWorld extends Experiment{
private Coin coin = new Coin();
public void init(){
super.init();
coin.setTailLabel("World");
addComponent(coin, 0, 0, 1, 1);
}
public void doExperiment(){
super.doExperiment();
coin.toss();
}
public void update(){
super.update();
coin.setTossed(true);
}
public void reset(){
super.reset();
coin.setTossed(false);
}
}


Let's try to understand what we have just done.

The first line creates a new applet object, called HelloWorld , which is a subclass of the Experiment object. Thus, HelloWorld will inherit all of the methods of Experiment.

The second line creates a new Coin object called, appropriately enough, coin .

The next group of lines is in a method called init that initializes the applet. The first line of this method calls the corresponding method of the parent Experiment object, while the next two lines change the default labels on the coin from H and T to Hello and World , respectively. The final line of the method adds the coin to the applet.

The next group of lines is in a method called DoExperiment that defines our random experiment. The first line calls the corresponding method in the Experiment object, and the second line tosses our coin.

The next group of lines is in a method called update that defines how the information in the applet will be displayed. The first line calls the corresponding method in the Experiment object, and the second line sets the "tossed" state of the coin to true (so that the coin label will be displayed).

Finally, the last group of lines is in a method called reset, which specifies the actions that occur when the user presses the reset button. Again, the first line calls the corresponding method in the Experiment object, while the second line sets the "tossed" state of the coin to false (so that the coin label will not be displayed).

Note that much of the structure and functionality of HelloWorld are inherited from the parent Experiment object by invoking the super- methods. This structure and functionality include the basic shell with the toolbar and buttons and the default actions performed when the user clicks on the buttons. All that we had to do was add the special functions that are appropriate for our applet.

At this point, you should be able to compile HelloWorld.java without errors using the Java compiler in the Java Software Development kit (or a more sophisticated Java development environment, if you have one).

Our final task is to create a stub HTML file so that we can view the applet in a browser. With your text editor, create a new file called HelloWorld.html, type (or copy and paste)the lines below, and save the file to your folder.



HelloWorld

The important tag is the applet tag, which merely has a reference to the HelloWorld class file and specifies the width and height of the applet.

That's it! You should be able to open HelloWorld.html with your browser and play with your applet. Click on the step button to toss the coin and see either "Hello" or "World" depending on whether you coin landed heads or tails. Click on the run button and practice changing the update and stop settings.

Figure 5 shows a screen shot of the applet. If you did not construct your own applet (or even if you did), you can click on the image to see the finished product.

### Figure 5. The Hello World! applet

Kyle Siegrist, "The Probability/Statistics Object Library - [i]Hello World![/i]," Loci (October 2004)

## JOMA

Journal of Online Mathematics and its Applications