Assignment 1: Class-based Inheritance and Virtual Dispatch in Java


Due Monday, October 9 at 11:59 PM

Goals for This Assignment

By the time you have completed this work, you should be able to:

Provided files:

Step-by-Step Instructions

Step 1: Download Needed Code

Download everything from the links above into a single directory.

Step 2: Read and Understand Provided Code

For this assignment, you'll be working with an immutable linked list implementation, a type of persistent data structure. While you (hopefully!) are familiar with linked lists, this implementation is likely very different from the one you're used to. Notably:

In addition to the above bullets, there is a provided JUnit 4 test suite in ImmutableListTest.java.

Step 3: (Try to) Compile and Run the Tests

You will need to compile and run the code. If the code fails to compile, do not try to run it; it either won't work, or you'll run an old, previously-compiled version. The two commands below compile and run the code, respectively. The two commands are slightly different, depending on what system you're running with. If you're on a UNIX-based system (e.g., Mac OS X, Linux), use:

javac -cp .:hamcrest-2.2.jar:junit-4.13.jar -Xlint:all ImmutableList.java Cons.java Nil.java ImmutableListTest.java
java -cp .:hamcrest-2.2.jar:junit-4.13.jar org.junit.runner.JUnitCore ImmutableListTest

If you're on Windows command prompt, use these two commands, which use semicolons instead of colons:

javac -cp .;hamcrest-2.2.jar;junit-4.13.jar -Xlint:all ImmutableList.java Cons.java Nil.java ImmutableListTest.java
java -cp .;hamcrest-2.2.jar;junit-4.13.jar org.junit.runner.JUnitCore ImmutableListTest

If you're on Windows Powershell, use these two commands, which put some arguments in single quotes:

javac -cp '.;hamcrest-2.2.jar;junit-4.13.jar' -Xlint:all ImmutableList.java Cons.java Nil.java ImmutableListTest.java
java -cp '.;hamcrest-2.2.jar;junit-4.13.jar' org.junit.runner.JUnitCore ImmutableListTest

If all tests are passing, you'll see output like the following:

JUnit version 4.13
....................................
Time: 0.053

OK (36 tests)

If tests are failing, the output will instead show which tests are failing. From there, you can look to see what those tests are doing in the test suite (ImmutableList.java), which will inform you of what needs to be modified.

Note that the provided code will not compile as provided. It's missing the implementations of multiple methods which are needed to make the tests compile.

Step 4: Implement Missing Code, with Restrictions

Add code to Cons.java and Nil.java to get it to compile and pass the tests. Specifically, you need to implement the following methods for each:

Example calls to these methods are in ImmutableList.java.

Restrictions

For full credit, your code:

These restrictions will force you to use recursion for a correct solution, and will also force you to fully exploit virtual dispatch (also known as dynamic dispatch, polymorphism, and ad-hoc polymorphism). While these restrictions will likely be annoying, it will force you to use a key object-oriented feature (virtual dispatch), as well as serve as good practice for later in the course (recursion).

Hints

Step 5: Turn in Your Code Using Canvas

Log into Canvas, and go to the COMP 333 class. Click “Assignments” on the left pane, then click “Assignment 1”. From here, you need to upload the following files:

In addition, if you collaborated with anyone else, be sure to download collaborators.txt and write the names of the people you collaborated with in the file, one per line. Please submit this file along with the other files.

You can turn in the assignment multiple times, but only the last version you submitted will be graded.