Download Lab 5

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lab 5
Java Collections Framework
Objectives
In this lab, you will learn how to
- Use and compare various collection classes in JCF
- Use the Comparable and Comparator interfaces
- Use Java reflection features
There is a wealth of collections types and implementations are provided in the Java
Collections Framework (JFC). In this lab, you will first taste the different flavors of
these interfaces and classes through an interesting sample program. Then you will be
asked to select the right class in solving a problem similar to the word counter program
listed in your text.
A Program Full of Fun
Use the CollectionTest.java class for this and the next section.
1. Lists. Compile and execute the program and save the output to a text file. Open
the source code and the result file side by side to answer the following questions:
a. How many different classes that implement the Collection interfaces
are used in the haveFun method? (You can use the class diagram in the
Collection Interfaces – Summary file under the \lecture folder for a
reference.)
b. How are these classes instantiated and populated?
c. What can you conclude from the result for the ArrayList class?
d. What classes have the same output as the ArrayList class? Try to
explain.
2. Iterators. Each Collection class has an Iterator associated with it. The
LinkedList class has both an Iterator and a ListIterator. Use the Java
online API specification (http://java.sun.com/j2se/1.4.2/docs/api/) and answer the
following questions:
a. Describe the relationship between Iterator and ListIterator.
b. What features do them have in common?
c. What ListIterator feature does that this program used and is not
available in the Iterator interface?
d. List one more feature that is not available with an Iterator object.
3. Java reflection. Simply put, reflection is the ability of a program to analyze its
objects and their capabilities at runtime, without knowledge of the specific type
the objects are in. We’ve learned that the Object class is the root of all Java
classes. There is a Class class in the java.lang package, of which you can
think as a type descriptor.
a. The Object class defines a getClass method, which returns the
runtime type of that object. Describe how this method is used in the
sample program.
b. Read the Java online API specification to suggest a way of retrieve all the
classes to which a given class is a subtype, but exclude the interfaces that
this class implements? (Hint: use getClasses and isInterface
methods.)
c. More reflection classes are available in java.lang.reflect package,
such as the Constructor class used in this program. You know that
classes in the java.lang package can be used in any programs without
an import statement. Can you use the Constructor class in the same
manor? Why?
d. Describe the way in which this program instantiates the Map objects.
What will you then comment on the two different approaches used in this
program: the way the Collection objects are created versus the way
the Map objects are created?
4. Maps. Let’s go back to the output file to look at the results for the Map types.
a. Compare results for three different Map types.
b. Compare the results along the implementation styles, namely, hash,
linked-hash, and tree.
Hands-on Exercises
Let’s have more fun by getting your hands dirty. You will be asked to refactor and
enhance the sample program, and modify the word counter program discussed in the
class.
5. Refactoring. Refactor the code segment in the haveFun method that instantiates
the List objects. Use a method (such as loadList) similar to loadMap to
create various List instances with a loop.
6. Change the sorting order. The digit strings are sorted in their natural order in the
sample program: that is the order in which they appear in the English alphabet.
Let’s change the order to something else that you feel like, by add a code segment
that instantiates a TreeSet object with a Comparator defined using an
(anonymous) inner class. You may choose from the following options to order
the entries:
a. The descending alphabetical order
b. The order in which the strings appear in the digits array
c. The length of the strings
d. The sum of the numeric values of all characters used in a string
e. The normal alphabetical order by first reverse each string from the last
character to the first one
f. Any other order you may think appropriate
7. A class report index generator. Assume a Political Science major asks you to
write a program that can generate an index for her class reports. The program
should read her paper from one text file and a list of key terms from another. It
should print out the paper with an index that indicates the numbers of word at
which a key term appears in her paper.
a. You may modify the word counter program given in the text for this
purpose. That program can reads and parse a text file and iterate through
the words and increment a number of occurrences for each distinct word
used in the paper. Your task is to
b. Use the Gettysburg Address and a list of words given in the terms.txt for
test purpose.
This part is used as a homework assignment worth an extra 20 points.
Don’t get stuck on any part of the lab. A sample solution is available in the lab folder.
Due Date
September 30, at beginning of class.