Homework 9

This homework will be a little different than your other homeworks, as you will be practicing using print statements to debug someone else's code.

In this homework, we've provided you with three java files -- a driver, a class called SentencePart3, and a testing file with test cases. The driver and the testing file both have main methods you can run, and if you do this, you will see that they crash. DriverPart3.java and SentencePart3.java are full of bugs, and they obviously don't pass the tests!

You can download the files you will need by right-mouse clicking on the links below, and choosing "Save Link As", and then saving a file with that name to a new folder on your computer:
DriverPart3.java
SentencePart3.java
Part3Tester.java
words_alpha.txt
covid_10K.csv -- click the link normally to get to Ed where you can download

Your assignment is to use debug print statements to work backwards, for each failing test case, to figure out on what line the fault is, and then fix it so it passes all the tests. You'll need to do this process several times, because there are several bugs.

Understanding the test cases

Take a look at Part3Tester.java, and in one English paragraph, explain how the test case sets up its inputs, what method(s) it is calling in the other two files, and what is the goal of each test case. Write these as comment blocks inside that testing file.

Debugging with print statements

Next, compile all three files (they should be in a new folder) with javac *.java and then run the test cases with java Part3_Tester. You will immediately notice a NullPointerException on line 36 in this DriverPart3.java. What's on that line? Well, it looks like:
words.put(word, words.get(word)+1);
and something there is null.

There are only two variables: words and word. Print them out before the line of code with the exception -- which one is null? It turns out neither is null:
word prints out to call
words prints out to an empty dictionary, {}

What does it mean to call get and put on an empty dictionary? If you're not sure, add in this print statements before that line:
System.out.println("words.get(word) is " + words.get(word));

We get the same exception there in that print statement! That means the problem is when we call get(...) on an empty dictionary -- and this makes sense if we think about it. We can't retrieve the value for call if it's not in the dictionary, can we?

So we have found the fault -- we need to check if the value isn't in the dictionary before we try to get it out and then put it back in. Go ahead and add an if statement to do that on the previous line. When you rerun your code, you should see YOU PASSED TEST2!!!! if your fix worked -- if not, try a different fix (and use more print statements if you need to see what was wrong with the code you added).

Once you've fixed this bug, comment out (but don't delete!) your print statements, and move on to fixing the next set of bugs (there are several) until you get all of the third test working in this file.

Running and debugging the driver

Next, let's fix the bugs revealed by the driver. Type java DriverPart3 into your terminal, and see where it crashes. Continue to fix the bugs until the program runs with errors, and the getTopWords() method is returning words in the tweets, not locations. If your code is correct, the most common words should be:

05198 of covid19
00612 of peopl
00586 of new
00555 of test
00515 of trump
00486 of case
00445 of pandem
00445 of death
00422 of coronaviru


Grading and Submission

In the terminal, type tar -cvf HW9.tar *.java to create the tarfile. This time, submit it to BB.

You will be graded on:
This homework will count as extra credit: you can either apply it to your Quiz6 score, or add it into your homework total. We will be calculating the higher of the latter two automatically and applying it for you during final grade calculations.

* Using ChatGPT for any assignment in this class, whether English text or code, is forbidden. In addition, it is important that you be able to communicate clearly and concisely, not just in this class, but in your future classes. For example, we will reduce points for any English answers that exhibit any of the following: