You are going to design and implement a class that represent

You are going to design and implement a class that represents an individual tweet (from Twitter). In the next project, you will be designing a system that can manage lots of tweets, so this class will be used again. Besides a Tweet class, you will also be implementing a class that reads a tweet from a file, and another that reads tweets and finds the longest one. I have a small data file with tweets from last weekend recorded in it. Each tweet is on one row. There are 3 fields: the user id, the date/time, and the actual text. There is a tab character (‘\\t’) separating each field. Here is a sample lkacoy Sat Oct 01 22:57:15 +0000 2016 RT @GoNUmhockey: Starters for the Huskies:N. Stevens - J Stevens - Aston-ReeseShea - CockerillRuck#GoNU | #RedBlackOnePack ShadiaFritz Sat Oct 01 22:57:16 +0000 2016 RT @GossipGirltbh: Leighton Meester is perfection https://t.co/090Wboj3Je airborne188 Sat Oct 01 22:57:16 +0000 2016 BBC News - Nuclear submarines construction set to start https://t.co/XSCL41K30F Your first task is to design a Tweet class with instance variables that represent those same fields. The user id and text fields will be strings. The date/time field must be LocalDateTime. This is a new built in class, introduced in Java8. You will need to import it with this statement import java.time.LocalDateTime; To declare an instance variable of this type, use private LocalDateTime tweetDate; Your class will need setters and getters, and an additional method called numChars that returns the number of characters in the text of the tweet. You will also need a constructor, with this header public Tweet(String uid, LocalDateTime aDate, String tweet) In order to test your Tweet class, I am giving you a simple test program called TweetTest.java. It simply tests that your constructor and get methods work. Make sure this test works correctly before going on to the rest of the program. This will be due on Oct 11. You also a class, TweetReader to read in one tweet from the file. It needs to have one method, with this signature. public Tweet read(Scanner in) This method reads one line of the file, and creates and returns a Tweet object from the file input. If there is no data to read in the file, it should return null. To read from the file, read one line from the Scanner: 2 String line = in.nextLine(); Then, use StringTokenizer to break up the line into pieces. The delimiter is the tab character (‘\\t’); StringTokenizer tokenizer = new StringTokenizer(line,\"\\t\"); String uid = tokenizer.nextToken(); Take a look at Task.java and TaskReader.java for an example. Once you have read in the strings, create a Tweet object using those values. The only complication is that the Tweet constructor will expect a LocalDateTime for the date. You will need to convert the string representation of the date to a LocalDateTime. This is how to do it. In this example, dt is the String representation of the date, as read in by nextToken. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(\"EEE MMM dd HH:mm:ss xxxx yyyy\"); LocalDateTime dateTime = LocalDateTime.parse(dt, formatter); I have created another test program. TweetReaderTest.java, for your TweetReader class. Use it to test your TweetReader and make sure it works before you go to the next phase. You will need two files, tweets.txt, and tweetsEmpty.txt, for testing. Place these two files at the top level of your project. Making TweetReaderTest work is the second phase of the project, due on Oct. 20. In the last phase of this project, you will create another class, called TweetProcess, with a main method. This main method will read all of the tweets in the file, one by one, and will print out the total number of characters in all the tweets, the average number of characters, as well as the user id associated with the shortest tweet. To read all of the tweets, use a loop with this set of steps Tweet tweet= reader.read(in); while (!(tweet == null)) { // do all processing // read another tweet= reader.read(in); } The output should look like this the total number of characters in the tweets: 39379 the average number of characters in the tweets: 79.8762677484787 The shortest tweet was sent by GarikGoldwater and is 11 chars long

I do not have the TweetReader Class but I do have the TweetReader Test

This is the TweetTest:

This is the TweetReaderTest:

Solution

import java.util.ArrayList;

import twitter4j.MediaEntity;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.URLEntity;
import twitter4j.UserMentionEntity;
import twitter4j.conf.ConfigurationBuilder;


public class TweetReader {
  
   private ConfigurationBuilder cb;
   private Twitter twitter;

   // Twitter oAuth stuff ... taken from the \"Manage Apps\" page of dev.twitter.com
   private static final String CONSUMER_KEY = \"hPPFrBezXdmMJmiGHDsItRjro\";
   private static final String CONSUMER_SECRET = \"hOcFWHFX4muCtyoGrkXtUeDZFF0A2x4Q0b5cOGkXyuKHk8ul5Z\";
   private static final String ACCESS_TOKEN = \"236562313-GlTz1MinsFwBuUgofBvGUB9IQ9FYMOrNwjYzW4FL\";
   private static final String ACCESS_TOKEN_SECRET = \"2rBnIpZWbnGnKhLjfDoGNbsNnheiTOKLacLtnPKAcwAph\";

  
  
