CSCI 151 - Lab 4 - Spring 2008 Email directory

11:59.59pm, Sunday, March 9

NOTE: I'm still waiting on the college for more/better directory information. You'll probably want to put off working on the application section until lab time as there may be some last minute changes. I've not heard back from the college, so go ahead with the data as-is.

NOTE: We're working on a way to easily share files between you and your partner. It should be working by the end of Monday, so during lab today, just use one person's account and look for an update in this space.

Project Description

This week we are going to implementing a doubly linked list and using it to run an email directory application. You'll also implement your first "real" iterator and can use it in your project.

The directory information that I'm supplying has been culled from a number of campus sources, but should not be made available to anyone outside this class. This includes posting it on web sites.

You may work with a partner on this assignment. I know that this means that some groups will have 2 people working on pieces individually instead of as a group. I'd like it if you initially sit down together and discuss the design of the overall program before splitting up the work instead of just trying to meld the two pieces together at the end.

Getting started

Since I don't want the data files distributed outside the class, you'll need to copy the jar file from a lab machine instead of downloading it.

    cp ~kuperman/pub/cs151/lab4.jar .

Part 1 - creating a doubly-linked list

You'll be creating a doubly linked list called MyLinkedList and an iterator for that class.

MyLinkedList

This is just a subset of LinkedList and should have the same behavior for the methods you write.

You should extend AbstractList and implement the Iterable interface.

Be sure to test your implementation thoroughly. In doubly linked lists, the removal of items can be especially tricky as you need to be sure to properly update the neighbor's next and previous pointers, as well as handling the special cases for removal from the front or tail.

We will not be permitting "null" pointers to be inserted into the list. You should throw a NullPointerException if someone attempts to do so.

Constructors

You only need a 0-argument constructor that creates an empty list.

Private Methods

Create a getNth() method that returns the node at a specified index, not the content.

Public Methods

boolean add(T data)
void add(int index, T data)
add an element into this list (either at end or by index)
throw a NullPointerException if the user tries to add a null pointer
throw IndexOutOfBoundsException as needed
T get(int i)
get object at position i
throw IndexOutOfBoundsException as needed
T set(int i,T data)
set the value at index i to data
throw NullPointerException if data is null
throw IndexOutOfBoundsException as needed
T remove(int i)
remove the element from position i in this list
throw IndexOutOfBoundsException as needed
void clear()
remove all elements from the list
boolean isEmpty()
determine if the list is empty
int size()
return the number of elements being stored

Additional methods

These are methods that weren't in MyArrayList, but might be useful in a LinkedList.

T getFirst()
return the first element in the list
throw NoSuchElementException if the list is empty
T getLast()
return the last element in the list
throw NoSuchElementException if the list is empty
T removeFirst()
remove and return the first element in the list
throw NoSuchElementException if the list is empty
T removeLast()
remove and return the last element in the list
throw NoSuchElementException if the list is empty
void addFirst(T data)
insert the item at the front of the list
throw a NullPointerException if the user tries to add a null pointer
void addLast(T data)
insert the item at the end of the list
throw a NullPointerException if the user tries to add a null pointer

Iterator

There is a skeleton of an iterator factory method included in the file. You need to implement hasNext and next in order to make it work.

Programming hints

You'll probably want to create a nested class to represent a node in your linked list. Remember that to do this, you can't declare the class as public. Also, since you'll need to be modifying these nodes, you might want to consider creating private methods that return a reference to a node instead of the data inside the node.

Testing

You should be able to re-use the tests you wrote in Lab 1 for MyArrayList. Just make changes similar to

    List<String> x = new MyLinkedList<String>();

Part 2 - Directory

For this part of the project, I want you to create a directory application that will let you have a personal list of email addresses as well as the ability to query the whole student directory.

DirectoryEntry

You first are going to create a class that can hold an entry and has methods that make it useful for searching. The fields that must be supported are:

  1. First name: a person's first name
  2. Middle name: a person's middle name
  3. Last name: a person's last name
  4. Degree: degree being worked towards
  5. Major: primary declared major
  6. Rank: class (e.g., Junior)
  7. Email: their full email address
  8. Dorm: their residence hall

