The aim of these exercises is to:
Experience using abstraction to design a computer programs to solve a real-world problem.
Practice creating and using a Java interface.
Become familiar with the javadoc
automatic documentation generation facility
Students should read Weiss Chapter 3 in preparation for these lab exercises.
Preparatory Exercise (Adapted from Weiss, Exercise 3.18).
A combination lock has the following basic properties:
open
and changeCombo
and private data
fields that store the combination. The combination should be set in the constructor.
Describe the behaviour of each operation using words. You will implement Java code for this behaviour later.
Writing and Implementing Interfaces
A Java interface specifies a series of operations required of a class in order for the class to meet a certain behaviour or standard. That is, an interface defines the minimum functionality required of a class, without specifying how those methods should be implemented. Interfaces are used to capture the functional similarities of different types that do not necessarily constitute a hierarchical class relationship.
The general form of an interface is:
public interface MyInterface { public returnType myFirstMethod (argumentType argumentName, ...); public returnType mySecondMethod (...); ... }
Notice that it has the same layout as a standard Java class file, except that:
the keyword interface
replaces the keyword class
,
there are no member variables, and
there is no code associated with the methods of the interface.
A Java implementation is a class that provides a concrete instantiation of the functionality demanded by an interface; the class asserting it provides an implementation for all methods specified in the interface. The Java compiler ensures this compliance, generating an error if the provided coverage of methods is incorrect or incomplete.
Java implementations are defined just like normal classes, except the additional keyword implements
and an interface name is added to the class definition.
For example:
// An implementation of a Lock using an integer to store the combination public class LockInt implements Lock { private int combination; ... }
In a file called Lock.java
, complete an interface for the combination lock question above..
The three methods should return true
if the operation succeeded (for example, if the lock opened with the combination passed to it), and false
otherwise.
Compile your interface to ensure you have the correct syntax.
The compiler should not report any errors.
Write two different implementations of the Lock
interface.
The first implementation (LockInt
) should store the hidden combination as an integer between 000 and 999 inclusive.
The second implementation (LockString
) should store the hidden combination as a String
.
In both cases, the member variables should be private
(obviously you don't want anyone looking at the combination!).
Also, as specified by the interface and regardless of how the combination is stored, the combination must be passed to the methods of the class as an integer in both implementations.
This means that the LockString
implementation will need to convert the integer argument to a string.
Compile both classes and write a small test program LockTest
to ensure both implementations work.
A reference variable of an interface type can "hold" an object of any class that implements that interface. For example, consider the following portion of a test program:
Lock myLock; myLock = new LockInt(345, true); myLock.changeCombo(345, 777); if (myLock.open(777)) System.out.println("Hey, I'm in!");
Notice that the only part of the program that refers to the specific implementation is the assignment statement on the second line. From then on, we only use the public methods available in the interface, regardless of which implementation we have picked. Try the above code and ensure your program works.
Now change the second line to:
myLock = new LockString(345, true);
and confirm that the rest of the program still works correctly. If it doesn't, seek help!
This is a powerful feature of object-oriented programming that we will be using throughout the unit to define and implement ADTs.
Using javadoc
As described in the lecture notes and the Java Programming Conventions document, all code submitted for assessment in this unit must be documented using the javadoc
automatic documentation facility.
Fully document your combination lock code of Question 1. You may need to reread Topic 4 of the lecture notes to understand how you can appropriately "mark-up" the code you are writing.
Using a graphical IDE like Eclipse you should be able to automate the documentation generation process within the IDE, forcing an update to the javadoc
documentation whenever the code is recompiled within the IDE.
Achieving this probably requires modification of the project's build.xml
XML build file - a file used by the ant
program to determine which commands to execute when your code is recompiled.
To learn more, and to get a better idea of what XML commands do and what additional commands are at your disposal, look at the man page for the ant
program or at the online manual on the Apache Ant Project web-site.
Don't be afraid to try things!