   public TweetReader(){
       cb = new ConfigurationBuilder();
       cb.setOAuthConsumerKey(CONSUMER_KEY);
       cb.setOAuthConsumerSecret(CONSUMER_SECRET);
       cb.setOAuthAccessToken(ACCESS_TOKEN);
       cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);

       twitter = new TwitterFactory(cb.build()).getInstance();
   }

   /**
   * Sends tweet from @madelinegannon account to another user.
   *
   * @param user
   * @param msg
   */
   public void tweetAt(String user, String msg){      
       try {
           twitter.updateStatus(user+\" \"+msg);
       } catch (TwitterException e) {
           e.printStackTrace();
       }
   }
  
   /**
   * Searches for tweets with a particular keyword.
   *
   * @param keyword
   * @param count
   * @return List of each tweet as a list of words.
   */
   public ArrayList<ArrayList<String>> searchFor(String keyword, int count){
      
       ArrayList<Status> tweets = new ArrayList<Status>();
      
       Query query = new Query(keyword);
       query.setCount(count);

       try {
           QueryResult result = twitter.search(query);
           tweets = (ArrayList<Status>) result.getTweets();      
       }catch (TwitterException te) {
           System.out.println(\"Couldn\'t connect: \" + te);
       }  
      
       return parseTweets(tweets);      
   }
  
   /**
   * Returns a list of tweets from a user.
   *
   * @param user
   * @return - List of each tweet as a list of words, from a giving user.
   */
   public ArrayList<ArrayList<String>> getTweetsFromUser(String user){
      
       ArrayList<ArrayList<String>> tweets = new ArrayList<ArrayList<String>>();
      
       // get the list of past tweets from a user
       ResponseList<Status> statuses;
       try {
           statuses = twitter.getUserTimeline(user);
//           for (Status s : statuses){
           Status s = statuses.get(0);
               tweets.add(parseTweet(s));
//           }
                  
       } catch (TwitterException e) {
           e.printStackTrace();
       }
      
       return tweets;
   }
  
   /**
   * Get the parsed text from each status.
   *
   * @param statuses
   * @return List of each tweet as a list of words.
   */
   private ArrayList<ArrayList<String>> parseTweets(ArrayList<Status> statuses){
       ArrayList<ArrayList<String>> tweets = new ArrayList<ArrayList<String>>();
      
       for (Status s : statuses)
           tweets.add(parseTweet(s));
      
       return tweets;
   }
  
   /**
   * Removing URLs and RTs from tweet text.
   *
   * @param s - status object for tweet
   * @return List of each word in tweet.
   */
   private ArrayList<String> parseTweet(Status s) {
      
       String originalTweet = s.getText();
       String tweet = originalTweet;
      
       // remove urls from tweet
       for (URLEntity entity : s.getURLEntities())
           tweet = tweet.replace(entity.getText(),\"\");
       for (MediaEntity entity : s.getMediaEntities())
           tweet = tweet.replace(entity.getURL(),\"\");
      
       // remove RTs
       for (UserMentionEntity ume : s.getUserMentionEntities()){
           if (tweet.contains(\"RT @\"+ume.getText()+\": \"))
               tweet = tweet.replace(\"RT @\"+ume.getText()+\": \",\"\");
       }
      
       // remove any extra spaces
       String[] words = tweet.split(\" \");
       ArrayList<String> msg = new ArrayList<String>();
       for (int i=0; i<words.length; i++){
           if (!words[i].equals(\"\"))
               msg.add(words[i]);                                  
       }
       System.out.println();
       System.out.println();
       System.out.println(msg);
       return msg;
   }
  
}

You are going to design and implement a class that represents an individual tweet (from Twitter). In the next project, you will be designing a system that can m
You are going to design and implement a class that represents an individual tweet (from Twitter). In the next project, you will be designing a system that can m
You are going to design and implement a class that represents an individual tweet (from Twitter). In the next project, you will be designing a system that can m
You are going to design and implement a class that represents an individual tweet (from Twitter). In the next project, you will be designing a system that can m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site