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


Due Friday, September 18 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. There is also a makefile (makefile), which can be used to make it easier to compile and run the code on a system with make installed (easy on Linux and Mac, not so easy on Windows).

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

If you have make installed, you can compile the code and subsequently run the test suite with:

make
make test

If you don't have make installed, you can directly run the commands on a UNIX-based system (e.g., Macs, Linux) with:

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, the above commands need to be tweaked to use semicolons instead of the colons, like so:

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, the commands need another tweak:

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.