Project #1348 - XML/Twitter

Assignment 7

For this assignment, you will be working with Twitter timelines. Your system will need to read in a list of Twitter users whose ids are stored in a file in XML format, request the timelines from Twitter, and integrate all of the tweets so that they can be displayed in reverse chronological order.


Format of the User Id File

The file containing the user ids will have the following format:


where the number of user elements is unlimited and X represents a Twitter user id such timoreilly or SteveMartinToGo. You should create your own test user id file. Your assignment will be evaluated using one created by the instructor with above format.


Retrieving a Twitter Timeline

The URL to use to retrieve a user's timeline is the following:<USER ID>

where <USER ID> is replaced with an actual user id. For example,

Your application will need to construct the request URL for each user when a query is made.

NOTE: Twitter imposes a rate limit on the number of requests that can come from a single IP to 150/hour (unless you have authenticated and that is not something we are doing). Once the limit has been reached you will receive an error response from their server until your count is reset. This means you need to provide yourself ample time to complete this assignment. If you are waiting until the last minute and are feverishly trying to debug, it's quite likely you will hit the limit.


What Twitter Data to Include

A timeline consists of a collection of statuses and each status contains a great deal of data. Here is a list of the status related data that needs to be modeled in the application and the rest can be ignored:

  • date posted (created_at), including time
  • tweet text
  • user name
  • user screen name
  • number of times the tween has been retweeted


Java Class Requirements

You application will need to use multiple classes. It is up to you to determine the appropriate classes to model a timeline in a way that makes sense for your application. However, your solution needs to contain the following three classes with at least the operations that are specified. The first is TwitterImporter which is analogous to LastFMImporter from the example.

public class TwitterImporter {
    public static X loadTimeline(String screenName) { //... }

The return type X is a class that you will define. This is the equivalent of the class TopTracks in the example. The second class is the one that handles retrieving the list of users stored in a file,

public class UserImporter {
    public static Y loadUsers(String documentLocation) { //... }

where Y a class you define. Lastly, you will need the test class,

public class T_Twitter {
    public static void main(String[] args) { //... }

which will coordinate the loading of users, loading of timelines, sorting of timelines, and display of the integrated timeline.


The Output

Your output should be written to the console. The tweets in the output should be all the tweets for all of the users specified in the user file listed in reverse chronological order (the newest one first). This means that the users' tweets will be intertwined. Each tweet should be displayed as follows:

Tim O'Reilly (@timoreilly): 
Neal Stephenson on the need for optimism in science fiction I totally agree.
April 20, 2012 - 4:42PM ~ 12 retweets

A single space must separate one tweet block from the next.


Handling Dates

The preferred way of handling points in time in Java using some subclass of the Java class Calendar. For us, we will use the class GregorianCalendar. You will need to translate the date in string form obtained from each status in the timeline to a GregorianCalendar instance and also from GregorianCalendar to a string. To help you along, the operations to handle these translations are provided:

// This utility operation translates a string containing a date in the format 
//    Wed Jun 20 03:15:06 +0000 2012
// to a GregorianCalendar instance.
private static GregorianCalendar convertStringToDate(String createdAtText) {
    GregorianCalendar calendar = new GregorianCalendar();
    Date date;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM d H:mm:ss Z yyyy");
    date = simpleDateFormat.parse(createdAtText, new ParsePosition(0));
    return calendar;

// This operation a string representation of the supplied date. The format of the
// string will look like
//     February 12, 2012 - 12:43PM
private static String convertDateToString(GregorianCalendar date) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM d, yyyy - h:ma");
    return simpleDateFormat.format(date.getTime());

 The operations are declared private and static as they will likely be utility operations declared within a class. However, you are free to change the visibility and/or remove the static declaration if you feel that fits best with your design.


Sorting a Timeline

In order to output the updates from multiple timelines, the straight-forward way is to create master timeline that contains all of the updates sorted from newest to oldest. This should be done in two steps: merge the timelines into a master timeline and then sort the master timeline from newest update to oldest.

The most natural way to merge the timelines is to create the master timeline and then add all of the updates from the first timeline, then add all of the updates from the second timeline, etc. The details of doing the merge are left to you. Sorting the master timeline is a bit trickier. Below is a method based bubble sort:

// This operation returns a timeline that contains the statuses within in provided 
// timeline sorted from newest status to oldest. The provided timeline is expected to 
// be non-null. 
private static Timeline sortTimeline(Timeline timeline) {

    Timeline sortedTimeline = new Timeline();
    Status[] statuses = timeline.getStatuses();
    Status temp;
    boolean swapOccurred = true;
    while (swapOccurred) {
        swapOccurred = false;
        for(int s=0; s < statuses.length - 1; ++s) {
            if (statuses[s].getDatePosted().compareTo(statuses[s+1].getDatePosted()) < 0) {
                temp          = statuses[s];
                statuses[s]   = statuses[s+1];
                statuses[s+1] = temp;
                swapOccurred = true;
    for (int s=0; s < statuses.length; ++s) {
    return sortedTimeline;


The method is declared private and static as it will likely be a utility operation declared within a class. However, you are free to change the visibility and/or remove the static declaration if you feel that fits best with your design.


Also, if you look at the code you will see that the operation expects

  • The class Timeline to have the operations
    public Timeline()
    public Status[] getStatuses()
    public void addStats(Status newStatus)
  • The class Status to have the operation
    public GregorianCalendar getDatePosted()

If you follow the prescribed method for translating an XML structure to a set of Java classes, you will have defined these operations.


Submission Requirements

Your submission for this assignment will consist of a single zip file that contains the Java source code files (no .class files!) and the two text files containing the asset and account data. The files should not be nested in another folder. The files need to compile with a Java 6 compiler and cannot depend on anything more than each other and the Java 6 Library.

Each each class must be contained in its own file and begin with a header of the form

// CLASS  : 
// AUTHOR : 
// ------------------------------------------------------------------------------------------------

You will complete the headers by providing:

  • The name of the class after "CLASS : "
  • Your name and access id after "AUTHOR : " in the form

        AUTHOR : Joe Marks (jam103)
  • A brief descrption of the class under CLASS DESCRIPTION
  • A description of any problems you know exist in your code such as methods that don't work right (explain the shortcomings) or assumptions made that might lead to the class not working correctly under KNOWN LIMITATIONS

Notes on Evaluation

The evaluation of your assignment is in terms of the quality of the content and presentation. For each dimension, below are a set of questions to consider as you are thinking about your solution. The list isn't necessarily complete; you will want to refer back to the lesson content.


  • Did I meet the specified requirements?
  • Did I use the most appropriate data types and classes provided by Java?
  • Are the classes I defined each based on a small number of related responsibilities?
  • Did I making appropriate use of visibility modifiers?
  • Did I make the best use of classes to model the data for the problem?
  • Is there anything I can do to improve the design of my classes and algorithms used?


  • Did I follow the course coding standards?
  • Is my source code well commented?
  • Are my comments complete sentences and use proper grammar and spelling?
  • Is there anything I can do to make the code more elegant?

Subject Computer
Due By (Pacific Time) 12/01/2012 12:00 am
Report DMCA

Chat Now!

out of 1971 reviews

Chat Now!

out of 766 reviews

Chat Now!

out of 1164 reviews

Chat Now!

out of 721 reviews

Chat Now!

out of 1600 reviews

Chat Now!

out of 770 reviews

Chat Now!

out of 766 reviews

Chat Now!

out of 680 reviews
All Rights Reserved. Copyright by - Copyright Policy