Anyone for Java?

If you don't see a title in changing rainbow colors at the top of this page, your browser does not understand the Java language. Get rid of the old clunker and try the latest version of Netscape!

Session 2 - March 12, 1997


Some Home-brewed Java

Today we will continue to learn how to write our own simple Java applets.


In a class of our own

A Java applet is composed entirely of classes. You can think of a class as a template for a particular type of object, such as a car. An object is characterized by attributes and methods. In the case of a car, attributes include color, number of doors, horsepower, manufacturer, year, and model. Methods are often actions, that, in the case of a car, would include go, stop, accelerate, turn, start the windshield wipers, and stop the windshield wipers.

We need to make a distinction between a type of object and an individual example of that type of object. For example, the word Car refers to a type of object, so Car is a class. A particular car, such as my 1988 Subaru Justy, is an individual example of a Car. In Java, I would call my car an instance of the class Car. When we name classes in Java, we traditionally start the name with an uppercase letter. However, instances of classes are referenced by instance variables, and the names of these variables traditionally start with lowercase letters. Names of methods also should start with lowercase letters.

The actions of methods can be modified by parameters. For instance, a turn can be to the right or the left, and should also specify a number of degrees. Windshield wipers can be put on intermittent, slow, or fast. In a Java implementation of a car, you might have a statement like carThree.turn("left", 90). carThree that uses method turn to tell an instance of a Car called carThree to turn and passes "left" and 90 to the turn method as parameters. carTwo.turnOnWipers("front", 3) might tell carTwo to turn on its front wipers at speed 3.

In order to make this discussion less abstract and more real, we will now create a class and then use it in an applet. The applet will create and use four instances of this class. Let's get into the habit of thinking about programming in terms of classes.

Suppose we want to create a class that can keep track of weather statistics for a locality on a given date. We can define the following class:

// WeatherData.java
import java.awt.*;
public class WeatherData {
  // the data
  private String locality;
  private String month;
  private String day;
  private String year;
  private int highTemp;
  private int lowTemp;
  private double precipitation;
  private double pressure;

  // constructor method creates an instance of the WeatherData class and initializes it
  // a constructor has the same name as the class
  public WeatherData(String locality, String month, String day,
            String year, int highTemp, int lowTemp, double precipitation,
            double pressure) {
    this.locality=locality;
    this.month = month;
    this.day = day;
    this.year = year;
    this.highTemp = highTemp;
    this.lowTemp = lowTemp;
    this.precipitation = precipitation;
    this.pressure=pressure;
  }

  // display method that puts the data on the screen
  public void display(Graphics g, int xloc, int yloc) {
    g.drawString(locality, xloc, yloc+10);
    g.drawString("Date: "+month+" "+day+", "+year, xloc, yloc+20);
    g.drawString("High: "+highTemp,xloc, yloc+30);
    g.drawString("Low:  "+lowTemp,xloc, yloc+40);
    g.drawString("Precipitation: "+precipitation, xloc, yloc+50);
    g.drawString("Pressure: "+pressure, xloc, yloc+60);

  }
}

The name of the file containing the class must be the same as the name of the class, with a .java extension added. Therefore, WeatherData.java would be the name of our file.

Now, how do we use our new class? We will write an applet that creates and displays four instances of the WeatherData class.

import java.applet.Applet;
import java.awt.*;
public class WeatherApplet extends Applet {
    // Declare four instance variables of the WeatherData class
    public WeatherData locationOne, locationTwo, locationThree, locationFour;

    public void init() {
        // Initialize the four instance variables
        locationOne = new WeatherData
             ("Stony Brook, NY", "March", "12", "1997", 70, 55, 0.0, 30.10);
        locationTwo = new WeatherData
             ("Ross Ice Shelf, Antarctica", "March", "12", "1997", 5, -22, 0.6, 29.95);
        locationThree = new WeatherData
             ("Upper Mantle", "March", "12", "1997", 5, -22, 0.0, 1800000.0);
        locationFour = new WeatherData
             ("Inner Core", "March", "12", "1997", 3001, 2999, 0.0, 9000000000.0);
    }
    public void paint(Graphics g) {
        g.drawString("The CHiPR Weather Report", 10, 10);
        locationOne.display(g, 10, 30);
        locationTwo.display(g, 250, 30);
        locationThree.display(g, 10, 130);
        locationFour.display(g, 250, 130);
    }

}

The following HTML code can display the applet:

<applet code="WeatherApplet.class" width=500 height=200>
</applet>

The following display results when the HTML file is opened in the browser you are now using, assuming that both class files and the HTML file are all in the same directory:

A Trip to the Library

... And while you're there, you may want to check out some books on Java.

Last modified March 6, 1997


[Anyone for Java? home]