Computer Science II
PA 01
Due by Wednesday, August 29th
[8:00 AM for code and 9:00 AM for paperwork]
For most of you, it has been a while since you last wrote any real Java code. For all of you, I suspect your code writing skills might be a little rusty. Add on to that the fact that you are learning a new environment in which to write, edit, compile, and execute your code, and you could use some fairly basic practice.
This assignment is not overly difficult from a coding perspective (although their are some challenges). It asks you to implement a small class that might 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 language. For example, if we were writing a program to implement a calendar or model a car, then we would need numbers to represent dates or the number of doors on that car. But these numbers aren't Java integers, which range from -231 to 231-1; they range from 1 to 31 (or less!) and 2 to 5, respectively.
In this assignment you will write and submit two classes.
Before you begin coding, you should read the details below under "Tasks" and make sure that you understand the entire program.
I strongly encourage you to use the concept of "incremental development" or "small steps" when working on this program. That is, begin with one simple piece of functionality in BoundedInteger. Think about how that functionality works, and how you would test that it works. Write the code in BoundedInteger that you think causes this functionality to work, and then write the code in BoundedIntegerTest that verifies that you were correct. In fact, many practitioners even reverse this process. That is, they first write a test that expresses what they want their code to do, and only then do they write the code to do it.
Take small steps, and your tests will give you feedback as soon as possible.
One last hint - my description below is based on the order in which we should think about the problem, but not actually the order in which we would write the code for the problem. Recall that I said in session 3 that you should think about the behavior of the class first and then worry about which instance variables are needed to support that behavior. Having said that, when we code, we have to begin by writing constructors and they deal with instance variables. Thus, even though we start thinking about behavior, we start coding with instance variables.
Write a class for representing bounded integers.
A bounded integer takes integer values within a given range. For example, the days part of a calendar takes values in the range [1..31]. The next day after 31 is 1. The day before 1 is 31.
Arithmetic operations
We would like to be able to use bounded integers in much the same way we use regular ints. But because they are objects, we need to do this via method calls. We would like to be able to:
Special things to consider : 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:
Special things to consider : 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.out.
Comparison operations
We would like to be able to:
The answer to each of these questions is true or false.
Object creation
If you consider the operations listed above, there are three things that a bounded integer will need to maintain about itself (instance variables). We want to be able to create bounded integers in two ways.
Of course, this means that you need to write two constructors.
Again, you may code these requirements in any order you choose, though you'll need at least one constructor before you can test any of the other things.
By the due date and time, submit the files
Shortly after this, you will submit paper printouts of your code.
Be sure that your submission follows all homework submission requirements.