Homework 2: Counter Varieties

Homework is important practice for you, but it’s not graded so you don’t have to submit it. This particular homework is designed to help you practice programming with interfaces and basic unit testing (via Java’s assert mechanism for now).

Overview

You will develop several implementations of the Counter interface we discussed in lecture. Each of your implementations will contain a main method for basic unit testing. All of your implementations will also “plug into” a general, polymorphic test program called PolyCount. Start by grabbing the Counter.java and PolyCount.java files from the Piazza post for this homework assignment.

The Counters

You need to develop a total of three implementations of the Counter interface as follows:

After you’ve done it once for BasicCounter the other two are just variations on the theme, but you’ll have to look up some additional Java things for each which makes for good review.

Unit Testing

All of the implementations above should contain a main method that tests whether the implementation works as expected. You should use Java’s assert mechanism for testing just like we did in lecture: The tests should “do nothing” if they pass, but “explode” very visibly if one of them fails.

Note that the unit tests in main should test exactly that counter, not what a weaker, more general specification would make you test. So presumably each will test against a fixed sequence of expected results from value() after certain up() and down() calls have been executed. Make sure that you test the obvious boundary conditions carefully (if there are any), most importantly for a SquareCounter counting down.

The PolyCount Program

Before you continue, be sure to actually read the PolyCount.java file, it contains some relevant information in comments. Once you have the three counters implemented, you should be able to compile and run PolyCount without any errors/exceptions. If you cannot, something is wrong with your code; try to figure out what it is and fix it.

Hints

Adding More Interfaces

You should only try this once you have finished the work above and all the test cases (your own as well as PolyCount) pass. You will now “rework” your counters to add another feature, and you’ll do it with interfaces:

This is not a lot of work, but if you’re new to interfaces you might have a hard time getting started. Don’t be shy and come to office hours if you cannot figure it out on your own!

The PolyCount Program

There should be nothing to do for PolyCount even after you’ve performed major surgery on your counters to make them resetable. If the PolyCount program no longer compiles or throws an exception after your modifications, you did something wrong somewhere along the way; try to figure out what it is and fix it.

Hints

Artifacts

At minimum, you should have the files BasicCounter.java, SquareCounter.java, FlexibleCounter.java, Resetable.java, and ResetableCounter.java; of course you also need our Counter.java and PolyCount.java files for completeness. All of these should compile without warnings with the -Xlint:all flag; there should be no checkstyle warnings either with the 601.226 configuration file posted on Piazza.