CPSC 170: PreLab 5

  1. Read section 7.7 in Lewis&Loftus. Use the information in this section and the online Java documentation and answer the following questions:

    1. List the methods in the MouseListener interface. They all take the same type of parameter; what is this type?


    2. List the methods in the MouseMotionListener interface. They all take the same type of parameter; what is this type?


    3. In the online documentation, look up the class that defines the parameter type in (a) and (b).
      1. What class is this a subclass of?

      2. Given an object of this type, you can tell exactly what event occurred using the getID method, which returns one of the constants defined in the class. But getID is not listed as a method for this class; what class is it inherited from?


    4. Look at the MouseAdapter class. It has the same methods as the MouseListener interface, but, as the documentation indicates, they have empty bodies. What is the purpose of this class?


  2. Consider classes Person and Student as shown below, and the following object declarations and instantiations:
    -----------------------------------
    
    public class Person
    {
      private long ssn;
      private String name;
    
      public Person(String name, long ssn)
      {
        this.name = name;
        this.ssn = ssn;
      }
    
      public String toString()
      {
        return name + " " + ssn;
      }
    }
    
    -----------------------------------
    
    public class Student extends Person
    {
      double gpa;
    
      //method definitions go here
    }
    
    -----------------------------------
    
    1. Write a constructor for the Student class that takes a name, ssn, and gpa and sets up a new Student object accordingly. Remember that Student is a subclass of Person.







    2. Write a toString method for the Student class that returns a string containing the student's name, ssn, and gpa. Note that name and ssn are private in Person.







    3. Suppose that object p is of type Person and object s is of type Student. Tell whether each assignment below is legal:
      1. p = s;
      2. s = p;