Part 1 - Tweet Bot To start analyzing tweets, we first need to read in and manage the state of tweets. To do this, create a class called TweetBot. A TweetBot should have the following constructor and methods and you should not define any other public methods for the TweetBot: Constructor public TweetBot(List tweets) Given a List of tweets, initialize a tweet list containing all tweets from the given collection. Note that you should not initialize your tweet list to the given List reference. You should create a new data structure and copy over all of the tweets from the given List, leaving the given List unmodified after the constructor is finished executing. Throws an IllegalArgumentException if the size of the given collection is less than 1. You may assume the given collection contains only non-empty and distinct strings. public int numTweets() Returns the number of tweets currently in the tweet list. public void addTweet(String tweet) Adds the given tweet to the end of the tweet list. You may assume that the given tweet does not already exist in the TweetBot's list of tweets. public String nextTweet() Returns the next tweet from the tweet list. If all the tweets in the list are exhausted, cycle around to the start of the list. For example, the following code snippet should produce the output in the comments. Suppose that tweets stores a list of Tweets ["A tweet about something controversial", "Remember to vote!", "Look at this meme :O"]. TweetBot bot = new TweetBot(tweets); System.out.println(bot.nextTweet()); // A tweet about something controversial System.out.println(bot.nextTweet()); // Remember to vote! System.out.println(bot.nextTweet()); // Look at this meme :O System.out.println(bot.nextTweet()); // A tweet about something controversial Think about how to get the next tweet from the list of tweets, and how you can cycle around to the start once you've gone through all the tweets! Special Case! As you're working on your TweetBot and testing your implementation, you'll likely start thinking about different cases that your TweetBot will need to handle. There is one specific special case whose behavior is particularly tricky, so we wanted to talk about it more in depth. Consider the case where your TweetBot is storing a sequence of 3 tweets, and you call nextTweet() on your TweetBot 3 times. Then, you immediately afterward add another tweet before calling nextTweet() again. On the next call to nextTweet(), your TweetBot should return the tweet that was just added -- NOT the first tweet. The pseudo-code example below demonstrates this. TweetBot bot = new TweetBot(["A", "B", "C"]); bot.nextTweet(); // A bot.nextTweet(); // B bot.nextTweet(); // C bot.addTweet("D"); bot.nextTweet(); // D Note that the case described here is a tricky one - really there are couple of different ways that a TweetBot could reasonably handle this situation. But, since you'll be writing code to pass tests that we provide, we're telling you how you should handle this situation for this assignment! public void removeTweet(String tweet) Removes the given tweet from the tweet list if it is present. Pay particular attention to making sure that the remove method should preserve the current iteration order of the nextTweet method above. In particular, using the example tweets from the last method, the following code should produce the output shown in the comments. TweetBot bot = new TweetBot(tweets); System.out.println(bot.nextTweet()); // A tweet about something controversial System.out.println(bot.nextTweet()); // Remember to vote! bot.removeTweet("Remember to vote!"); System.out.println(bot.nextTweet()); // Look at this meme :O System.out.println(bot.nextTweet()); // A tweet about something controversial System.out.println(bot.nextTweet()); // Look at this meme :O In other words, the nextTweet method should continue the iteration through the Tweet list, returning the Tweet immediately following the one that was last returned by nextTweet, continuing with only the removed tweet cut out of the cycle. Be careful about removing a tweet that is not in the TwitterBot. In this case, the state of the TwitterBot should be unchanged. public void reset() Resets the iteration state of the TweetBot such that subsequent calls to nextTweet start back at the beginning. i have code but it is only passing 10/15 test case and the nexttweet and reset method are nto working accordingly, i guess it it not returning the most recently added tweet
Part 1 - Tweet Bot
To start analyzing tweets, we first need to read in and manage the state of tweets. To do this, create a class called TweetBot. A TweetBot should have the following constructor and methods and you should not define any other public methods for the TweetBot:
Constructor
public TweetBot(List<String> tweets)
Given a List of tweets, initialize a tweet list containing all tweets from the given collection. Note that you should not initialize your tweet list to the given List reference. You should create a new data structure and copy over all of the tweets from the given List, leaving the given List unmodified after the constructor is finished executing.
Throws an IllegalArgumentException if the size of the given collection is less than 1.
You may assume the given collection contains only non-empty and distinct strings.
public int numTweets()
Returns the number of tweets currently in the tweet list.
public void addTweet(String tweet)
Adds the given tweet to the end of the tweet list.
You may assume that the given tweet does not already exist in the TweetBot's list of tweets.
public String nextTweet()
Returns the next tweet from the tweet list. If all the tweets in the list are exhausted, cycle around to the start of the list.
For example, the following code snippet should produce the output in the comments. Suppose that tweets stores a list of Tweets ["A tweet about something controversial", "Remember to vote!", "Look at this meme :O"].
Think about how to get the next tweet from the list of tweets, and how you can cycle around to the start once you've gone through all the tweets!
Special Case!
As you're working on your TweetBot and testing your implementation, you'll likely start thinking about different cases that your TweetBot will need to handle. There is one specific special case whose behavior is particularly tricky, so we wanted to talk about it more in depth.
Consider the case where your TweetBot is storing a sequence of 3 tweets, and you call nextTweet() on your TweetBot 3 times. Then, you immediately afterward add another tweet before calling nextTweet() again. On the next call to nextTweet(), your TweetBot should return the tweet that was just added -- NOT the first tweet. The pseudo-code example below demonstrates this.
Note that the case described here is a tricky one - really there are couple of different ways that a TweetBot could reasonably handle this situation. But, since you'll be writing code to pass tests that we provide, we're telling you how you should handle this situation for this assignment!
public void removeTweet(String tweet)
Removes the given tweet from the tweet list if it is present.
Pay particular attention to making sure that the remove method should preserve the current iteration order of the nextTweet method above. In particular, using the example tweets from the last method, the following code should produce the output shown in the comments.
In other words, the nextTweet method should continue the iteration through the Tweet list, returning the Tweet immediately following the one that was last returned by nextTweet, continuing with only the removed tweet cut out of the cycle.
Be careful about removing a tweet that is not in the TwitterBot. In this case, the state of the TwitterBot should be unchanged.
public void reset()
Resets the iteration state of the TweetBot such that subsequent calls to nextTweet start back at the beginning.
i have code but it is only passing 10/15 test case and the nexttweet and reset method are nto working accordingly, i guess it it not returning the most recently added tweet
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images