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, select Create Method > Behavior, and then select Opaque Behavior (or Activity).
- Click on the put Operation again, and open the Alf editor window (select Windows > Alf), if it isn't already open.
Type the Alf code shown in the following figure.
- When the text is correct, click 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 new get Operation, select Create Method > Behavior, and then select Opaque Behavior (or Activity).
- Click on the get Operation again, and open the Alf editor window (select Windows > Alf), if it isn't already open.
Type the Alf code shown in the following figure.
- When the text is correct, click 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