Homework 11: Debugging with print statements
The homework problems below will help you prepare for Quiz 6
Introduction
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. This is a very common practice in industry!
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:
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:
.put(word, words.get(word)+1); words
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 tocall
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: * 2 pts: Having three sufficient explanations of what the test cases are doing (one short paragraph each). ChatGPT/LLM-sounding answers will not get credit. Your answers must demonstrate understanding the test case, not just writing out what every single line of it does. * 6 pts: Sufficient print statements that tell a story of how you found the faults. That is, we can see your thought process by where and what you chose to print out (the print statements themselves don’t need to be in proper English). * 1 pt: pass all three test cases completely. * 1 pt: Driver correctly prints out the topwords after fixing all the bugs.
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: * Overly long, flattering, and/or flowery writing that does not get to the point * Generalizations and other high-level statements that do not answer the question and do not demonstrate an understanding of the test cases * Unnecessary equivocation (talking about multiple possibilities or discussion of multiple answers) and ambiguous meanings.
Submission and grading
When you are ready to submit your assignment, inside that folder type the following command, which will zip up all your java files into a tarfile called Homework11.tar
:
tar -cvf Homework11.tar *.java
You will then upload this single Homework11.tar
file to BB for grading.