I am having trouble on figuring out how to tackle this test case, below is my code and image one is the test case I am having trouble at(since it will tested automatically, i dont have the testcase.) image two is the main method, plz debug and fix it. Provide the screenshot of the code and output when finsih. import java.util.*; import java.io.*; public class TweetBot { private List tweets; private int index; public TweetBot(List tweets){ if(tweets.size() < 1){ throw new IllegalArgumentException("need contain at least one tweet!"); } this.tweets = new ArrayList<>(tweets); } public int numTweets(){ return tweets.size(); } public void addTweet(String tweet) { tweets.add(tweet); if (tweets.size() == 1) { index = 0; } } public String nextTweet() { if (tweets.size() == 1) { index = 0; return tweets.get(0); } else { if (index == tweets.size()) { index = 0; } String tweet = tweets.get(index); index++; return tweet; } } public void removeTweet(String tweet) { int tweetIndex = tweets.indexOf(tweet); if (tweetIndex == -1) { // Tweet was not found in the list, so the state of the TweetBot should be unchanged return; } tweets.remove(tweetIndex); if (index > tweetIndex) { // If the removed tweet was before the current index, shift the index back by 1 index--; } else if (index == tweets.size()) { // If the current index is now out of bounds, set it to the last index in the list index = tweets.size() - 1; } } public void reset(){ index = 0; } } This is the wrong output I got: A tweet about something controversial Remember to vote! Look at this meme :O A tweet about something controversial Remember to vote! Look at this meme :O Hii, How are everyone? A tweet about something controversial Remember to vote! Look at this meme :O The most frequent word is: a //plz ignore this
I am having trouble on figuring out how to tackle this test case, below is my code and image one is the test case I am having trouble at(since it will tested automatically, i dont have the testcase.) image two is the main method, plz debug and fix it. Provide the screenshot of the code and output when finsih.
import java.util.*;
import java.io.*;
public class TweetBot {
private List<String> tweets;
private int index;
public TweetBot(List<String> tweets){
if(tweets.size() < 1){
throw new IllegalArgumentException("need contain at least one tweet!");
}
this.tweets = new ArrayList<>(tweets);
}
public int numTweets(){
return tweets.size();
}
public void addTweet(String tweet) {
tweets.add(tweet);
if (tweets.size() == 1) {
index = 0;
}
}
public String nextTweet() {
if (tweets.size() == 1) {
index = 0;
return tweets.get(0);
} else {
if (index == tweets.size()) {
index = 0;
}
String tweet = tweets.get(index);
index++;
return tweet;
}
}
public void removeTweet(String tweet) {
int tweetIndex = tweets.indexOf(tweet);
if (tweetIndex == -1) {
// Tweet was not found in the list, so the state of the TweetBot should be unchanged
return;
}
tweets.remove(tweetIndex);
if (index > tweetIndex) {
// If the removed tweet was before the current index, shift the index back by 1
index--;
} else if (index == tweets.size()) {
// If the current index is now out of bounds, set it to the last index in the list
index = tweets.size() - 1;
}
}
public void reset(){
index = 0;
}
}
This is the wrong output I got:
A tweet about something controversial
Remember to vote!
Look at this meme :O
A tweet about something controversial
Remember to vote!
Look at this meme :O
Hii, How are everyone?
A tweet about something controversial
Remember to vote!
Look at this meme :O
The most frequent word is: a //plz ignore this
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 1 images