COMP 1006 - Assignment 7

Due : Monday March 18, 2013 at 9pm

Submit a single zip file called userID.assignment7.zip to cuLearn with all of your solutions to the following problems. Submit early and submit often. Here, userID is your Carleton userID number.


Using Java's built-in ADTs

In this assignment you will use some of Java's ADT classes that are rooted at the Collection class or the Map class (See page 203 in the course notes.)
  1. PriorityQueue: Create a class called ER, that will simulate an emergency room waiting list. You class will need to have the following methods:

    public void addPatient(String name, int severity, Date time)
    // Purpose: adds a person to the waiting list in the emergency 
    //          room.  
    // Preconditions: name is not null
    //                severity is an integer in the range [1,17]
    //                time is the current time
    // Postconditions: the person is added to the emergency room
    //                 waiting list.  The "priority" in the list is 
    //                 based on severity (1 being least important and
    //                 17 being most important) first and for patients
    //                 with equal severity, based on time (FIFO).
    
    public String nextPatient()
    // Purpose: extract the patient in the waiting list with 
    //          the highest priority
    // Preconditions: none
    // Postconditions: if there is at least one person in the 
    //                 waiting list, it returns the name of the 
    //                 person with the greatest priority, and 
    //                 also removes that person from the list.
    //                 If the waiting list is empty, 
    //                 returns the string "Empty".
    
    public boolean updatePatient(String name, int severity, Date time)
    // Purpose: change the priority of a patient in the waiting list
    // Preconditions: name is not null
    //                severity is in the range [1,17]
    //                time is the current time
    // Postconditions: If a patient with the given name is on the waiting
    //                 list then their priority is updated, based on the 
    //                 new severity and time; and returns true.
    //                 If a patient with the given name is not on the 
    //                 waiting list then returns false.
    
    public ER()
    // Empty constructor to create an empty waiting list
     
    
    You will need to create another class (that implements Comparable) to store your patients. Your waiting list will be a priority queue of patients. Your classes can extend or implement whatever you need to help with your code.

    Do not add a main() method to your ER class (or your patient class).

    Submit your ER.java file and any other *.java files (other classes) you created for this problem (in your zip file).

    Notes:


  1. ArrayList: Create a class called Words that stores a collection of words using an ArrayList. (A "Word" will be any string.) The class should have the following methods:

    public void vowelSort()
    // Purpose: sorts the collection of words (by the number of vowels)
    // Preconditions: none
    // Postconditions: the collection of words will be sorted
    //                 in decreasing order, based on the number
    //                 of vowels in the word.
    //                 vowels = {a,e,i,o,u,y}
    
    public void printWords()
    // Purpose: display all the words in the collection
    // Preconditions: none
    // Postconditions: displays all the Words in the collection
    //                 to the terminal (System.out)
    
    public void addToMiddle(String word)
    // Purpose: add a word to the collection
    // Preconditions: word is not null
    // Postconditions: add the word to the collection of words in the 
    //                 "middle" of the collection.  
    
    public String getLeastVowels()
    // Purpose: removes a word from the collection
    // Preconditions: none
    // Postconditions: If there is at least one word in the 
    //                 collection, it returns a word with 
    //                 minimal number of vowels in it, and removes
    //                 it from the collection.
    //                 If there are no words in the collection,
    //                 returns null.        
    
    public Words()
    // zero parameter constructor to create
    // and empty collection of words
    
    public Words(String[] words)
    // constructor that creates a collection
    // of words given in the words array
    // Preconditions: words is not null, words.length > 0
    //                all Strings in words are not null
    // Postconditions: All the strings in words are now 
    //                 in the collection of words
      
    

    Your classes can extend or implement whatever you need to help with your code.

    Submit your Words.java and any other java files (classes) you need for this problem (in your zip file).


  1. HashMap: Create a class called PhoneBook. Your class will store a collection of key-value pairs, where the keys are Strings (names) and the values are Info objects (which you will also create).

    The Info class must store a phone number and an email (both Strings).

    Your PhoneBook class must have the following methods:

    public boolean add(String name, String phone, String email)
    // Purpose: adds a key-value pair to the collection
    // Preconditions: name, phone, email are all not null
    // Postconditions: add the key-value pair with
    //                      key = name
    //                      value = Item with phone,email
    //                 to the collection.
    //                 If name is already a key in the collection, this 
    //                 key-value pair overwrites the existing one, 
    //                 and the method returns true.
    //                 If name is not already a key in the collection,
    //                 adds the key-value pair and returns false.
    
    
    public String getPhone(String name)
    // Purpose: retrieve the phone number for person with key = name
    // Preconditions: name is not null
    // Postconditions: if name is a key in the collection, return the 
    //                 phone number for that person
    //                 if name is not a key in the collection, 
    //                 return null
    
    public String getEmail(String name)
    // Purpose: retrieve the email number for person with key = name
    // Preconditions: name is not null
    // Postconditions: if name is a key in the collection, return the 
    //                 email for that person
    //                 if name is not a key in the collection, 
    //                 return null
    
    public String[] findName(String phone)
    // Purpose: retrieve the names of all persons with given phone number
    // Preconditions: phone is not null
    // Postconditions: returns an array of all names that are keys
    //                 with value containing the given phone number.
    //                 returns null if there is no key with a value
    //                 containing the given phone number
    
    public PhoneBook()
    // zero parameter constructor to create an empty phone book
    

    Your classes can extend or implement whatever you need to help with your code.

    Submit your PhoneBook.java and your Info.java files for this problem (in your zip file).