Lab 6: Auto Complete
Overview
This is the first week of a multi-week project. The project will allow you to integrate learning from throughout the freshman software development and data structures sequence. You will develop several data structures and analyze the advantages and disadvantages of each. The project will result in a user interface showing the results.
Assignment
Your program must have a user interface similar to the one shown here.
Users may type characters into search box. The list of matches below must be immediately after a letter is typed or deleted. In addition, the time required to find an exact match and all matches with the same prefix should be displayed in appropriate text boxes for both the sorted list and unsorted list data structures.
Details
AutoCompleter
interface
You must create the AutoCompleter
interface with the following methods:
boolean add(String word)
— returnstrue
ifword
is added to the object. Ifword
isnull
or an empty string, anIllegalArgumentException
is thrown.int size()
— returns the number of items contained by the object.boolean exactMatch(String target)
— returnstrue
iftarget
is found in the object. Iftarget
isnull
, anIllegalArgumentException
is thrown.String[] allMatches(String prefix)
— returns an array of all the strings in the object that begin with theprefix
. Ifprefix
is an empty string, an array of all the strings in the object are returned. Iftarget
isnull
, anIllegalArgumentException
is thrown.static String format(long nanoseconds)
— returns a human-friendly string representing the number ofnanoseconds
. The format of the string must be consistent with the examples below. Select the first line where the first number is at least1
. Note: this method is declaredstatic
which means that it should be implemented in the interface.- 2 day(s) 5 hour(s) 32 minute(s)
- 14 hour(s) 22 minute(s) 8 second(s)
- 42 minute(s) 55.3 second(s)
- 18.8 second(s)
- 998.8 millisecond(s)
- 318.8 microsecond(s)
- 7 nanosecond(s)
Unsorted Collection Implementation
Implement the AutoCompleter
interface using an unordered collection. Your class
must have one private attribute: private final Collection<String> items
that is
assigned via a one-argument constructor. Note: the constructor should remove any
strings in the collection passed to the constructor.
Sorted List Implementation
Implement the AutoCompleter
interface using a sorted list. Your class
must have at least the following private attribute:
private final List<String> items
that is assigned via a one-argument
constructor. Recommendation: add the items in whatever order they are
given, then ensure that items
is sorted before searching for matches.
Use Collections.binarySearch()
in your implementations of exactMatch()
and allMatches()
.
Exception Handling
If any problems are encountered with reading the input file with the word list, the program should display a useful error message to the console and terminate gracefully. The program should not crash or display any exceptions.
Just For Fun
Ambitious students may wish to:
- Add
allNonPrefixMatches(String substring)
to the interface. This method returns all strings that contain the substring passed as an argument.
Acknowledgment
This laboratory assignment, developed by Dr. Chris Taylor.