5b

Advanced Placement

Computer Science Lab

 

 

Interfaces

 

 

 

 

Introduction

 

In general people think of an interface as the relationship between two objects.  For example, an instrument can interface with a computer.  In the Java programming language an interface has a particular meaning in that an interface is a collection of constants and abstract methods.  When these method a declared abstract, the only thing that is included is the header and parameter list of the header.  The header is then followed by a semicolon, unlike the method header in a normal class.  The other distinguishing characteristic of an interface is that it cannot be instantiated.  When an interface is used it is implemented.  When an interface is implemented the user must write code for each of the methods that has be declared in the interface.  Use of an interface ensures that the programmer will write a certain set of methods in the implementing class.  A class can implement several interfaces.

 

 

 

 

Programming Assignment

 

Design an interface called Lockable that includes the following methods: setKey, lock, unlock, and locked.  The setKey, lock, and unlock methods take an integer parameter that represents the key.  The setKey method establishes the key.  The lock and unlock methods deny or permit access to the methods of a class.  The locked method returns a boolean of true for locked and false if unlocked.  A lockable object is an object whose methods and/or data are protected by the methods of the interface.  Once you have created the interface, implement a version of the Account class from Chapter 4 so that it can be locked.  Design a client class with an appropriate dialog box that obtains a password from the user that is the key.  Have the program print out some account data.

 

Turn in a program listing your source code.  Source code should use proper formatting.  Also turn in a copy of your output as in previous assignments.  This can be obtained by running the program and then using AltPrintScreen to copy the window of the output onto the clipboard. Then paste the contents of the clipboard into WordPad and print out the document. The code should be organized with the main method on type, then your interface, and then your class.