P2: Using Instance Variables and
ArrayLists on a User-Defined Class

Due Monday, June 29, 12:00 PM via Checkin
100 points

1. Objectives

The objectives of this assignment are to use (1) use instance variables, and (2) ArrayList of objects. You will build on P1 and add new functionality. You will implement two classes Tweet and TwitterDB. The Tweet class captures each tweet along with the userid and date/time of posting. The TwitterDB class is used to gather information from the tweets as a whole.


2. Tweet class

You will be able to reuse some code from P1 (copy-paste with appropriate modification). However, you must fix an error that was present in the sample code provided for setting up the scanner delimiter. Use the following code instead (note the two backslashes before dash):

useDelimiter("[ *\\-,!?.]+")

Implement the Tweet class in a file called called Tweet.java. You must use the following three instance variables:

The Tweet class must implement the following constructor: The Tweet class must implement the following methods:

3. TwitterDB class

Implement the TwitterDB class in the file TwitterDB.java. You must use the following instance variable:

Implement the following constructor:

Implement the following methods:


4. General Information

None of the methods above should be declared static. In fact, compilation of your program will fail if the method signatures in your program are different than above. To help you develop your program here is a tweets file. Preliminary testing using the online system will be performed using these file. You get results of preliminary testing almost instantaneously using the online submission system. To grade your program (final testing) we will use different files, so do not hardcode any parameters into your program. We encourage you to construct your own tweet files that test various scenarios (e.g., that you successfully ignore case).

Keep in mind that we will not test your main method -- the methods you implement will be tested directly. However, you should use your main to test the methods that you write. A barebones main can include something like:

    public static void main(String[] args) {
        TwitterDB tdb = new TwitterDB("tweets.txt");
        System.out.println("Number of tweets: " + tdb.getNumberOfTweets());
        Tweet t = tdb.getTweet(0);
        System.out.println("Printing t:");
        System.out.println(t);
        System.out.println("User ID: " + t.getUserID());
        System.out.println("Tweet: " + t.getTweet());
        System.out.println("Number of characters: " + t.numChars());
        System.out.println();
        ArrayList tweets = tdb.tweetsBy("USER_989b85bb");
        System.out.println("USER_989b85bb sent how many tweets? " + tweets.size());
        tweets = tdb.tweetsBefore("2010-03-07T18:26:13");
        System.out.println("How many tweets before 2010-03-07T18:26:13? " + tweets.size());
        System.out.println("Most common word: " + tdb.mostCommonWord());
        System.out.println("First tweet that contains \"spirit\": " + tdb.search("spirit"));
    }

You can make the following assumptions about how we will test your methods:

During preliminary testing, your score equals the number of test cases passed. H owever, during final testing, certain test cases may be weighted more than other s. More difficult methods will be worth more points.


5. Submission

This assignment requires you to create two files, but the assignment submission system can only accept one file. Thus, we will need to "combine" the two Java files into one file. Here's how to do this.

Create a single jar file called P2.jar from the two program files using the instructions provided here. Check that you are only combining the source file (i.e., Tweet.java and TwitterDB.java) and not the class files (i.e., Tweet.class and TwitterDB.class) by mistake. Submit the P2.jar file via the online checkin system. This system performs preliminary testing of your program on the same data file above. Final grading will be performed on a different set of files.


6. Acknowledgements

The twitter dataset we provided to you is a subset of a much larger dataset which is available here.