CPSC 124, Fall 2001
Quiz #6, November 5

This is the sixth quiz in CPSC 124: Introductory Programming.

Question 1: Suppose that the definition of a class begins with the line

       public class Fred extends Applet implements MouseListener {

What does the phrase "implements MouseListener" mean here? When you see this phrase, what would you expect to find in the class and how would you expect it to be used?

Answer: MouseListener is an "interface", which means that it specifies some methods that must be defined in a class that implements the interface. The MouseListener interface specifies five methods for handling mouse events, such as mousePressed() and mouseEntered(). I would expect to find definitions for these methods in the class, although some of them might be empty.

Since I assume that there is a reason for making the applet implement MouseListener, I would expect the applet to be set up to listen for mouse events. It might be set to listen for mouse events on itself with the command addMouseListener(this), or it might be set to listen to some other component with a command such as comp.addMouseListener(this).

Question 2: Explain the term polymorphism, and give an example.

Answer: Polymorphism refers to the fact that different objects can respond to the same message in different ways. More exactly, a method call such as obj.method() can call different methods in various classes, depending on the actual type of object to which the variable obj refers. This can happen because a method in a class can be redefined in a subclass. For example, a Shape class might have a draw() method. This method might be overridden in subclasses such as Rectangle and Oval. If shape is a variable of type Shape, then shape could refer to an object that belongs to any of the classes Shape, Rectangle, or Oval. When shape.draw() is called, the method that is executed is the one in the appropriate class. If shape is a Rectangle, then shape.draw() presumably draws a rectangle. If shape is an Oval, then shape.draw() presumably draws an oval.

("Polymorphism" could be translated as "many shapes" or "many forms".)

Question 3: The repaint() method in an applet does not actually draw anything on the applet. What does it do?

Answer: The repaint() method does not itself call either the paint() or update() method. It only tells the system that it should call paint(). The system will call the paint() method later, when it gets a chance. In general, repaint() is called by a program when the picture drawn on the applet has to change. (The system also calls paint() automatically when the applet has to be drawn because it has just appeared on the screen or because it has been covered by another window and has just been uncovered. But this has nothing to do with repaint() and does not ordinarily result in a change in the picture shown on the applet.)

Question 4: What is the relationship between objects of class Button and ActionEvents ?

Answer: When a button is clicked, it generates an event. This event is represented by an object of type ActionEvent. This object is passed to the actionPerformed method of any ActionListener that has been registered to listen for action events from the button. Thus, ActionEvents are part of the mechanism that is used to make some action occur when a button is clicked.

David Eck, eck@hws.edu