Lab 4

Due September 30, 2011 - 11:59pm

    1. Implement a program that reads the contents of a file such as the dracula.txt file attached at the bottom of the page and builds a HashMap of ArrayLists that stores a mapping from each word in the document to a list of the locations where the word occurs. The requirements of the program are as follows:
      1. The user will enter the name of the file to be processed.
      2. Your program will process the file and generate the HashMap of ArrayList objects. For example, if your document contained the following text "The cat and the dog ran." your HashMap would have 5 key, value pairs where the keys would be as follows: {the, cat, and, dog, ran}. The value for 'the' would be an ArrayList containing the integers 1 and 4 since the word 'the' appears at positions 1 and 4 in the document. The value for 'cat' would be an ArrayList containing the integer 2, and so on.
      3. Once the data structure has been built, allow the user to repeatedly (in a loop) enter search words. For each word entered, print a list of the locations where the word occurs and then prompt for the next word.
      4. Hint: you can remove punctuation characters by calling the following method on a string and saving the result: replaceAll("\\W", "") In this example, \\W is a regular expression specifying any non-word charcater. Any non-word character is replaced with nothing. You can also use the String toLowerCase method to convert the entire word to lower case.
    2. Implement a program that repeatedly (in a loop) prompts the user for an email address and sends a pre-configured message to each address entered by the user. The user should be able to quit the program by entering 'quit' to end the loop. Hints:
      1. You need to add two properties to the Properties object you create (see the Project 2 description). The Properties object is essentially a HashMap that stores key, value pairs representing relevant properties associated with the message. The first property you need to set is the mail server. The key for this property should be "mail.smtp.host" and the value should be "smtp.usfca.edu". The second property you need to set is the sender. The key for this property should be "mail.from" and the value should be your email address.
      2. You will need to import the following classes:
        1. import java.util.Date;
        2. import java.util.Scanner;
        3. import java.util.Properties;
        4. import javax.mail.Message;
        5. import javax.mail.MessagingException;
        6. import javax.mail.Session;
        7. import javax.mail.Transport;
        8. import javax.mail.internet.MimeMessage;

Submission

Please submit your work in an SVN directory https://www.cs.usfca.edu/svn/<username>/cs112/lab4.

Submission Instructions