Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
searching using arrays

Posted by LostInJava (LostInJava), 6 May 2004
hi am stuck on arrays  

can anyone please tell me how to make a phonebook search using an array? it has to be in alphabetical order (the names) and i aint got no clue how to do it. i have to add an entry and then be able to search for it. any addionatal help would be appreciated. please help me asap as i gota do this asign pretty soon!
thanks in advance

Posted by admin (Graham Ellis), 6 May 2004
How far have you got so far?   Is the problem getting data into the array, or sorting it?   Are you using strings or "proper" telephone entry objects?  Did you mean array or ArrayList?

Very happy to look at code and comment / assist with specific problems ... but this really isn't a "please write my  assignment for me" service    ... also note my comment on urgent answers in the "Assistance" section, "Answers on Opentalk" thread -- Graham

Posted by LostInJava (LostInJava), 6 May 2004
hi thanks for the speedy reply
i have the code here first:
I have done 2 classes so far and this is the code written below:

public class PhoneBook
{
   private static final int BOOK_SIZE = 5;
   private PhoneBookEntry [] entryList;
   private int nextFreeLocation;
   
   public PhoneBook()
   {
       entryList = new PhoneBookEntry [BOOK_SIZE];
       nextFreeLocation = 0;      
   }
   
   public void addEntry(String theName, long theNumber)
   {
       if(nextFreeLocation == BOOK_SIZE)
       {
           entryList[BOOK_SIZE -1] = new PhoneBookEntry(theName, theNumber);
       }
       else
       {
           entryList[nextFreeLocation] = new PhoneBookEntry(theName, theNumber);
           nextFreeLocation++;
       }
   }
   
   public
   
}

---------
public class PhoneBookEntry
{
   private String name;
   private long number;
   
   public PhoneBookEntry(String givenName, long givenNumber)
   {
       name = givenName;
       number = givenNumber;
   }
   
   public String getName()
   {
       return name;
   }
   
   public long getNumber()
   {
       return number;
   }  
}


-----------------------

now the scenario i was given is :

Mobile phones offer the facility for creating personalised phonebooks of a limited capacity and typically allowing for the storage of the name of a contact and associated phone number.
It is usually possible to order the entries held in a phonebook in a variety of ways. For example, entries may be ordered by frequency of use, alphabetically on the name of the contact or in a sequence specified by the user of the phone.
Retrieving a particular number from a phonebook can be done in a variety of ways. Typically by providing the name, or a part of the name, of a contact or by specifying a location with in the book.
Also facilities for removing a contact from the book or changing the number associated with a contact are provided.

and the questions i need to answer are:
1) Adding a new entry
a)      A new entry may be added to the phonebook in the next vacant location.
b)      A new entry may be added to a specified location in the phonebook. The location being specified by an integer value. How to resolve the problem of the specified location already being occupied is up to you.


2) Retrieving a phone number
a)      Giving the integer value that specifies the location may retrieve the phone number of an entry at a particular location in the phonebook.
b)      Providing a name will retrieve the phone number associated with that name.
c)      Providing no more than the first three letters of a name will retrieve the phone number associated with that name. How to deal with multiple names that start with the same three letters is a problem that you have to resolve.

3) Modifying entries
a)      The phone number associated with a particular name may be changed.
b)      The location of a particular entry in the phone book may be changed
c)      A particular entry may be removed from the phone book.

am really stuck on the best way to do a search facility and how can i edit any name or number i have stored
any ideas
thanks for looking

musteek taoy

Posted by admin (Graham Ellis), 7 May 2004
on 05/06/04 at 21:59:07, LostInJava wrote:
am really stuck on the best way to do a search facility and how can i edit any name or number i have stored
any ideas


Search ...

How about providing a number of search methods in the PhoneBook class such as "getbyname". The getbyname method would loop through each element of the PhoneBook to which it applied (probably a for loop) and check the string returned by getname from each PhoneBookEntry to see it it matched.  You might like to use equalsIgnoreCase for the test, on the assumption that users don't always get their capitalisation right.

Edit ...

The approach I would take would be to add methods to the PhoneBookEntry class to set value - thus setname, etc.

Another matter to consider in your code structure ...

You've limited your phone book entries to five, and you're silently overwriting the last entry if someone tries to enter more. In my view, this is not good practice to put it mildly. You really ought to be using a Vector or an ArrayList .... of if you MUST use an array for some reason I'm not aware of, you should throw an exception when the limit is reached.  In general it is very poor programming practice to write constant limits into a program ... five phone numbers may seem like a lot, but when I think about it you could reach me on six different numbers  

Posted by admin (Graham Ellis), 8 May 2004
This topic seems to have degenerated into "please do my coursework for me" via personal messages.  Quite happy to help you learn, but doing complete assignments for people is NOT our purpose so I'm locking the topic.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho