Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Done in Java, any help would be appreciated. I have started on the author question, but cannot figure out how to make it save the
Done in Java, any help would be appreciated. I have started on the author question, but cannot figure out how to make it save the content (everything after author)
Design and implement at least two non-trivial(*) operations in your TweetCollection class, to allow clients to use the collection of tweets, e.g. those operations may be, e.g.:
- getTweets(String content) - returns either either an ArrayList type or LinkedList of TweetClass objects, i.e. all tweets that contain the specified content string (use either ArrayList type or LinkedList, according to what you chose for Part A)
- getTweetsByAuthor(String author) - - returns either either an ArrayList type or LinkedList of TweetClass objects, i.e. all tweets by the specified author
- Test your implementation by writing Java client code (i.e. main() ...) that uses your new TweetCollection class and its methods.
TweetClass.java:
import java.io.IOException; public class TweetClass { String content; String author; static String s = "lmao"; //create constructor- public TweetClass(String content, String author) { this.content = content; this.author = author; } //print content of tweet public void printTweet() { System.out.println(content); } //boolean method contains(s) public boolean contains(String s) { if(content.contains(s)) return true; else return false; } public static void main(String[] args) throws IOException { TweetCollection tweeter = new TweetCollection(); tweeter.readURL(); //print or not found TweetClass specificAuthor = tweeter.getTweetByAuthor("Appterra"); if(specificAuthor == null) specificAuthor.printTweet(); else System.out.println("Not found!"); } }
TweetCollection.java:
import java.io.IOException; import java.net.URL; import java.util.*; public class TweetCollection { //instance variables public TweetClass[] tweets; private int size; private String author; private String content; //constructor public TweetCollection(){ tweets = new TweetClass[1000]; size = 0; } public void readURL(){ try { // Step 1: create a scanner System.out.println("we're going to create a scanner"); // From a URL URL url = new URL("http://homes.soic.indiana.edu/classes/fall2020/csci/c343-mitja/test2020/tweet-data-September10.txt"); System.out.println("obtained a URL"); Scanner in = new Scanner(url.openStream()); System.out.println("scanner created"); // From System.in (user's inputs) // Scanner in = new Scanner(System.in); // From a local file (e.g., tweet-data-September10.txt on your local machine) // Scanner in = new Scanner(new FileReader("tweet-data-September10.txt")); // Step 2: read data int i = 0; while (in.hasNext()) { //nextLine() reads a line; //Scanner class has other methods to allow the user to read values of various types, eg.., nextInt() String str = in.nextLine(); System.out.print("at line "); System.out.print(i); System.out.print(" there is ["); System.out.print(str); System.out.println("]"); i = i + 1; } System.out.println(); //Step 3: close the scanner in.close(); } catch (Throwable e) { System.out.println("exception is "+ e); e.printStackTrace(); } } public TweetClass getTweetByAuthor(String author) throws IOException { URL url = new URL("http://homes.soic.indiana.edu/classes/fall2020/csci/c343-mitja/test2020/tweet-data-September10.txt"); System.out.println("obtained a URL"); Scanner in = new Scanner(url.openStream()); System.out.println("scanner created"); //loop to keep authors while (in.hasNext()) { author = in.next(); in.nextLine(); System.out.println(author); }//while for(int i = 0; i < size; i++) { if(tweets[i].author.equals(author)) return tweets[i]; }//for return null; }//author }
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