this is my code, but after automatically check it, it shows smt went wrong(image one) image two is the spec for nextTweet and below is my code, since it's the system automatically check the code, I dont really have a textcases. plz debug it and fix it. show the screenshot of the code when u finish. thanks Below is my whole code for TweetBot 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) { // if the tweet was the first one added, set the index to 0 index = tweets.size() - 1; } } 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; } } and here is the main class method (ignore the most frequent word) import java.util.*; import java.io.*; public class TwitterMain { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("tweets.txt")); // Make Scanner over tweet file List tweets = new ArrayList<>(); while (input.hasNextLine()) { // Add each tweet in file to List tweets.add(input.nextLine()); } TweetBot bot = new TweetBot(tweets); // Create TweetBot object with list of tweets System.out.println(bot.nextTweet()); // should print "tweet about something controversial" System.out.println(bot.nextTweet()); // should print "Remember to vote!" System.out.println(bot.nextTweet()); // should print "Look at this meme:0" System.out.println(bot.nextTweet()); // should print "tweet about something controversial" (because the index has wrapped around) bot.addTweet("This is a new tweet!"); System.out.println(bot.nextTweet()); // should print "This is a new tweet!" bot.addTweet("Hii, How are everyone?"); System.out.println(bot.nextTweet()); // should print "Hii, How are everyone?" TwitterTrends trends = new TwitterTrends(bot); // Create TwitterTrends object // TODO: Call and display results from getMostFrequentWord and your // creative method here String mostFrequentWord = trends.getMostFrequentWord(); System.out.println("The most frequent word is: " + mostFrequentWord); } }
this is my code, but after automatically check it, it shows smt went wrong(image one)
image two is the spec for nextTweet and below is my code, since it's the system automatically check the code, I dont really have a textcases. plz debug it and fix it. show the screenshot of the code when u finish. thanks
Below is my whole code for TweetBot
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) {
// if the tweet was the first one added, set the index to 0
index = tweets.size() - 1;
}
}
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;
}
}
and here is the main class method (ignore the most frequent word)
import java.util.*;
import java.io.*;
public class TwitterMain {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("tweets.txt")); // Make Scanner over tweet file
List<String> tweets = new ArrayList<>();
while (input.hasNextLine()) { // Add each tweet in file to List
tweets.add(input.nextLine());
}
TweetBot bot = new TweetBot(tweets); // Create TweetBot object with list of tweets
System.out.println(bot.nextTweet()); // should print "tweet about something controversial"
System.out.println(bot.nextTweet()); // should print "Remember to vote!"
System.out.println(bot.nextTweet()); // should print "Look at this meme:0"
System.out.println(bot.nextTweet()); // should print "tweet about something controversial" (because the index has wrapped around)
bot.addTweet("This is a new tweet!");
System.out.println(bot.nextTweet()); // should print "This is a new tweet!"
bot.addTweet("Hii, How are everyone?");
System.out.println(bot.nextTweet()); // should print "Hii, How are everyone?"
TwitterTrends trends = new TwitterTrends(bot); // Create TwitterTrends object
// TODO: Call and display results from getMostFrequentWord and your
// creative method here
String mostFrequentWord = trends.getMostFrequentWord();
System.out.println("The most frequent word is: " + mostFrequentWord);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
but on ur full java program's nextTweet method... u didnt change it.
and after I fixed the code like u said: here is my (wrong) output:
A tweet about something controversial
Remember to vote!
A tweet about something controversial
Remember to vote!
Look at this meme :O
This is a new tweet!
missing: Hii, How are everyone?