And you may add to these if you want. However, for the equals() method, these are the only fields that matter.

NOTE: for your own sanity, you should not permit any field to contain the ':' character. Check for them and either drop them, transform them into a valid character, or truncate the string at that point. Similarly, you should check that the user doesn't input any '%' characters in inputs other than those for searching

The equals() method

For the equals() method, I want you to apply the following rules:

  1. Matches must be made in case-insensitive fashion. However, the case that is displayed to the user must allow for mixed case.

    "Benjamin" matches "BeNjAmIn" but prints differently

  2. A blank string (i.e., "") should match anything. So, if there is no major entered, you should allow any major to match.
  3. A string that ends in '%' should be used to match from the beginning up to, but not including the '%' [see String.startsWith()], all other strings should match for the full string. For example
            "Ben"  doesn't match "Benjamin"
            "Ben%" matches       "Benjamin"
            

Directory

Now create a class Directory.java that will act as your interface to manipulating these DirectoryEntry objects (which you'll be storing in MyLinkedLists).

Command line arguments

If two arguments are provided, use these values for the filenames of the personal and directory files.

    % java Directory <personal.txt>  <directory.txt> 

If one is specified, use it for the personal file, and assume "directory.txt" for the other.

    % java Directory <personal.txt> 

If none is specified, use "directory.txt" for the directory and "personal.txt" for the personal.

    % java Directory 

The directory file should be considered a read-only list similar to your phone book -- don't add or remove from it.

The personal file (personal.txt) contains your personal address book. There may be duplicates between this file and the directory.

Input file format

Entries are to be stored between runs of the program in flat text files. The format of the files is one entry per line with fields separated by ':' characters.

    First:Middle:Last:Degree:Major:Rank:Email:Dorm

For splitting the line, you might want to look at String.indexOf() methods or String.split().

Note that there will be many entries with blank values such as no dorm or middle name listed.

Commands to be supported

Your Directory program should support the following operations. You may implement additional functions for consideration for extra credit.

  1. Save personal list to a file -- only save when the user requests it

    Here's how to create a PrintWriter

    PrintWriter outFile=new PrintWriter(
                        new BufferedWriter(
                        new FileWriter("personal.txt"))); 

    and now you can use 'outFile' just like 'System.out'

    outFile.println("This line will be in personal.txt");
    outFile.print("this has no newline"); 

    Note: when you are done writing to your file, you need to close it. Normally, this is done when your program exits, but students have been reporting problems with this.

    outFile.close();
  2. Load personal list from a file -- revert back to the most recently saved copy
  3. Add a new entry
  4. Search the personal list
  5. Delete from the personal list
  6. Edit an entry
  7. Query the main directory
  8. Display the full personal list of addresses with index numbers
  9. Allow the user to swap a pair of items in the list based on index numbers

Programming hints

Recall that you are implementing MyLinkedList so that it has a subset of the java.util.LinkedList methods. This means that you can build and test your Directory application separate from the MyLinkedList program. However, you need to first write the DirectoryEntry class that you will be using.

Notice how there are many similar actions in the above? Perhaps you can create methods to take care of such instead of having duplicated code.


handin

Look through your programs and make sure you've included your name at the top of all of them.

README file

You need to create a file called "README" that contains the following information:

  1. Your name
  2. Any known problems or assumptions made in your classes or program
  3. Anything you've implemented for extra credit

If you have suggestions for improvements to this lab, feel free to add them to this file too.

Honor code

If you adhered to the honor code in this assignment, add the following statement to your README file:

I affirm that I have adhered to the Honor Code in this assignment.

handin

You now just need to electronically handin all your files. Assignment is 4.

Don't forget to run lshand and verify that things were submitted.


Lab Goals

This lab is intended to:


Last Modified: March 07, 2008 - Benjamin A. KupermanVI Powered