CSC 241- Lec 3 (July 8th 1996)


Applets

An applet is a small embeddable Java program. In our course, we will set up HTML files that will run our applets. What you will write are subclasses of the Applet class. The Applet class is a subclass of Panel which in turn is a subclass of Container, which in turn is a subclass of Component, which in turn is a subclass of the Object class. Component implements an interface named ImageObserver. We really have not discussed interfaces and this not the time for it anyway, but suffice it to say that they are similar to abstract classes (i.e. classes that don't have an implementation). Why do I bother telling you about the class hierarchy leading to the Applet class? I guess it is rooted in my desire to know how things work. For the Applets that you will need to write for this course you will have examples to guide you as to what methods you need to write an so forth, however, if you decide to find out more about any of those methods, you may need to look much deeper than just the Applet class. Java is rich with classes that enable you to do graphics and many other neat things. Probably, the most appealing part of Java is the Applet class and that fact that it can be transported across the internet and run on any platform. We will begin by looking at Hello World example. I wrote a couple of simpler Applets first, if you like to check them out too: First Hello World and Second Hello World.

A Hello World Applet

This is a detailed discussion of the Third Hello World Applet.

public class hello3 extends Applet means that the class name is hello3 and it is a subclass of the Applet class.

The variable declarations at the top of this class work the same way as they do with other classes in Java--they are available in all methods of hello3.

private Font hello_font; is the declaration of hello_font which will later get constructed in the init method. This is the statement that constructs hello_font: hello_font=new Font("Helvetica",Font.BOLD,48);. Note that the arguments for the constructor are the Font name, its style, and size. In the following pair of statements, we first set the font to hello_font for our applet's Graphics object (all applets have one), then we draw the string "HELLO WORLD" using that font. g.setFont(hello_font);
g.drawString("HELLO WORLD",40,110);

private Button bye_button; is the declaration of bye_button which will also get constructed in the init method. The following sequence of statements construct the bye_button and set its front and background colors: bye_button = new Button("Bye");
bye_button.setForeground(Color.black);
bye_button.setBackground(Color.lightGray);

The statement that follows these this.add(bye_button); is designed to include the button in the drawing area associated with the applet.

The paint method is invoked once when the applet starts, but you will notice that we call it from the action method also. The parameter for this method is a Graphics object, check out the Graphics class to see the description of the methods invoked here.

The action method is the most interesting one used here. It handles events initiated by the user. In our case, it is invoked when either of our buttons our clicked. Note that we check, if the action taken by the user was the click of either hello_button or the bye_button. If the event is not one of those two, we pass it along to the supper class action method.


Stacks and Queues

These two classic data structures have simple and clean behaviors that make them a good topic for a starting point in discussing data structures. Data structures refers to the representation of data in our programs/systems. Stacks and Queues both act as a container of elements with well defined methods that enter elements into them or take elements away. A Stack allows us to implement a Last In/First Out (LIFO) behavior, which simply means new elements are always put in front of the line and that is where they come off from. Queues, on the other hand, behave in First In/First Out manner.

There are numerous application for either of these data structures. For example, you issue a print command on a multi-user system, is it not possible for other people to also issue the same command? A queue allows us to process these print requests in order. An example of where a stack is useful is in implementation of a preemption mechanism for resources. Lets consider a single cpu system with multiple processes competing for access to cpu. Often we assign priority to these requests. Suppose we have 4 priority levels, 1 through 4. Now, assume a process with the lowest priority (1) takes over the cpu and is processed for a while. Suppose that a new process comes along with priority 2, it preempts the first process by taking it over the cpu, but the system can't throw away the first process, so it is put on a stack. Consider a process with priority 3 coming along and preempting the process with priority 2, guess what happens to the process that was preempted? Yes, it gets put on the stack, but because of the behavior of the stack, when the priority 3 process is done, it is no brainer as to who should be picked from the ones preempted, because the one with the highest priority will be at the top and is one we take off since the data structure is a stack.

There are variety of implementations for stacks and queues. For a queue, we can define an array and put each element that comes in in the next available spot. When it is time to take an element off, we take the element in cell zero and move the rest back. We can implement a circular queue where we keep to indexes one to a the front of the queue and one to the back. In such a implementation, we don't move everything back when an element is taken off--we just move our index over to the next cell. But, it s more complicated, as the name indicates when we get to the last spot in the array we circle back to the beginning. This makes adding and removing a little more complex, but the advantage is in that you don't have to move everything back, each time that you remove an element. The last implementation is a linked list. If we don't know how much room we need to put aside for our quere, we implement it with a linked list that can grow indefinitely! We will look at more next time, but here is a simple one for stacks.

  public class Stack {
  private int size;
  private int top=-1;
  private int l[];
  public Stack (int init_size) {
    size = init_size;
    l=new int[size];
  }
  public boolean empty() {
    return top == -1;
  }
  public boolean full() {
    return top == size-1;
  }
  public int pop() {
    top--;
    return l[top+1];
  }
  public void push(int val) {
    l[++top]=val;
  }
}

To see the complete .java file, go to intStack.java and to see a test program for it check out test1.java.