CPSC 124, Fall 2011
Quiz #7

Question 1. Explain the purpose of the statement:

     panel.addMouseListener( listener );

Answer. This command says that whenever a mouse event occurs on the panel, then the appropriate method in listener should be called to handle that event. The listener object must implement the MouseListener interface, which includes methods such as mousePressed(evt) and mouseExited(evt). For example, when the user presses the mouse on panel, the listener's mousePressed method will be called, and this method can respond to the event.

Question 2. What is an abstract class?

Answer. An abstract class is a class that exists just for the purpose of creating subclasses and defining the common properties of all its subclasses. An abstract class is not used to directly create objects. In Java, a class must be declared to be abstract; for example,

     public abstract class DrawItem { ... }

Question 3. Suppose that Fillable is an interface defined as

     public interface Fillable {
         public void fill();

What are the two things that a class would have to do to implement this interface?


1. The class must include a full definition for fill:   public void fill() { ... }

2. The class must declare that it implements Fillable. For example:

     public class Jug implements Fillable {
         public void fill() { 
            ... // definition of fill
         ...  // other stuff

Question 4. Suppose that the following class represents simple counters:

     public class Counter {
         protected int count = 0;
         public void increment() {
            count = count + 1;

Write a subclass of Counter that inherits everything from that class but adds a new method named reset. The reset() method should set the value of count to 0.

Answer. (The new class must extend Counter and must define a reset method. The method can change the value of counter since that variables is protected in class Counter.)

     public class ResetableCounter extends Counter {
         public void reset() {
            count = 0;