The second test will be given in class on Wednesday, November 9. It covers Sections 10.1 through 10.4 and Sections 11.1 through 11.4. Note that sections 10.5 and 11.5 are not included. There will be no questions that are specifically about earlier material; however, you should know that material and you might need to know it for some questions. (In particular, questions about the classes in the JFC might require some knowlege of the actual data structures that they use internally, which were covered in Chapter 9. Also, there could be questions about run time efficiency that use what you learned about that copy back in Chapter 8.)
The format will be similar to the first test: There will be some definitions and essay-type questions. Some questions will ask you to write code segments or methods or possibly even complete classes. There might also be some questions that ask you to read some code and figure out what it does.
Here are some terms and ideas that you should be familiar with:
Generic programming what problem is solved by generic programming parameterized classes in Java, such as ArrayList<T> the Java Collection Framework (JCF) working with generic lists, sets, and maps the JFC does not work with primitive types wrapper classes for primitive types: Integer, Double, Boolean, Character conversion between primitive types and wrapper classes: autoboxing and unboxing using wrapper classes with the JFC the class Object, the superclass for all classes methods in class Object that are often overridden: toString(), equals(x), hashCode() the interface Comparable<T> and its method compareTo(x) traversing collections; what's required to do so in a uniform way interators the interface Iterator<T> and the method coll.iterator() for a Collection Iterator methods: iter.hasNext, iter.next(), and iter.remove() using a "for-each loop"; example: for ( String str : strList )... using sets to store collections with no duplicates using TreeSet to store sets in increasing order (based on the compareTo() method) implementing set operations with addAll(), removeAll, retainAll() using maps to associate values to keys hash tables and hash codes the structure of a hash table: an array of linked lists how hash codes are used run-time efficiencies of various operations comparing run times for operations on ArrayList and LinkedList find, insert, remove operations on TreeSets/TreeMaps run in Θ(n*log(n)) time find, insert, remove operations on HashSets/HashMops run in Θ(1) time parameterized interfaces and classes in the JFC Collection<T>; methods: add(x), remove(x), contains(x), size(), clear(), isEmpty(), iterator(), addAll(collection), removeAll(collection), retainAll(collection) List<T>; extra methods: get(index), set(index,x), remove(index) ArrayList<T> LinkedList<T> Set<T> HashSet<T> TreeSet<T>; extra methods: subSet(s,e), headSet(e), tailSet(s) Map<K,V>; methods: put(k,v), get(k), containsKey(k), keySet() HashMap<K,V> TreeMap<K,V> streams for input and output files how streams and files are abstractions and why that is important binary streams versus character streams character sets and character encoding; for example, ISO-8859-1, UTF-8, UTF-16 InputStream and OutputStream Reader and Writer IOExceptions PrintWriter: the methods print(), println(), checkError() files, directories, and file paths the File class; methods: exists(), length(), canRead(), canWrite(), isDirectory() FileReader and FileWriter; FileInputStream and FileOutputStream using a Scanner to read from a text file using a PrintWriter to write to a text file designing a format for a data file why use human readable (character) files? computer network network protocols IP: unreliable delivery of packets to a computer on the Internet TCP: reliable, two-way communication between programs IP addresses and port numbers host names; translation of host names to IP addresses the client/server model for TCP connections serverSocket.accept() and new Socket(host,port) are "blocking" operations the ServerSocket class: constructor: new ServerSocket(port) serverSocket.accept(); the Socket class: constructor: new Socket(host, port) socket.getInputStream() and socket.getOutputStream()
1. There are two Set classes in Java, TreeSet and HashSet. What's the difference? How do the run times of their operations compare? Why might you choose to use one rather than the other?
2. What is a hash code, and how are hash codes used in a hash tables?
3. The Java Collection Framework uses parameterized types such as ArrayList<T> and TreeMap<K,V>. Discuss parameterized types: What can you do with them, and why do they exist in the Java langauge? What do parameters like the "T", "K", and "V" represent?
4. What is meant by the client/server model for network communication.
5. Suppose that a variable named words
is of type TreeSet<String>.
(a) Write a code segment that uses a "for-each"
loop to print out all the words in this set.
(b)The set is represented as a TreeSet rather than a HashSet. What consequence
does this have for the output in part (a)?
(c) Under what circumstances might you prefer to use a HashSet rather than
a TreeSet? Why?
6. Suppose that a variable named symTab is of type HashMap<String,Double>,
and that symTab is used to represent a symbol table that associates values to "variable" names.
(a) Write a Java statement that will associate the value 3.14 to a variable named pi.
(b) Write a Java statement that will print out the value associated to the variable named foo.
(c) Suppose that you want to add 1 to the value associated with the variable named count.
Write one or more Java statements that will do this. (You want to change the value that
is stored in the symbol table, not just compute the answer!)
7. A file named "data.dat" contains integers. Write a program that will read the integers from this file and create a new file named "result.dat" that contains the same integers, but sorted and with duplicates removed. You can do this using only the classes listed in the import statements shown below. You can use one big try..catch to handle errors.
import java.util.Scanner; import java.util.TreeSet; import java.io.File; import java.io.PrintWriter; public class Uniq { public static void main(String[] args) {
8. Write a Java code segment that will create a HashSet<Integer that contains exactly 10 different numbers chosen at random from the range 0 t0 99. (Hint: In the end, the size of the set must be 10.)
9. Write a Java code segment that will create a client Socket to connect to a server on math.hws.edu, port 20000. It should read one line of text from the server and output that line to System.out, then close the connection.