The List ADT
· Use an abstract data type for a list
· Use a parameter in a method whose type is a class
· Use methods of an existing class
· Access data members within a class
· Chapter 4: The ListInterface interface and lists in general
· Chapter 5: Array-based implementations of list and the AList class
The ADT for a list contains methods to add objects to the list, delete objects in the list, retrieve objects from the list, and determine the number of objects in the list. These methods have been specified in the interface ListInterface.java.
A list may contain any type of object. The ADT methods support any object. In this lab the objects will be characters.
The TestList class in this lab contains a method that creates two lists. These lists are passed as parameters to other methods in the class. The methods for a list (add, remove, getEntry, and getLength) are then used to manipulate the lists.
The AList class contains an implementation of the list ADT specified in ListInterface.java. Modifications are made in this class by directly accessing the data members in the list. Modifications in the TestList class cannot access the data members in the list directly since they are private. The TestList class must call the methods in the AList class to manipulate the list.
- In the TestList class, complete the method readList by adding code after the comment labeled ADD CODE HERE. The method reads characters from a given text file into a list. (The details of reading from a file are done for you, so don't panic!) Follow the specifications given in the method's comments. This method is outside of the class so you pass the list to the method as a parameter. Note that you transform each character into an instance of Java's wrapper class Character so that you have an object that represents the primitive character. Add each character to the list.
- Complete the method displayList (at the ADD CODE HERE comment) to display all of the items in the list on one line. This method is outside of the class, so you must pass the list to the method. Use a for loop to get each character in the list. The loop ends once the counter control variable reaches the length of the list. The Character method charValue returns the primitive character in a Character object.
- Complete the method displayListBackward (at the ADD CODE HERE comment) to display all of the items in the list in reverse order on one line. This method is outside of the class, so you must pass the list to the method as a parameter. Use a for loop that starts with the position of the last character in the list (use the getLength method for the list) and decrements by 1 for each iteration of the loop. The Character method charValue returns the primitive character in a Character object.
- Complete the method replaceElement (at the ADD CODE HERE comment) to replace an element in the list, given its position. Remove the element from the position and add the new element to this position. This method is outside of the class, so you must pass the list to the method as a parameter.
- Complete the method test. There are three places to insert additional code. Each area has a comment ADD CODE HERE.
- Get the length of the list by using the getLength method for the list and saving the result in a new variable.
- Replace the *'s in the list with blanks using a for loop that gets each character in the list. Check the value of the character using the charValue method and use the replace method for the list when the character is an *.
- Remove the blanks from the list.Use a for loop to traverse list backwards to avoid getting positions that no longer exist after entries have been deleted. Check the value of the character using the charValue method and use the remove method for the list when the character is a blank.
- Complete the method copy to make a copy of a list that you pass as a parameter. Add code in the for loop that retrieves each element in the list and use the add method for the other list to add the character.
- Close the file TestList.java and open AList.java. Another way to replace an item on a list is to have a method replace within the class implementing the list. Complete the method replace in AList. Since this method is within the class, you have direct access to the private data. Thus, your implementation should differ substantially from the one that you wrote for replaceElement in the TestList class. (See the specifications in ListInterface.) Remember to check the value of position so you do not try to access an element outside the bounds of the array
- Complete the method equals that determines when two lists are equal, that is, when they contain the same characters in the same order. Since this method is within the class AList, you have direct access to the private data. First check if the lengths are equal. If the lengths are equal, use a for loop to compare corresponding pairs of elements from each list.
- Open the file TestList.java; in the method test, remove the two lines of comments labeled REMOVE THIS LINE AND THE SIMILAR LINE LATER IN THIS FILE. Create a program to call the test method in the TestList class.