Homework Assignment 3

  1. Provide 2 reasons why object-oriented programs run more slowly than procedural programs.

  2. Answer each of the following questions about modules:

    1. Name two benefits of modules, as discussed in class.
    2. What is Java's module mechanism (you can simply state the keyword).
    3. C++ uses two features to implement its module mechanism. Name those two features, and match them to the two benefits you described in part (a) (i.e., for each one C++ feature, indicate which benefit it provides).

  3. Behold the following interface and class declarations:
        interface ActionListener {
          void actionPerformed(ActionEvent e);
          void buttonClicked(ActionEvent e);
        class Actor implements ActionListener {
          public Actor();
          public void actionPerformed(ActionEvent e);
          void buttonClicked(ActionEvent e);
          public void displayBanner(String banner);
    Answer the following questions:
    1. Declare a variable called myListener to be of type ActionListener and initialize it with an instance of Actor.
    2. Can myListener call actionPerformed? Why or why not?
    3. Can myListener call displayBanner? Why or why not?

  4. Suppose you have a bank simulation in which customers enter a bank, are serviced by a teller, and then leave. Further suppose that there are the following types of events in your simulation:

    1. Arrival event: A customer arrives at the bank at time t. No other information is associated with the arrival event.
    2. Teller event: A customer with customer id cust_id is assigned to teller teller_id at time t.
    3. Departure event: A customer with customer id cust_id finishes his/her business with teller teller_id and leaves the bank at time t.
    The time, customer id, and teller id are all integers. Each event must also have a method called processEvent associated with it that processes the event. You do not need to know what this method does, but the method will be different for all three events. Each event will want to provide access to its time through a getTime method. The implementation for this method is identical for all three events (i.e., they would like to share the implementation). The simulation will maintain an event queue in which events of any of the above types are added to the queue. The queue should support two operations: 1) insertion of an event into the queue based on its time, and 2) removal of the event with the smallest time.

    Answer the following questions:

    1. Design an inheritance hierarchy for this simulation by drawing a tree with class names.

    2. For each class indicate which variables will be declared by that class.
    3. Should processEvent be a virtual or non-virtual method? Why?
    4. Should getTime be a virtual or non-virtual method? Why?

    5. What is the most efficient data structure for implementing your queue (hint: think back to either CS140 or CS302)?

  5. Augment your answer to the expression tree problem in homework set 1 by:

    1. allowing the values of expression trees to be assigned to variables.

    2. allowing expressions to reference variables. The variables can be any string.

    For example, I should now be able to type:

    >>> = total 6 >>> 6.00 >>> = tax .08 >>> 0.08 >>> = total * total + 1 tax // same as (total = total * (1 + tax)) >>> 6.48 A sample parse tree for the expression = total * total + 1 tax would be:
                   /   \
               total    *
                      /   \
                   total   +
                         /   \
                        1    tax
    With this assignment you must handle recursive prefix expressions. It is quite acceptable to use my solution from homework 2 to implement recursive prefix expressions. I will release my solution to homework 2 three days after it is due.

    1. You should assume that the data is error free. Hence you do not need to worry about badly formed expressions, using variables before they are assigned a value, or trying to assign to numbers. We will defer error handling until we cover exception handling.

    2. You should use an appropriate data structure to store variable names and their values (you only have to store their values, you do not need to save the expression trees once they have been evaluated) and you should use Java's pre-defined set of data structures if possible.

    3. Hints:
      • You will need two new classes for this assignment, one for assignment and one for ids. The eval method for assignment should evaluate its right hand argument and assign the resulting value to the appropriate id. The eval method for id should look up the value of the id and return it.

      • When you assign a value to a variable (i.e, use it as an lvalue), you should first check whether an object exists for that variable. If no object yet exists, then you should create an object for it and establish a binding between that variable and its object in your data structure. If an object already exists for that variable, then you should use that object.

      • When you try to reference the value of a variable (i.e., use it as an rvalue), you should use the variable name to look up the object for that variable and place a pointer to that object in the expression tree.