To Lecture Notes

IT 313 -- Mar 5, 2020

Review Exercises

  1. Write the getCheckedOut method of the LibraryManager for Project 6b. Ans:
    public ArrayList<LibraryItem> getCheckedOut( ) {
        ArrayList<LibraryItem> output = new ArrayList<LibraryItem>(20);
        for(LibraryItem item : output.values( )) {
            if(item.isCheckedOut( )) {
                output.add(item);
            }
        }
        Collections.sort(output);
        return output;
    }
    
  2. Explain how the State pattern works. Give an example.
    Ans: The State pattern creates one object in the state diagram. For example, for the Gumball State Diagram, the classes are
    HasQuarterState  NoQuarterState  SoldState  SoldOutState
    
    A simpler example is this Two Floor Elevator State Diagram. The states could be represented by the classes FirstFloorState and SecondFloorState. The two actions "Press Button 1" and "Press Button 2" shown on the state diagram are the meaningful actions. Other actions such as "Press Button 1" if the elevator is already at Floor 1 or "Press Button 2" if the elevator is already at Floor 2 are not meaningful and do not result in a state transition. Of course the controller for an elevator is probably not implemented with Java classes.
  3. In the observer pattern, what is the difference between the observable and the observer? Look them up in the Observer Example, list the methods for each.
    Ans: Observable is what is being observed, such as a Twitter Feed. Observer is what is observing the Observable, for example the Twitter followers. Observable is a base class; Observer is an interface. The Observable methods are addObserver, clearChanged, countObservers, deleteObserver, hasChanged, notifyObservers, setChanged. The required method of Observable is update.
  4. Explain the similarities and differences between the classical Observer pattern and a dentist's office and its patients being notified of upcoming appointments.
    Ans: The dentist's office is the Observable and the patients are the Observer objects. For a patient to become an Observer, the receptionist must record the patient in the appointment book. Then the day before the appointment, the patient gets a call that he or she has an appointment the next day. The analogy breaks down a little because an Observable notifies all the patients at the same time when notifyObservers gets called. However, the receptionist does not notify all patients at the same time. They are notified individually the day before their appointment occurs.
  5. Explain how the Strategy Pattern works. Give an example.

SQL (Standard Query Language)

The SQLite Relational Database Management System

Java and SQL