1. Virtual Laboratories
  2. Object Library
  3. Hello World Tutorial

Hello World Tutorial

Introduction

The goal of this section is the construction of a very simple applet from components in the library. This tutorial is adapted from the article The Probability/Statistics Object Library, published in the Journal of Online Mathematics and Its Applications (2004). To work through this example yourself, you will need the Java Software Development Kit (available at the Sun Java), a simple text editor (such as Windows Notepad), and of course, your Java-enabled browser.

Note that this tutorial is not relevant if you simply want to use one of the applets without modification. In that case, you simply need to download the Java Archive (JAR) file objects.jar and write the appropriate applet tag in your HTML page. Detailed instructions are given on the page for each applet.

Programming the Objects

The Hello World applet will require 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, download objects.zip, which contains the source and resource files for all objects in the library. Next extract the files in this ZIP archives into a folder of your choice. Next, create a new folder, and copy the following files from the object library into the new folder. (The file Coin.java is in the devices package; the other files are in the experiments package.)

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

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

  package edu.uah.math.experiments;
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 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.setHeadLabel("Hello");
      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 done.

  1. The first line creates a new applet object, called HelloWorld that is a subclass of the Experiment object. Thus, HelloWorld will inherit all of the methods of Experiment. Note, however, that we do not need to know or care what happens in the parent method; this is one of the essential ideas of object oriented programming.
  2. The next line creates a new Coin object called, appropriately enough, coin.
  3. The next group of lines form a method called init that initializes the applet. The first line of this method calls the corresponding method of the parent Experiment object, which creates the toolbar and other basic structure. The next two lines in this method 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.
  4. The next group of lines form a method called doExperiment that defines our random experiment. Once again, the first line calls the corresponding method in the Experiment object while the second line tosses our coin.
  5. The next group of lines form a method called update that defines how the information in the applet will be displayed. Once again, the first line calls the corresponding method in the Experiment object while the second line sets the "tossed" state of the coin to true (so that the coin label will be displayed).
  6. Finally, the last group of lines form a method called reset that 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 coin label will not be displayed).

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

The HTML File

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

<html>
    <head>
      <title>Hello World</title>
    </head>
    <body>
      <p><applet code="HelloWorld.class" width="450" height="300">HelloWorld</applet></p>
    </body>
  </html>

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. A screen shot of the applet is given below; you can click on the image to see the live applet, if you did not construct your own applet.

Hello World Applet