Week 5 Class Exercise 1: Sorting Spongebob Tweets

Chris Tralie

Overview / Logistics

The purpose of this class exercise is to give you practice with interfaces, and, in particular, the Comparable interface. In the process, you will also learn some basic strategies for data wrangling, or the process of combing through large amounts of data to find what's relevant.

Click here to download the skeleton code for this exercise, which you can open with NetBeans. You will be editing src/Tweet.java.

Learning Objectives

  • Implement compareTo from the comparator interface
  • Work with arrays of objects
  • Access array indices carefully

Background: Tweet Objects

I have scraped the majority of the tweets from the SpongeBob account on Twitter in 2020 using the Twitter API, and I have put them into the file tweets.txt in the Data directory. (Those curious about how I scraped the tweets can refer to the TwitterScraper.py in the Data folder). I then created a Tweet class to encapsulate what it means to be a tweet, along with a static method to load an array of tweets from the data text file. Every tweet object has the following fields:

  • date: A YYYY/MM/DD format of the date
  • time: The time in HH:MM:SS format
  • text: The text of the tweet
  • retweets: How many retweets the tweet has
  • likes: How many likes the tweet has

I've provided a sample static method that goes through an array of tweets and figures out the most commonly used words, as you can see if you click here. This is a great example of using HashMaps together with the Comparator interface, so you should study it before class. If you run that method, you should see the following output

the: 249
spongebob: 150
a: 140
on: 125
your: 109
to: 105
is: 102
you: 101
of: 87
and: 84
this: 70
in: 64
for: 61
with: 60
|: 46
we: 44
episodes: 39
all: 37
now: 36
@nickelodeon: 34
are: 34
our: 34
it's: 33
at: 33
patrick: 31
day: 31
free: 29
what: 29
bikini: 26
happy: 26
who: 26
not: 25
from: 24
when: 24
an: 24
you're: 23
🍔: 23
new: 22
it: 22
if: 21
don't: 20
bottom: 20
need: 20
👏: 20
that: 20
like: 20
have: 19
but: 19
us: 19
@plutotv: 18
										

Programming Tasks

During class, your group will work on the following tasks

Task 1: Sort the tweets by their number of likes

Implement the Comparable interface and sort the tweets by their number of likes. Print the tweet with the most likes and the tweet with the least likes.

Task 2: Sort the tweets by their date

Change your comparator so that it sorts the tweets from most recent to oldest. You will have to use the compareTo method of the String class.

Then, update your comparator so that it sorts the tweets first by date, and then by time. So tweets on the same date but at a later time should be first.