Project: Pretest

This goal of this exercise is to demonstrate how difficult complete correctness is, even on reasonably simply tasks. This may be a humbling exercise, but we hope not a discouraging one.

Secondary goals include getting your mind back into Java programming and, more importantly, back into thinking about algorithms and data structures; and satisfying my own curiosity about how much you have retained from Programming II.

You will be asked to do a series of problems exercising mostly Programming I material. They will be tested using given JUnit tests. Conventional wisdom is that even professional programmers have difficulty getting perfect solutions (covering all corner cases) to problems like these when put under time pressure. Also tasks like this aren't uncommon in job interviews. Let's see how you do.

1. Set up

Make a folder and copy the starter code from the course folder:

cp -r ~tvandrun/Public/cs345/pretest/. .

Open Eclipse. I recommend you make a new workspace for this course. Start a new Java project. Uncheck "Use default location" and instead select as a location the folder that you made (ie, the folder containing the things copied from the course folder). Hit "Next>" and confirm that you see something like this:

Hit "Finish". In the next screen, in the Package Explorer tab, expand "tests" to see BinSearchTest and several versions of LinkedListTest.

Right-click on BinSearchTest and select "Run as -> JUnit test". If you see something like the following, then you're all set up.

2. Exercise 1

Open Exercise1.java. Your task is to implement binary search.

The method stub takes a sorted array array of some type generic T that implements Comparable and a specific item of that type. Recall that the Comparable interface has a method compareTo() that can be used like

x.compareTo(y)

...and it will return a negative number if x is "less than" y, a positive number if y is "less than" x, and 0 if x and y are "equal". Some details:

Test your implementation using BinSearchTest. JUnit will give you a failure message indicating something of what is wrong. Mainly you need to think about your code.

When you pass all the tests, move on.

3. Exericse 2

Open Exercise2.java. Your task is to implement certain operations in a linked list class. Read the documentation and code through the add() method to get a feel for how this linked list works.

Run the JUnit test LinkedListTestA. Out of the box the following tests should pass: emptySize, oneSize, emptyIterator, fillIterator, fullSize, and oneIterator. The rest should fail.

A. Retrieving an element

Write the get() method according to the specification given in the documentation. Test by rerunning the JUnit test. If you write this correctly, LinkedListTestA should all pass.

Again, you don't waste time looking at the source code for the tests. The error messages (as well as the names of the tests), should give some hints about what's wrong if they fail. When they all pass, try the next one.

B. Setting an element

Write the set() method according to the specificiation given in the documentation. This should be very similar to get(), right? You may overwrite the datum of an existing node---you do not need to make a new node. Test this using LinkedListTestB, which will also run all the tests of LinkedListTestA, which it inherits.

C. Removing an item

Everything we've done so far is "easy"; the difficulty comes only with the time pressure. But now see if you can push it even further with a problem that has some challenge to it. Implement the remove() method of the Exercise2 class (not the remove() method of the iterator), according to the speficiation given in the documentation. Test this using LinkedListTestC (which inherits all the tests from the two previous steps).

Let's see some green bars.

4. To turn in

Please copy the Java files you modified your files to your turn-in folder:

cp exercises/Exercise*.java /cslab.all/linux/class/cs345/(your id)/pretest

To keep up with the course, this should be finished by Jan 12.


Thomas VanDrunen
Last modified: Tue Jan 5 11:17:50 CST 2016