Assignment 2: Assignment for Topic 2, Classes

This assignment asks you to write (at least) two classes, based on their descriptions.

Class Lock.

A "combination lock" has a combination, which consists of three hidden integers. A lock's combination is set by the creator of the lock. Once a lock is created, there is no direct access to the combination.

A combination lock also has a display consisting of three integers, which the lock's owner has entered in one way or another. (On a combination lock, the owner would turn the lock display, actually a dial, to the right to the first number, then to the left to the second number, and then to the right to the third number). When a lock is first created, the three guesses are set to 0.

The guesses on the display can be changed by someone who is trying to unlock the lock, and they can be changed in any order. Also, a lock can be reset, meaning that the three guesses are set to 0.

A lock is unlocked if (and only if) each guess matches its corresponding integer in the combination; otherwise, it is locked. The lock can be asked whether it is locked or unlocked. The lock also can produce a String that  describes its state, consisting of the three current guesses and whether the lock is locked or unlocked.

Here ends the description of a electronic combination lock.

Design and write a class to model combination locks. Do not prompt the user to input the guesses; instead, provide three methods that the owner of a Lock can call to change the guesses.

Class LockFirm.

Locks are made and sold by the "lock firm". Initially, the firm has no locks. However, anyone can buy a new lock from the firm (we don't deal with the price of a lock). When someone asks to buy a lock, they are given three things: (1) a new lock (in your implementation, the name of an instance of class Lock), (2) the combination (i.e. three integers), and (3) a serial number, which is an integer.

The Lock firm maintains a list (probably an array) of all locks sold, together with their serial numbers and combinations. Lock owners often forget their combinations. They can always ask the firm for the combination, provided they can tell what lock it is (by giving the name of their instance of Lock) and the serial number.

Here ends the description of the lock firm.

Your job

Design and write a class to model the lock firm. You may have to write one or two other classes in order to do this properly. Also how you choose serial numbers and combinations is up to you. Using a random number generator might be a good idea; look in class Random in package java.util.

Here is a class with a method main that we used while checking out our solution. Read it to get some insight into the other classes that we used
and for its programming style --good use of indentation, statement comments, comments on variables (we went overboard to help you out), etc.

We suggest strongly that you test class Lock thoroughly before proceeding to writing and checking out class LockFirm. This will reduce the time it takes you to check out the whole program.

What to do with your program

Take the program to one of the office hours for CS202. It should be on a floppy disk, ready to run. If the person is not busy with someone else, they will
grade your program while you are watching; otherwise, you can leave it and pick it up later.

The grader will be instructed to return the program to you, ungraded, if proper indentation and method specifications are not used.

A solution set for this assignment is now available.


Last Modified:  Tuesday, October 02, 2001 12:58:18 PM