We will now add two operations to the AddressBook class, one to put an entry into an AddressBook and another to look up an address for a name.
To add a put Operation to AddressBook
- Click on the AddressBook symbol on the Class Diagram. Click on the little Create Element button at the top right of the Class symbol, and select Operation.
- Type put as the name of the Operation and, inside the parentheses, type the parameter list name: String, address: String, then press Return.
- Click on the new put Operation, and, in the Alf editor window, press Create.
Type the Alf code shown in the following figure.
- When the text is correct, press Save.
To add a get Operation to AddressBook
- Click on the AddressBook symbol on the Class diagram. Click on the little Create Operation button to the top right of the Operations compartment.
- For the operation and its signature, enter get(name: Sting): String[0..1] and press Return. (Make sure you don't leave the default "()" at the end of what you enter.)
- Click on the get Operation, and, in the Alf editor window, press Create.
Type the Alf code shown in the following figure.
- When the text is correct, press Save.
You may want to consider yourself how to code a remove(name: String) Operation that removes an entry from an AddressBook.
Next: Testing the AddressBook
Related Pages