Homework Assignment 4

Submission directions at end of this assignment.

  1. In C++, how can I inherit a class's implementation without inheriting its interface? See the video lecture on Multiple Inheritance if you are having difficulty or see my online notes.

  2. What is the difference between replicated inheritance and shared inheritance. What is the default type of inheritance in C++? See the video lecture on Multiple Inheritance if you are having difficulty or see my online notes.

  3. Provide 2 reasons why object-oriented programs run more slowly than procedural programs. See the video lecture on Inheritance Implementation if you are having difficulty.

  4. Answer each of the following questions about modules:

    1. Name two benefits of modules (hint: if you are having trouble, then check part b below--it might help).
    2. 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). Check out the video lecture on Module Mechanisms in C/C++ or check my online notes if you are having difficulties.

  5. This problem reviews Java interfaces from last week. 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?

  6. The type of graphics commands provided by a graphics package can influence the way a class hierarchy is designed. For example, some graphics systems, including X, provide a single command for drawing arcs, ovals, and circles. More particularly, X's command for drawing arcs, ovals, and circles can be roughly written as follows:
    	DrawArc(left, top, width, height, angle1, angle2)
    angle1 and angle2 denote the beginning and ending angle of the arc. An oval can be created by setting angle1 to 0 and angle2 to 360. A circle can be similarly created, except that in addition the width must equal the height.

    The format of this command could lead you to define a class hierarchy in which an oval is a subclass of an arc, and a circle is a subclass of an oval:

    The arc class might define the following methods:

    1. void SetAngle1(double angle);
    2. void SetAngle2(double angle);
    3. void Draw(some parameters);
    4. void setLeft(int left);
    5. void setTop(int top);
    6. void setWidth(int width);
    7. void setHeight(int height);

    The oval subclass does not want to provide either the SetAngle1 or SetAngle2 methods because it is a closed arc. The circle subclass additionally wants to eliminate the setWidth and setHeight methods and add a setDiameter method. Answer the following questions:

    1. What is the problem with the class hierarchy?
    2. Suggest another way to implement the oval and circle subclasses that allows you to "inherit" the implementation of the arc subclass without incurring the disadvantages of actual inheritance. Sketch out a sample Java class declaration for a circle that includes the following elements:
      1. declarations for the instance variable(s)
      2. declarations for the public methods (it is okay to use my "some parameters" notation for the parameters to the Draw method)
      3. implementations for the setLeft and setDiameter methods.

  7. You are given the following declarations in Java (you should assume that each class would be placed in a different file):
    package LinkedList;
    class ListNode {
        protected int value;
        String name;
    package LinkedList;
    public class List {
        ListNode header;
        protected ListNode sentinelNode;
        public List() {
            header = new ListNode();
    1)      header.value = 10;
    2)      header.name = "brad";
    	sentinelNode = new ListNode();
    package LinkedQueue;
    class Queue extends LinkedList.List {
        public Queue() {
    3)      header.value = 20;
    4)	sentinelNode.value = 30;
    Answer the following yes/no questions about the above code and for each answer explain why you answered as you did:

    1. Is it legal to access the value variable in statement 1?
    2. Is it legal to access the name variable in statement 2?
    3. Is it legal to access the header variable in statement 3?
    4. Is it legal to access the sentinelNode variable in statement 4?
    5. Is it legal to access the value variable in statements 3&4 (even if you answered no to either statement 3 or 4, assume that you had answered yes and consider whether based on a "yes" answer, if value would be accessable)? Hint the answer and reason is the same in both cases.

  8. Rewrite problem 5 from Homework 3 so that the files are placed in a package named graphics. If you could not make problem 5 work, then use my solution. This problem only requires you to add package declarations to your file and figure out how to jar it up so that it works properly from a jar file. You do not have to do any programming for this problem.

  9. This problem has several parts that walk you through the creation of creating an abstract class in C++ (much like an interface in Java) and a class that implements the abstract class by subclassing it:

    1. Create an abstract class named Queue with the following pure virtual methods:

      1. void add(int value): appends an integer to the back of the list.
      2. int remove(): removes and returns an integer from the front of the list.
      3. bool isEmpty(): returns true if the queue is empty and false otherwise.
    2. Create a class called ArrayQueue that subclasses the Queue class and that uses an appropriate data structure from the C++ stl library to implement the queue (do not use the STL's queue class--use either a vector or a linked list).
    3. Create a simple test driver program that:
      1. declares a variable of type Queue and assigns to it an instance of an ArrayQueue object ArrayQueue
      2. adds the integers 3, 10, and 5 to the queue, removes two elements from the queue and prints them on separate lines, adds the integers 6, 9, and 12 to the queue, and finally empties the queue by calling remove repeatedly until the queue is empty and printing each element. Sample output might be:
    4. Use the following filenames:
      1. Queue.h: Contains the class declarations for Queue and ArrayQueue.
      2. ArrayQueue.cpp: Contains the class implementation for ArrayQueue
      3. QueueDriver.cpp: Contains your test driver program

    What To Submit

    Submit three files:

    1. A pdf or ascii file with your answer to questions 1-7.
    2. An executable jar file named Graphics.jar with your classes and source code for question 8.
    3. A tar file with your .cpp/.h files for question 9. Make sure that you follow our naming conventions so that the TA can easily compile your files using a makefile.