Question
Note that each line of the file is formatted as follows: latitude longitude 6 year-month-day hour:minute:second text of tweet For instance, the first tweet is
Note that each line of the file is formatted as follows:
latitude longitude 6 year-month-day hour:minute:second text of tweet
For instance, the first tweet is as follows:
41.386263640000003 -81.494450689999994 6 2011-08-28 19:02:28 Yay. Little League World Series!
Your assignment is to create a program which builds a linked list of tweets based on an initial search term and then allows the user to narrow the search by specifying another term. To do this, you will need to write three class files.
The Tweet Class
Write a Tweet class that you will use to keep track of all the information about a given tweet such as latitude, longitude, year, month, day, hour, minute, second, and text of the tweet. The Tweet classs constructor Tweet(String s) has a String parameter s that represents a line from tweetdata.txt. This constructor will need to use a Scanner in order to get pieces of information that are contained within s. For example, you might declare the Scanner like this:
Scanner scan = new Scanner(s);
When using scan, you will need to parse doubles, ints, and Strings. To do this, you will need to make use of the nextDouble, nextInt, next, and nextLine() methods. One problem that you will need to work through is how to take apart year, month, and day from year-month-day in each line. To do this, try storing year-month-day inside of a string by calling next, like this:
String date = scan.next();
Then call dates split method, which splits year-month-day into three different Strings and stores
them in an array. Like this:
String[] d = date.split("-");
You can then convert each String from d into an int and store them in their respective variables.
For example, to store the year inside the year variable, do this:
year = Integer.parseInt(d[0])
Your Tweet class should have a public method called print that prints a tweet in a more readable format like this:
Text: Yay. Little League World Series!
Location: 41.3863, -81.4945
Date: 8/28/2011
Time: 19:2:28
Your Tweet class should also have a public method named textContains which can be called to determine if the text of the tweet contains a given substring. For instance, if I called textContains on the example tweet above with the search term Little it would return true, but if I called it with Major it would return false.
Since the tweet text is stored as a String, you will want to make use of the indexOf method, which tells you if a particular substring exists within another String. For example, if my String variable is called text and the substring Im searching for in text is s, then text.indexOf(s) returns the position within text where s can be found. If text doesnt contain s, then text.indexOf(s)
returns -1. Thus, you might implement textContains by doing something like this:
public boolean textContains(String searchTerm)
{
return !(text.indexOf(searchTerm) == -1);
}
The TweetList Class
You need to create a class called TweetList which implements a linked list of Tweets. This class will need a constructor and the following methods:
1.a public method called print for printing all of the tweets in the list
2.a public method called prepend for adding new Tweets to the list (use the O(1) prepend technique that we discussed in class)
3.a public method called size for returning the number of items in the list
4.a public method called filter for removing all tweets that do not match the given search criteria keyword. For example, if filter takes the argument hello, then it will remove every tweet from the list that does not contain the string hello in its text. This method is similar to (but not the same as) the remove method from the StringList class.
The TweetListSearcher Class
Create a new class called TweetListSearcher inside of the class only contains a main method. This method should ask the user to specify the name of the tweet data file as well as a search term to use. You should loop through all of the tweets in the data file and prepend any of them that contain search term to your TweetList. You should then tell the user how many tweets matched their search and allow the user to either print the list or search within the results.
WARNING! Do not use a Scanner to scan the tweet data file. Unfortunately, Java scanners do not work very well when reading large text files. Instead, you need to declare and initialize a FileReader,
FileReader file = new FileReader(fname);
where fname is a String that represents the tweet data file you want to read. A FileNotFoundException will be thrown if fname is not in your project folder. You should then give file to a BufferedReader, like this:
BufferedReader read = new BufferedReader(file);
The BufferedReader variable read works kind of like a scanner. When you want to read a line from the tweet data file, you call reads readLine method. You can also use this with a while-loop to read through the entire file until you reach the end.
while ((line = read.readLine()) != null)
Note that an IOException may be thrown when read.readLine() is called, since it is possible that some kind of read error may occur. To properly handle the exceptions that might arise, you should put any code related to FileReader and BufferedReader inside of a try block followed by two catch blocks. For example, you might do something like this:
try
{
FileReader file = new FileReader(fname);
BufferedReader read = new BufferedReader(file);
System.out.println("Enter a word or phrase to search for: ");
String search = keyboard.nextLine();
String line;
while ((line = read.readLine()) != null)
{
// Add tweets to your TweetList here
}
read.close();
}
catch (FileNotFoundException e)
{
System.out.println("The file " + fname + " has not been found.");
}
catch (IOException e)
{
System.out.println("An error occurred while reading " + fname + ".");
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started