Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: I'm stuck on this question in Java and we have to make a telephone directory?

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I'm stuck on this question in Java and we have to make a telephone directory?

    What the question states is:


    Having supplied an interface the question arises as to how it should be implemented. There are a
    number of possibilities including the use of an array list or a linked list; however it is perfectly possible, and in fact instructive, to begin by implementing PhoneDirectory using a simple array of DirectoryEntry objects. This approach is illustrated below:
    public class ArrayPhoneDirectory implements PhoneDirectory 
    { 
     private static final int INIT_CAPACITY = 100; 
     private int capacity = INIT_CAPACITY; 
     
     // holds telno of directory entries 
     
     private int size = 0; 
     
     // Array to contain directory entries 
     
     private DirectoryEntry[] theDirectory = 
     new DirectoryEntry[capacity]; 
     
     // Holds name of data file 
     
     private String sourceName = null; 
     
     // Flag to indicate whether directory was modified since 
     it was last loaded or saved 
     
     private boolean modified = false; 
     
     // add method stubs as specified in interface to compile 
     
     ............ 
     
     // add private methods 
     
     ........... 
    }

    Develop a tester class called ArrayPhoneDirectoryTester . As usual all methods in the class being tested should be executed at least once by the test driver. Supply standard test documentation – Test Plan, Test Data and Test Log.

    In addition to the methods described in the interface the class ArrayPhoneDirectory contains three private methods listed below:
    // Searches the array of directory entries for a specific name 
    private int find(String name){} 
     
    // Adds a new entry with the given name and telno to the array of 
    // directory entries 
    private void add(String name, String telno){} 
     
    // Creates a new array of directory entries with twice the capacity 
    // of the previous one 
    private void reallocate(){}

    These methods can be thought of as “helper” methods in that they assist the work of the public methods of the class. For example, the find() method is used by the lookUpEntry() method to locate the position of the a specific directory entry.

    Pseudo-code algorithms for the methods of ArrayPhoneDirectory are listed below
    Algorithm for load

    create a Scanner to read file
    while (not end of file)
    use Scanner to read the name
    use Scanner to read the telno
    create a DirectoryEntry
    add the new entry to theDirectory
    close file
    Hint. This method is a good place to start coding but note that load makes use of the add method
    in case the number of entries stored in the text file has exceeded the initial capacity of the directory.
    So to implement load fully you will need a working version of the ”helper” method add. However
    you can do this in stages by first developing a basic version of add which does not increase the size
    of the directory. In reading data from the file you may assume that the file only contains matched
    pairs of names and numbers. There is no need to consider the case of a name not being associated
    with a telephone number.

    Algorithm for addChangeEntry
    call find to see whether name is in directory
    if name is in theDirectory
    change the telno using setNumber
    return the old telno
    else
    add a new entry to theDirectory using method add
    return null
    Algorithm for lookUpEntry
    use find to locate position of entry
    if entry is found
    use getNumber to return the telno
    else
    return null

    Algorithm for Method save
    create a PrintWriter object
    for each entry in theDirectory
    call getName to get the name from the entry
    write the name on a line
    call getNumber to get the telno from the entry
    write the telno on a line
    close file
    Pseudo code for the two private helper methods is listed below.

    Algorithm for add
    if size >= capacity
    reallocate()
    set theDirectory[size] to new DirectoryEntry(name, telno)
    increment size

    Algorithm for reallocate
    set capacity to 2 * capacity
    set newDirectory to new DirectoryEntry[capacity]
    copy contents of theDirectory to newDirectory
    set theDirectory to newDirectory
    I don't know how to do any of this at all


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I'm stuck on this question in Java and we have to make a telephone directory?

    I don't know how to do any of this at all
    You should talk to your instructor to get some help understanding what is being asked.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm stuck on this question in Java and we have to make a telephone directory?

    Quote Originally Posted by Norm View Post
    You should talk to your instructor to get some help understanding what is being asked.
    I've asked other people and I have a vague idea on what to do and I've emailed my lecturer (but because it's just after Easter and we haven't started back yet) he hasn't got back to me

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: I'm stuck on this question in Java and we have to make a telephone directory?

    In cases like this, usually you just need to start coding. The details of the assignment will become clear progressively.

    Important: Do not write a big bunch of code in one go. You will run into lots of problems. Always write small chunks, test, and once you have confirmed that it works properly, write the next small chunk. Testing in this case can be as simple as writing some println() statements so that you can visually check that the expected values are obtained when you run the program.

    It is hinted that the load method "is a good place to start coding," and I agree because it's pretty straightfoward:
    Algorithm for load

    create a Scanner to read file
    while (not end of file)
    use Scanner to read the name
    use Scanner to read the telno
    ...
    You should have learned how to use Scanner by now, but if you're still unsure, go through Scanning (The Java™ Tutorials > Essential Classes > Basic I/O), and refer to Scanner (Java Platform SE 7 ). Just concentrate on reading the file, and extracting name and telno. As mentioned in my note above, at this point test your code by printing out name and telno to confirm at least this bit of code is working before tackling the next bit.

    ...
    create a DirectoryEntry
    add the new entry to theDirectory
    close file
    DirectoryEntry is a custom class. If you're unsure how to create one, see Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects). The pseudocode above states that you'll need to call the add method after creating a DirectoryEntry. As a start, you can write a simple add method that simply prints out name and telno that are passed to it to confirm that add is indeed called with the correct name and telno. Test it again.

    Once you have confirmed that the above are working properly, you're done with the load method, and you're ready to proceed to the add method.

Similar Threads

  1. Replies: 1
    Last Post: March 17th, 2014, 06:45 PM
  2. Replies: 2
    Last Post: January 25th, 2014, 07:38 PM
  3. make a jtree from a directory
    By ostad in forum Java Theory & Questions
    Replies: 2
    Last Post: July 5th, 2012, 12:35 AM
  4. HELP!!! Entry Class to represent entries in a telephone directory
    By Princess D in forum Java Theory & Questions
    Replies: 10
    Last Post: January 22nd, 2010, 05:39 AM
  5. How to create directory in Java?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:43 AM