Session 5

Implementing a BoundedInteger

Computer Science II
Object-Oriented Programming

Beginning Homework 1

Homework 1 asks you to implement a BoundedInteger class. A BoundedInteger is an object that acts like an int, but it has a minimum and maximum value.

Where do we start? Choose a requirement, and write a test. The simplest place I can think to start is by creating one object and seeing if its value is what I expect.

First I need a test class. So:

    mac os x > mkdir session05
    mac os x > cd session05
    mac os x > mkdir homework01
    mac os x > cd homework01/
    mac os x > cp ~/home/teaching/062/web/sessions/session01/GenerateTest.class .
    mac os x > java GenerateTest BoundedIntegerTest

Now I write my first test:

    public void testTwoArgumentConstructor()
        BoundedInteger minutes = new BoundedInteger( 0, 59 );
        assertEquals( 0, minutes.value() );

[ Notice: names of method, constructor, method. ]

The test doesn't compile, so I write code to make it compile and run:

    public class BoundedInteger
        private int lowerBound;
        private int upperBound;
        private int value;

        public BoundedInteger( int lowerBound, int upperBound )
            this.lowerBound = lowerBound;
            this.upperBound = upperBound;
            value           = lowerBound;

        public int value()
            return value;

[ Notice: names of instrance variables, use of this. ]

Pick another requirement, write a test, write some code, clean up. I'd start with incrementing, changing value, then rest of incrementing. Then adding, followed by decrementing and subtractintg. Finally printing and comparing.

[ Notice: use of methods to implement others, duplication in tests. ]

How can we test writing to System.err?

For now, let's examine our error output "by hand", either in the shell window or in an output file. Read about how to redirect standard and error output in the notes below.

Walking Through Actual Homework 1 Submissions

Now that we've successfully implemented (some of) BoundedInteger in class, and some or all of the object in your assignments, let's walkthrough some submitted code to see what we can learn...

Exercise: Take two submissions and discuss them in groups of two or three. What do you like about these programs? What would you improve?

Consider these issues:

When we write the tests matters...

Preparing for Homework 2

... on-line auctions ... the Google IPO ...

... auctioning to the highest bidder ...

... goals: selling all shares, maximizing revenue, being fair

A Dutch flower auction runs (roughly) like this: The price for a lot starts at a high number. At the beginning of the auction, a large clock on the wall begins counting down -- quickly -- from the high price to lower and lower prices. The first bidder to stop the clock agrees to pay the currently-shown price for the lot.

Variation... Take m bids. Sell to the top n bidders at the nth price.

Designing Good Programs

In writing tests, we design the interaction with an object.

In refactoring the class, we design the implementation of the object.

We must pay attention at each step.

Wrap Up

Redirecting Input and Output

All shell commands and most Unix commands take their input from standard input ("stdin"), write their output to standard output ("stdout"), and write their error output to standard error ("stderr"). By default, stdin is connected to the keyboard, and stdout and stderr are connected to the terminal screen.

We'll learn more about these streams soon, but for now we need to know how to change where data sent to stdout and stderr go.

You can redirect one of these streams by specifying the desired location on the command line using (get ready for some jargon...) a redirection metacharacter.

In Linux, your shell interpreter is bash, which uses a particular set of command-line metacharacters. Numbers refer to the kind of I/O being done: 0 for standard input, 1 for standard output, and 2 for standard error. Here are some of the standard forms of redirection for the bash shell:




Redirect standard output to a file


Redirect standard error to a file


Redirect standard error to standard output


Redirect standard input to a file


Pipe standard output to another command


Append to standard output to a file

< and > assume standard input and output, respectively, so you don't need to list the numbers 0 and 1.

Here are some examples, some general and some we can use in testing our BoundedIntegers:

We will see examples that redirect input soon...

Eugene Wallingford ..... ..... January 25, 2005