CSE 143 Sp03 Midterm 2 Page 1 of 7 Question 1. (2 points) What is the difference between a stream and a file? Question 2. (2 points) Suppose we are writing an online dictionary application. Given a word (a String) we need to be able to quickly find the definition of that word. Which of the Java collection types List, Set, or Map would be the appropriate data structure for this problem and why? Question 3. (2 points) In the dynamic array implementation of the List data structure, we have to increase the size of the array when we try to add a new item to the list and there is no room available in the array. Two possible choices are to add a fixed number of slots to the list or double the capacity of the list when this situation occurs. Is either of these strategies better than the other? Why?
CSE 143 Sp03 Midterm 2 Page 2 of 7 Question 4. (3 points) Write down the output that is produced when the following code using a stack is executed. Assume that the stack contains String objects only, so no casting is required when an object is retrieved from the stack. Stack s = new Stack(); s.push( banana ); s.push( squash ); s.push( tomato ); String s1 = s.pop(); System.out.println(s1); s.push( broccoli ); String s2 = s.top(); System.out.println(s2); String s3 = s.pop(); System.out.println(s3); Question 5. (3 points) Prove that 42 + 6n 2 + 17n is O(n 2 ).
CSE 143 Sp03 Midterm 2 Page 3 of 7 Question 6. (8 points) Complete the following so it opens a named file, reads the file a line at a time using readline(), and prints on System.out each of the lines in the input file that contains two or more copies of a particular string. If any stream operations generate an exception, print an appropriate message and stop. Example: if target is tar, then a line consisting of the words "tartar sauce" would be printed, while the line "Pine tar" would not be Notes: You do not need to use a JFileChooser to get the name of the file the file name is one of the parameters. Summary information about various stream classes and class String are included on the next page, which you can tear out if you wish. import java.io.*; /** Open the named file and print all lines containing two or * more copies of the target string on System.out. * @param filename the name of the file * @param target the string to search for. Lines in the file * containing two or more copies of this string are * printed on System.out. */ public void printselectedlines(string filename, String target){
CSE 143 Sp03 Midterm 2 Page 4 of 7 Selected reference information about some Java library classes and s. class String int indexof(char ch) int indexof(char ch, int start) int indexof(string str) int indexof(string str, int start) int lastindexof(char ch) int LastIndexOf(char ch, int start) int lastindexof(string str) int lastindexof(string str, int start) class Reader void close() throws IOException int read() throws IOException class Writer void close() throws IOException write(int character) throws IOException first position of ch first position of ch>=start first position of str first position of str>=start last position of ch last position of ch<=start last position of str last position of str<=start class FileReader extends Reader FileReader(String filename) throws IOException class FileWriter extends Writer FileWriter(String filename) throws IOException class BufferedReader extends Reader BufferedReader(Reader in) String readline() throws IOException class BufferedWriter extends Writer BufferedWriter(Writer out) void NewLine() throws IOException class PrintWriter extends Writer PrintWriter(Writer out) void print(type value) void println(type value)
CSE 143 Sp03 Midterm 2 Page 5 of 7 Question 7. (15 points) For this problem, implement an iterator class for a list whose underlying representation is a double-linked list. (Hint: DON T PANIC!! This is basically a question about linked lists. The iterator stuff is only to provide a realistic setting.) The JavaDoc pages for interface Iterator are attached at the end of this exam for reference, although the information you need is included in the comments below. The nodes in the double-linked list are defined by the following class. public class DLink { public Object item; public DLink next; public DLink prev; // one node in a double-linked list // data associated with this link // next link in the list; null if none // previous link; null if none /** Construct a new node referring to the given object */ public DLink(Object item, DLink next, DLink prev) {... Complete the s of class SimpleLinkedListIterator below. You should assume that the iterator class is an inner class inside SimpleLinkedList. That means that your code has direct access to the private variables in SimpleLinkedList; you don t need to add any get s or do something tricky to access the instance variables first and last. You do not need to detect whether any list operations alter the list while the iteration is in progress. You do need to detect whether remove() is used properly. Hint: This is one question where it will really help to draw some pictures and think first before starting to write code. /** A simple List class */ public class SimpleLinkedList { // instance variables private DLink first; // first item in the list, or // null if the list is empty private DLink last; // last item in the list, or // null if the list is empty... other s and s omitted... /** Iterator for SimpleLinkedList class (nested class) */ private class SimpleLinkedListIterator implements Iterator { --- write the code for this class on the following pages ---
CSE 143 Sp03 Midterm 2 Page 6 of 7 Question 7. (continued) /** A simple List class */ public class SimpleLinkedList { private DLink first; // first item in list; null if empty private DLink last; // last item in list; null if empty... /** Iterator for SimpleLinkedList class (nested class) */ private class SimpleLinkedListIterator implements Iterator { // Declare any instance variables you need here /** Construct a new iterator */ public SimpleLinkedListIterator() { // include any necessary initialization here /** Return true if this iterator has more elements */ public boolean hasnext() { /** Return the next element in the iteration * @throws NoSuchElementException if the iteration has * no more elements */ public Object next() throws NoSuchElementException {
CSE 143 Sp03 Midterm 2 Page 7 of 7 Question 7. (concluded) /** Remove from the underlying List the last element returned * by this iterator (the last element returned by next()). * This can only be called once per call to next(). * @throws IllegalStateException if next() has not yet been * called or if remove() has been called after the * most recent call to next(). */ public void remove() throws IllegalStateException { // end of remove // end of SimpleLinkedListIterator class // end of SimpleLinkedList class