Programming Assignment 5

Due Date: Tuesday, February 14, 10:00PM Pacific Time

Learning Goals


Different assignments in this course have different collaboration policies. On this assignment, you can collaborate with anyone in the course, including sharing code. In your submission, give credit to all students and course staff who helped you with this assignment by noting their name and how you used their ideas or work. Note that using someone’s work without giving credit to them is a violation of academic integrity.

Starter Code

You can get the starter code at

Not all files have been provided for you. If you do not see the java file, you will need to make a new one on your own.

Debug Runtime Errors

In the starter code, a file named is given. The class Debug has a main method. This method has to take a String array as its parameter from the command line, consider the initial argument of the array as a keyword, and print the number of occurrences that the keyword appears in the rest of the array. The code has several runtime errors. Fix the errors so that the code satisfies the following:

  1. If the input array is empty, then the output should print No keyword is given
  2. If the input array is not empty, the initial argument should be a keyword with the prefix -- (two dashes). If the argument does not satisfy this format, then the output should print Write a keyword with the prefix --
  3. If the input array is not empty and the keyword is given in the correct format, then calculate the count of occurrences of the keyword after the initial argument. If the array has only one String, then the count is 0. The output should print The answer is [count]


$ javac   
$ java Debug   
No keyword is given   
$ java Debug melon   
Write a keyword with the prefix --   
$ java Debug --melon   
The answer is 0   
$ java Debug --melon apple orange melon watermelon melon banana   
The answer is 2   

We can use System.out.println() method to debug runtime errors. The lines 3, 7, 10, 12 (comments starting with //) in are provided to help you debug the code. You may remove // and check the output of these method calls. These will print out some information about parameters or variables. We can see what values are assigned to those during execution, which makes debugging easier. Remember to put // back before submission.

Array Methods

In a file called, write the following methods in a class called ArrayExamples. For each, write at least three tests (a test is a use of checkExpect) where each of the three has a different length of array used in the input.

Using Main and Command-line Arguments



  1. Can we use <some other library> in this PA instead of loops?
    • Try to use the things we learned most recently. That said, if you know something you want to try, that’s fine. Just be warned that it might be more work to not use the stuff we just learned
  2. I wrote test methods with the Tester, but ./run is telling me that no tests ran.
    • Tester methods have to start with “test” at the beginning! For example, boolean testAdd(Tester t) { …. }. In, all tests should be in class ArrayExamples, not Pair.
  3. I failed some tests on Gradescope, but it is not showing me any error message, so I don’t know what is wrong.
    • We did something slightly different with this autograder. For each method we wrote several tests but only publicly show you some of their results in detail. The goal is to give you a nudge to think more about detailed tests you could write for your own methods. So you can see where you failed and all you know (for now) is that you should try testing and understanding those methods more.
  4. My earliest method’s tests on Gradescope are not passing.
    • Double check your understanding of compareTo method. Try using compareTo with longer strings and seeing the result. Does the method always return 0, -1, or 1?
  5. I am receiving an array index out of bounds error in
    • Unlike some of the previous method, you can not make the assumption that string argument(s) will be given. Hence, args[0] will throw the index out of bounds error. What is a possible way to check if any arguments are given?
  6. Help! I did the conditional checking and is still throwing index out of bounds!