In-class Exercise: 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.

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, right click on "tests.jar" and select "BuildPath->Add to build path". Then expand "tests.jar" until you see BinSearchTest.class and LinkedListTest.class.

Select BinSearchTest.class and click on the run button. If you see something like the following, then you're all set up.

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. You are not given the source code of the test cases, but 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 (or when you're stumped), move on.

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 LinkedListTest.class. Out of the box the following tests should pass: emptySize, oneSize, fullSize. 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, the following tests should pass (in addition to the tests that pass out of the box, which should still pass):

Again, you don't have the source code for the tests, but 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. If you write this correctly, the following additional test should pass (and no previously-passing test should break):

C. Iterating over a list

Write the iterator() method according to the specification given in the documentation. For convenience, you can find a stub for an anonymous inner class commented out. Note that you do not need to implement the remove() method of the Iterator (leave it as it is, throwing the exception). If you write this correctly, the following additional tests should pass

(and no previously-passing test should break):

D. The hard one: Removing an item

If you've made it this far, I'm already impressed. Let's see if you can push it even further. Implement the remove() method of the Exercise2 class (not the remove() method of the iterator you wrote in the previous part), according to the speficiation given in the documentation. If you write this correctly, the following additional tests should pass

(and no previously-passing test should break):

Let's see some green bars.

To turn in

Please copy your files to your turn-in folder:

cp exercises/Exercise*.java /cslab.all/ubuntu/cs345/turnin/(your id)/pretest

Thomas VanDrunen
Last modified: Fri Jan 9 16:08:49 CST 2015