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
Hint. This method is a good place to start coding but note that load makes use of the add methodAlgorithm 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
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 nullPseudo code for the two private helper methods is listed below.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
I don't know how to do any of this at allAlgorithm 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