Object-oriented programming Exercise Exercise 1 Object-oriented programming in Java Warm-up Exercise Your login id is j2meNN and the initial password is j2meNN00NN, where NN is the number on your address label. Please change the password! The URL for the course is: http://www.cs.bham.ac.uk/~mdr/Sendo. Please visit it and bookmark it. Visit this exercise in the web browser, so that you have access to the links. The Wireless Toolkit's shortcuts can be found under "My Documents" and inside "J2ME Wireless Toolkit ...". You will be using the KToolbar program in this folder. Copy shortcuts to "KToolbar", "Documentation" and "Run MIDP Application" to your Desktop. Also, run the "Default Device Selection" shortcut, and choose "DefaultColorPhone" as the preferred emulator. Your file space is on drive Z. Your applications will be created in the directory Z:\WTK104\apps. Take a look at this folder. Use the KToolbar program and create a new application (project) called Hello. In the pop-up dialog box, enter the following data:
Project Name: Hello
MIDlet Class Name: HelloMIDlet
A new directory Hello should get created under Z:\WTK104\apps. Go to the directory Z:\WTK104\apps\Hello\src. Save the following file there. Make sure that file name was not mangled by the browser.
HelloMIDlet.java
You are going to be creating your first J2ME program in a file called Hello.java in the src directory. Use one of the editors in the directory J:\Shortcuts for this purpose (med, Jedit, or Xemacs). Type into Hello.java the Java code for a class called Hello. This class should contain a single method with the following type:
public String warmUp (int n)
The method should return an arbitrary string of your choice, e.g., "My first J2ME class," with the integer n appended to it. Use the Build command of KToolbar to compile the code. If you get error messages, check with the lab demonstrators to figure out what they mean. To test the code, use the Run command of KToolbar. This brings up a mobile phone emulator with the Hello application. The user interface should be self-explanatory. PhoneBook Exercise This exercise asks you to create a PhoneBook class that stores names and phone numbers and allows the operations of look-up, insertion and deletion. You are given a LinkedList class that maintains a traversable linked list of strings. You will be using this to create a PhoneBook. You would want to create two linked lists in a PhoneBook, one for storing names and one for storing phone numbers. Make sure that the two lists are traversed in lock step so that you get the corresponding names and numbers. Requirements for the PhoneBook class Constructor: PhoneBook() - creates a new empty phone-book. Instance methods: void insert(String name, String number) Inserts a new entry in the phone-book. The entry can be inserted anywhere, but it is advisable to insert it at the end, in order to avoid confusion to the user. void lookUp(String prefix, StringBuffer name, StringBuffer number) throws NotFoundException Looks up the entry of a name starts with the given string prefix. If no such entry exists, throws the exception. If an entry is found, appends the name of the entry to the StringBuffer name and the phone number of the entry to the StringBuffer number. void repeatLookUp(String prefix, StringBuffer name, StringBuffer number) throws NotFoundException Like lookUp, but continues search for another entry that begins with the String prefix. void delete() Deletes the entry last looked up. The given LinkedList class The following LinkedList class is given for your use in creating PhoneBook. Constructor: LinkedList() - creates a new empty linked list. Instance methods: void reset() - sets the cursor to the beginning of the linked list. void advance() - advances the cursor by one position. boolean hasElements() - returns true if the cursor is not at the end of the list, and false otherwise. void moveToEnd() - moves the cursor to the end of the list. Insertion is possible at this position, but no look-up or deletion. String current() - returns the String stored at the current cursor position. void insert(String s) - inserts s at the current position. void delete() - deletes the element at the current cursor position. Other given classes PBMIDlet - a test driver user interface for testing the PhoneBook class. You don't need to worry about how this is implemented. NotFoundException - an exception class to be used in PhoneBook methods. It has a single constructor with no arguments. String - Built-in class from the java.lang package, for immutable string objects. See the WTK documentation. StringBuffer - also from the java.lang package, for mutable strings. See the WTK documentation. Practicalities You can find a sample running application by downloading this jad file, and running it using the "Run MIDP Application" shortcut. Use the KToolbar program and create a new application (project) called PhoneBook with the following data:
Project Name: PhoneBook
MIDlet Class Name: PBMIDlet
Save the following files in the directory Z:\WTK104\apps\PhoneBook\src. LinkedList.java NotFoundException.java PBMIDlet.java Type your code into a file called Phonebook.java in the src directory and follow the same procedures as in the warm-up exercise. You can start by creating a skeleton PhoneBook.java file with all the method bodies empty. With this skeleton code, the Build command of KToolbar should be able to compile your code and report any problems. You can then fill in the method bodies one by one. You can place arbitrary testing code of your choice in the constructor of PhoneBook. This will be called when the PhoneBook application is launched in the mobile phone emulator. The messages printed using System.out.println will appear in the KToolbar console. Back to contents � 2003 Russell Beale, Uday Reddy and Mark Ryan