Homework Assignment 1

Writing a Simple Class


810:062
Computer Science II
Object-Oriented Programming


Due: Tuesday, January 25, at 8:00 AM


Introduction

This first assignment asks you to implement a small class that will later serve as part of larger programs. You will implement a new kind of number, a BoundedInteger.

When we write programs to solve problems in the world, we often need to model particular kinds of values, and those values don't match up with the primitive types in our program langauge. For example, if we were writing a program to implement a clock or a calendar, then we would need numbers to represent minutes or days. But these numbers aren't Java integers, which range from -231 to 231-1; they range from 0 to 59 and 1 to 31 (or less!), respectively. Java's base types are useful building blocks, but at the level of atoms, not molecules.

Write your program in the style we first learned in Session 1: first write a test that expresses what you would like your code to do, and then write the code to do it. Take small steps, and your tests will give you feedback as soon as possible.

Use the GenerateTest program to create your test class.

Be sure that you have completed the exercise from Session 2, which steps you through the installation of JUnit, our testing framework, in your Linux account and makes sure that you are ready to use it in all of your programs. If you have any questions or problems, please ask soon. And remember: The tests in your test class must begin testXXX.

And, of course, all instance variables are private.


Tasks

Write a class for representing bounded integers.

A bounded integer takes integer values within a given range. For example, the minutes part of a time takes values in the range [0..59]. The next minute after 59 is 0. The minute before 0 is 59.

Object creation

We want to be able to create bounded integers in two ways.

  1. We specify the object's lower bound, its upper bound, and its initial value.
  2. We specify only the object's lower bound and its upper bound. The object's initial value is the same as its lower bound.

Of course, this means that you need to write two constructors.

We would like to be able to use bounded integers in much the same way we use regular ints.

Arithmetic operations

We would like to be able to:

A bounded integer "wraps around" when it does such arithmetic. For example, suppose that we have a minutes bounded integer whose value is 52. Adding 10 to this object gives it a value of 2.

Value operations

We would like to be able to:

If we try to set a bounded integer to a value outside of its range, the object should keep its current value and print an error message to System.err.

Comparison operations

We would like to be able to:

The answer to each of these questions is true or false.

You may implement these requirements in any order you choose, though you'll need at least one constructor before you can test any of the others.


Deliverables

By the due date and time, submit the files

Be sure that your submission follows all homework submission requirements.


Eugene Wallingford ..... wallingf@cs.uni.edu .... January 17, 2005