Assignment 03: Working with strings and lists

Starter code: working-with-strings-and-lists-student.zip

Overview

In this assignment, you’ll practice working with strings, and with lists in two contexts. First, you’ll write a series of static methods that interact with Strings, and then with objects that obey the List interface. Then, you’ll extend the ArrayList class, adding new instance methods to a subclass.

We’ve provided a large set of unit tests to help with automated testing, though you might also want to write a driver class with a main method for interactive testing. The Gradescope autograder includes a few more tests, but they exist primarily to verify you’re not gaming the autograder. If your code can pass the tests we’ve provided, it is likely correct.

Goals

Downloading and importing the starter code

As in previous assignments, download and decompress the provided archive file containing the starter code. Then import it into Code in the same way; you should end up with a working-with-strings-and-lists-student project.

What to do

There are three files that you need to open and complete.

First, open src/string/exercises/StringExercises.java. This file contains a set of static methods and documentation describing them. Implement each method as described. Be sure to read over the String API. There are methods in there you will want to use. One of the String.indexOfs and the two String.substring methods will make your life easier here; you shouldn’t need to write any loops to complete this file. Finally, note you can use the String.split method as follows to get an array of the words: String[] words = s.split("\\s+");. (You can treat this as “magic” – it’s a use of “regular expressions”, something we haven’t covered yet.)

Next, open src/list/exercises/ListExercises.java. This file contains a set of static methods and documentation describing them. Implement each method as described. Some hints:

Then, open src/list/exercises/ExtendedArrayList.java. This file describes a class that extends (that is, subclasses) ArrayList, adding several instance methods. Implement each method as described. Remember, these methods are instance methods, and your code is a subclass. They are operating on the current instance (this) of the ExtendedArrayList. You can call methods like size() directly, and they’ll operate on the current (this) list.

We will not test your code on inputs that are not described by the documentation. For example, all of the list and string parameters are described as “non-null”, so you don’t need to (and thus shouldn’t) write code to handle the case of a null value being supplied as one of these parameters.

We’ve commented out the “timeout” code at the top of each test class, so if you want to use the debugger you won’t have any obstacles in your way. On the other hand, this means that if you see a test get “stuck,” it’s probably because you have an infinite loop in the code it’s exercising. Remove the comments if you think this is happening.

What not to do

(I apologize for the tone of the following, but I need to be absolutely clear about something that is not permissible.)

This is the first assignment where you may be tempted to game the autograder, as many of the static methods are standalone “functions,” with well-defined behavior that depends only upon their input. Don’t do it.

What do I mean by gaming the autograder? Since we are still giving you the tests, you may be tempted to write code that is not general, but that does pass the tests. Put another way, you may be tempted to hard-code the “correct” answers to the tests (that won’t work on other inputs!) into your methods like so:

public static int findMarc(String string) {
    if (string.equals("")) {
        return -1;
    } else if (string.equals("Marc")) {
        return 0;
    } else if (string.equals("Marc ")) {
        return 0;
    } else if (string.equals("  Marc  ")) {
        return 2;
    } else {
        return -1;
    }
}

Do not take this approach, or anything similar. We expect you to write general solutions to the assignments, not just attempt to pass the tests. We give you the tests to help you learn (though note that in 187, and later in this class, and in life generally, you won’t always get the tests handed to you), not to let you pattern-match if-then-else statements on the test cases.

Doing so is gaming the autograder. We will treat it as academic dishonesty and lobby for the maximum possible sanction – at least an F in this course, if not an academic suspension.

If you are having trouble understanding the assignment or need help, please ask! But don’t game the autograder.

Submitting the assignment

When you have completed the changes to your code, you should export an archive file containing the entire Java project. To do this, follow the same steps as from Assignment 01 to produce a .zip file, and upload it to Gradescope. Note that if you want things to upload faster, you can use an external program to zip only the src/ directory by expanding the project; that’s all this autograder requires.

Remember, you can resubmit the assignment as many times as you want, until the deadline. If it turns out you missed something and your code doesn’t pass 100% of the tests, you can keep working until it does.