Principles of Java Language with Applications, PIC20a E. Ryu Fall 2017 Final Exam Monday, December 11, 2017 3 hours, 8 questions, 100 points, 9 pages While we don t expect you will need more space than provided, you may continue on the back of the pages. This exam is closed book and closed notes. Student name: Teaching staff signature: Do not turn to the next page until the start of the exam.
1. (2 points) In what version of Java was default methods for interfaces introduced? A. Java 5 B. Java 6 C. Java 7 D. Java 8 E. Java 9 2. (2 points) The antonym (the opposite) of functional programming is A. procedural programming. B. imperative programming. C. object oriented programming. 3. (2 points) The utility class Collections has a function max, which returns the maximum element of a Collection<?> based on a input Comparator<?>. Which of the following is a valid signature of Collections.max? A. public static <T > T max ( Collection <? super T > coll, Comparator <? super T > comp ) B. public static <T > T max ( Collection <? extends T > coll, Comparator <? extends T > comp ) C. public static <T > T max ( Collection <? extends T > coll, Comparator <? super T > comp ) D. public static <T > T max ( Collection <? super T > coll, Comparator <? extends T > comp ) 4. (2 points) You can catch a RuntimeException with a catch block. A. True B. False 5. (2 points) Explain the term autoboxing in one or two sentences. Page 2
6. (30 points) Write class IteratorReader, which reads characters from an Iterator<Character>. Make IteratorReader inherit the redacted version of Reader, which is provided in the documentation; the actual class java.io.reader has more functionality (and therefore more to override to inherit) but we simplified it a bit. import java.io. Reader ; import java. util.*; public class Test { public static void main ( String [] args ) throws Exception { String str = " Hello world "; // convert str to List < Char > ArrayList < Character > c_ list = new ArrayList < >(); for ( int i =0; i<str. length (); i ++) c_list. add ( str. charat (i )); Reader r = new IteratorReader ( c_list. iterator ()); while ( true ) { int nextchar = r. read (); if ( nextchar == -1) break ; System. out. print (( char ) nextchar ); r. close (); An Iterator<Character> allows us to iterate through a sequence of characters. IteratorReader is a wrapper class. The read() method of IteratorReader retrieves the same sequence of characters one could obtain with the Iterator<Character> provided through the constructor. The specification of IteratorReader is as follows. IteratorReader has one public constructor public IteratorReader ( Iterator < Character > itr ) which appropriately initializes the IteratorReader based on the input Iterator<Character>. IteratorReader overrides the method public int read () throws IOException read() returns a single character that represents a letter or a digit. If there are no more such characters to read, return -1. IteratorReader overrides the method public void close () throws IOException which nulls any reference of the Iterator<Character> provided through the constructor. In other words, the IteratorReader will not have access to the Iterator<Character> after close() has been called. Finally, write a short paragraph (3 sentences at most) explaining when IteratorReader throws any Exceptions and justifying why this is a sensible design choice. Page 3
Page 4
7. (30 points) You are a journalist working for a newpaper company with a long history. Out of curiosity, you would like to look up the most recent article that uses the archaic word thou. Articles are represented as Article Objects. public class Article { public string title ; public string fulltext ; public int publicationyear ; public int publicationmonth ; public int publicationdate ; public string author ; Assume fulltext contains English letters and usual proper punctuation. Assume the ArrayList<Article> corpus contains all articles ever published in your paper. public class Test { public static void main ( String [] args ) { ArrayList < Article > corpus ; // somehow load corpus... // start writing code here... Write code that looks through corpus and finds the most recent Article with the word thou in its text or title. Output the search result in any reasonable manner using System.out. If two or more Articles are the most recent, then you can output any one of them. In other words, you can output any one of the winners. If no article contains the word thou, say so. You may not use the methods indexof(...) or lastindexof(...) of class String. Hint. Make sure your code does not think words like thousand or lighthouse are matches for thou. Page 5
Page 6
8. (30 points) The following GUI // file RandomGUI. java import javax. swing.*; import java. awt.*; public class RandomGUI { private JFrame frame = new JFrame (" Random GUI "); private JLabel label = new JLabel (" X"); private JButton button1 = new JButton (" Roll dice "); private JButton button2 = new JButton (" Flip coin "); public RandomGUI () { frame. setdefaultcloseoperation ( JFrame. DISPOSE_ON_CLOSE ); label. sethorizontalalignment ( SwingConstants. CENTER ); frame. getcontentpane (). add ( label, BorderLayout. CENTER ); frame. getcontentpane (). add ( button1, BorderLayout. SOUTH ); frame. getcontentpane (). add ( button2, BorderLayout. NORTH ); frame. setsize (300, 200); frame. setvisible ( true ); when run with // file Test. java public class Test { public static void main ( String [] args ) { new RandomGUI (); creates the window Page 7
At this point, nothing happens when we click the buttons. Add to and modify the provided code so that clicking the top button changes the text X to a random choice of Heads and Tails and clicking the bottom button changes the text X to a random integer between 1 and 6. The completed GUI should display something like when the button is clicked. Use lambda expressions for this problem. If you do not use lambda expressions, you will get partial credit of up to 20 points. Page 8
Page 9