Prelab 4

Windows Manager
Due by 10am, Monday 1 Oct 2012

In this prelab, you will familiarize yourself with some of the design and implementation issues in the upcoming lab 4. Please write or type up your solutions, and hand in a paper copy before class on Monday. Remember, late prelabs receive zero credit.


Motivation

On your monitor right now, you most likely have at least two open windows. Often, the windows on the screen get "stacked" one on top of the other, and depending on how they are arranged you may be able to see all the windows, or just parts of some. To bring a hidden window to the front, you just click on it, and all the other windows remain in the same order as before. For example,

 

 

A stack of windows After clicking on the fourth window from the top

This lab is going to model this behaviour using a linked list.

Part 1 - Doubly Linked Lists

In this section of the lab you will implement your own doubly-linked list and a corresponding iterator.

Suppose your MyLinkedList<T> class is implemented as a doubly linked list with the following class members:

    private Node<T> head;
    private Node<T> tail;
    private int     size;

where both head and tail are null when the list is empty.


  1. Which (if any) of the add(index, data) and remove(index) methods require special cases in the following situations:
    • size is equal to 0.
    • index is equal to 0.
    • index is equal to size.
    • index is equal to size-1.


  2. Suppose you were to write an append method with the following header:
        public void append(MyLinkedList<T> other)
    
    that should append the given other linked list on to the end of the linked list pointed to by head. Write the method body for the this method, keeping in mind any special cases you may need, such as one or both lists being empty. Don't forget to update this list's size!


  3. Suppose both the current list and other are of size O(n). What is the running time of your append method from question 2?


  4. Suppose the Java's String class is implemented as an ArrayList of Characters. Now suppose I have two Strings of length O(n). How long would it take to append one string to the end of the other?




  5. Your MyLinkedList class will generate a ListIterator when the factory method listIterator() (from AbstractList) is invoked. ListIterators support both going forward and backwards and contain methods previous() and hasPrevious() in addition to next() and hasNext().

    Fill in the following methods listed in this anonymous class:

        public ListIterator<T> listIterator() {
            return new ListIterator<T>() {
                // What class members do you want here?
    
    
    
                public boolean hasNext() {
    	        // what line of code goes here?
    
    
                }
                public T next() {
                    // what lines of code go here?
    
    
    
    
    
                }
     
    
    
                public boolean hasPrevious() {
    	        // what line of code goes here?
    
    
                }
                public T previous() {
                    // what lines of code go here?
    
    
    
    
    
                }
                // ... and some more ...
            };
        }
    
  6. What if we also wanted to implement both a remove() method that removes the most recently returned item from either next() or remove() as well as an add(x) method that adds a new item just before whatever would have been returned by next() but just after whatever would have been returned by previous()? Give brief method bodies for these as well:
            public void remove() {
                // what lines of code go here?
    
    
    
    
            }
    
            public void add(T x) {
                // what lines of code go here?
    
    
    
    
    
    
            }
    


Part 2 - Windows Manager

In the second part of the lab you implement a Windows Manager with your MyLinkedList.

Program Structure

A WindowsManager object is a "monitor" that maintains a list of rectangular windows displayed within it. It may contain any number of windows, and they may overlap; overlapping windows form a sort of stack, stored in a WindowList object that you will write.

Windows respond to mouse clicks; the clicked-on window moves to the top of the "stack".

Each Window may contain coloured squares, stored in a SquareList object that you will write. One may draw a square in a window by clicking in the window somewhere that's not already occupied by a square. One may remove a square from a window by clicking on the square. Thus each window is associated with a list of squares to be drawn in the window. The squares in the window are drawn (in any order) when the window is drawn. All squares in a window are drawn in the same colour, a colour associated with the window (thus different windows may be associated with different colours.)

Each Square is a square of a given colour. It draws itself using a paint method, which is called when a window changes or is moved in the list.

WindowList

Your first task is to write a class WindowList to represent a list of windows being monitored. As such, your WindowList class will contain a class variable MyLinkedList<Window> to hold the stack of windows on the monitor. Each node in the linked list will contain a single window.

  1. When a user clicks on a window in the stack, it needs to brought to the front, keeping the other windows in the same order. The handleClick(int x, int y) method in the WindowList class is called by the WindowsMonitor class when there is a mouse click at position (x,y). Write the method body, where you can assume you have access to the class variable windows of type MyLinkedList<Window>, and that a Window has a method contains(x,y) that returns true if the Window contains the point (x,y). You should state clearly whether you are assuming that the top window is at the front or rear of your linked list.




SquareList

Your second task is to write a class SquareList to represent a list of squares on a given window. As such, your SquareList class will contain a class variable MyLinkedList<Square> to hold the list of squares in the window. Each node in the linked list will contain a single square.

  1. When a user clicks on a point (x,y) on a window, we either need to remove all squares that contain that point, or if there are no such squares, create one. The handleClick(int x, int y) method in the SquareList class is called by the WindowList class when there is amouse click at position (x,y). Write the method body, where you can assume you have access to the class variable squares of type MyLinkedList<Square>, and that a Square has a method contains(x,y) that returns true if the Square contains the point (x,y).



  2. Briefly describe 3 test cases you think are important to test the WindowsManager program. For example, you may want a test to check whether you correctly delete windows from the start of the list, or whether you are able to add a square to an empty list.