Drill 3
Sun Feb 12 2023 00:40:36 GMT+0000 (Coordinated Universal Time)
Saved by @Achilles_17
class TextTweet{ String contents; int likes; TextTweet(String contents, int likes){ this.contents = contents; this.likes = likes; } boolean hasMention(String username){ if (this.contents.contains("@" + username + " ")){ return true; } else if (this.contents.indexOf("@", this.contents.indexOf(" "))!= -1){ return (this.contents.substring(this.contents.indexOf("@", this.contents.indexOf(" "))).equals("@" + username)); // I received help from Carlos Gomez during lecture for this method on 2/10/23 } else { return false; } } boolean hasLike(){ return (this.likes >= 1); } String firstMention() { int aI = contents.indexOf("@"); if (aI == -1) { return ""; } int sI = contents.indexOf(" ", aI); if (sI == -1) { return ""; } else{ return contents.substring(aI + 1, sI); } } int Likes(){ return likes; } String Contents(){ return contents; } } class ReplyTweet{ TextTweet ReplyTo; String contents; int likes; ReplyTweet(TextTweet ReplyTo, String contents, int likes){ this.ReplyTo = ReplyTo; this.contents = contents; this.likes = likes; } boolean MorePopularReply(){ return (this.likes > ReplyTo.Likes()); } int allLikes(){ return this.likes + ReplyTo.Likes(); } boolean hasMention(String username){ if (this.contents.contains("@" + username + " ")){ return true; } if (this.ReplyTo.Contents().contains("@" + username + " ")){ return true; } if (this.contents.indexOf("@", this.contents.indexOf(" "))!= -1) { return (this.contents.substring(this.contents.indexOf("@", this.contents.indexOf(" "))).equals("@" + username)) || (this.ReplyTo.Contents().substring(this.ReplyTo.Contents().indexOf("@", this.ReplyTo.Contents().indexOf(" "))).equals("@" + username)); } else { return false; } } } class Drill3{ TextTweet tweet1 = new TextTweet("hello world my name is @dummy1", 0); TextTweet tweet2 = new TextTweet("hello world my name is @dummy 3", 85); ReplyTweet tweet3 = new ReplyTweet(tweet1, "goodbye world I will see @dummy1 again", 99); ReplyTweet tweet4 = new ReplyTweet(tweet2, "goodbye cruel world. I hope I never see dummy again", 82); boolean ex1 = this.tweet1.hasMention("dummy"); // expected: false boolean ex2 = this.tweet2.hasMention("dummy"); // expected: true boolean ex3 = this.tweet1.hasLike(); // expected: false boolean ex4 = this.tweet2.hasLike(); // expected: true String ex5 = this.tweet2.firstMention(); //expected: dummy boolean ex6 = this.tweet3.hasMention("dummy"); //expected false boolean ex7 = this.tweet4.MorePopularReply(); //expected false
Comments