Principles of Java Language with Applications, PIC20a E. Ryu Winter 2017 Final Exam Monday, March 20, 2017 3 hours, 8 questions, 100 points, 11 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) An inner class inherits the outer class. A. True B. False 2. (2 points) When you override equals you should override hashcode. A. True B. False 3. (2 points) Exceptions are Objects. A. True B. False 4. (2 points) You can use the enhanced for loop with A. Iterator<T>. B. Iterable<T>. C. both Iterator<T> and Iterable<T>. 5. (2 points) Consider the following code. public class Test { public static void main ( String [] args ) { SomeClass c1 = new SomeClass (); SomeClass c2 = new SomeClass (); SomeClass c3 = new SomeClass (); class SomeClass { public double field1, field2 ; static { System. out. println (" This ran "); public SomeClass () { field1 = 0; field2 = 0; How many times is This ran printed to the command line? A. 0 B. 1 C. 2 D. 3 E. 4 F. 5 Page 2
6. (30 points) Write a class named EscapedReader that reads only letters and numbers and skips over everything else. Make EscapedReader 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. For example, say the file testfile.txt contains The term <I>nuée ardente,</i> or "glowing cloud" was first used by La Croix (19 04) in his description of the volcanic flows he observed in the 1902 eruption of Mt. P elée, a historically active volcano on the island of Martinique. and we run the code import java.io.*; public class Test { public static void main ( String [] args ) { Reader r = null ; try { r = new FileReader (" testfile. txt "); //r = new EscapedReader (r); while ( true ) { int nextchar = r. read (); if ( nextchar == -1) break ; System. out. write (( char ) nextchar ); catch ( IOException e) { System. out. println (" Exception " + e + " happened "); finally { try { r. close (); catch ( IOException ex) { System. out. println (" Stream close failed "); The the contents of testfile.txt are displayed on the command line. commented line, i.e., when we replace r = new FileReader (" testfile. txt "); //r = new EscapedReader (r); When we uncomment the with r = new FileReader (" testfile. txt "); r = new EscapedReader ( r); we want the output to be ThetermnueacuteeardenteorglowingcloudwasfirstusedbyLaCroix1904inhisdescriptionofthevol canicflowsheobservedinthe1902eruptionofmtpeleacuteeahistoricallyactivevolcanoontheisla ndofmartinique Page 3
The specification of EscapedReader is as follows. EscapedReader has one public constructor public EscapedReader ( Reader in) which appropriately initializes the EscapedReader based on the input Reader in. (So EscapedReader is a wrapper class for Reader.) EscapedReader overrides the method public void close () throws IOException which closes the Reader the current EscapedReader uses. EscapedReader overrides the method public int read () throws IOException read() reads and returns a single character that represents a letter or a digit. If there are no more such characters to read, return -1. You ll want to use the method public static boolean isletterordigit(char ch) of class Character. So Character.isLetterOrDigit(c) returns true if c is a char that contains a letter or digit and false otherwise. Finally, EscapedReader appropriately throws Exceptions thrown by the Reader it uses. EscapedReader doesn t otherwise cause new Exceptions. Remark. When a system processes a user s input, special characters sometimes pose a security threat, and malicious users (i.e., hackers) can exploit this. One counter measure to this is to make sure your program has no bugs (which doesn t work very well). A better counter measure is to filter out (i.e., escape ) all characters that are deemed risky. For example, in an attack called SQL injection, a malicious user enters 1;DROP TABLE users as a username. Because of the semicolon, this will cause the system to drop (i.e., delete) all users from the database. If special characters are escaped, the input becomes 1DROP TABLE users and no harm is done to the system. Page 4
Page 5
7. (30 points) A stack is a data structure that allows duplicates and supports last-in, first-out (LIFO) access. (Queues support FIFO access, while stacks support LIFO access.) With LIFO access, you retrieve the most recently added element. Write a generic class named MyStack<E> with the following specification. MyStack<E> has one public constructor public MyStack () which creates an empty MyStack<E>. MyStack<E> has the method public int size () which returns the number of elements within the stack. MyStack<E> has the method public void push_ back ( E e) which adds an E to MyStack<E>. MyStack<E> has the method public E pop_ back () throws EmptyStackException which retrieves and removes the E that was most recently added to MyStack<E>. java.util.emptystackexception if MyStack<E> is empty. pop_back throws MyStack<E> has the method public E peek () which retrieves but does not remove the E that was most recently added to MyStack<E>. peek returns null if MyStack<E> is empty. Page 6
For example, we could use MyStack<E> as follows. public class Test { public static void main ( String [] args ) { MyStack < Integer > stack = MyStack < >(); stack. push_back (1); stack. push_back (2); stack. push_back (3); System. out. println ( stack. pop_back ()); // output 3 System. out. println ( stack. peek ()); // output 2 System. out. println ( stack. peek ()); // output 2 System. out. println ( stack. peek ()); // output 2 System. out. println ( stack. pop_back ()); // output 2 System. out. println ( stack. pop_back ()); // output 1 stack. push_back (1); stack. push_back (2); stack. push_back (3); System. out. println ( stack. pop_back ()); // output 3 stack. push_back (4); stack. push_back (5); System. out. println ( stack. pop_back ()); // output 5 System. out. println ( stack. pop_back ()); // output 4 System. out. println ( stack. pop_back ()); // output 2 System. out. println ( stack. pop_back ()); // output 1 Hint. Make MyStack<E> have a private ArrayList<E>. (However, do not make MyStack<E> inherit ArrayList<E>. That s not what I mean.) Page 7
Page 8
8. (30 points) The following GUI // file DiceRoller. java import javax. swing.*; import java. awt.*; public class DiceRoller { private JLabel label ; private JButton button ; public DiceRoller () { JFrame frame = new JFrame (" Dice Roller "); frame. setdefaultcloseoperation ( JFrame. DISPOSE_ON_CLOSE ); label = new JLabel ("X"); label. sethorizontalalignment ( SwingConstants. CENTER ); button = new JButton (" Roll dice "); frame. getcontentpane (). add ( label, BorderLayout. CENTER ); frame. getcontentpane (). add ( button, BorderLayout. SOUTH ); frame. setsize (300, 200); frame. setvisible ( true ); when run with // file Test. java public class Test { public static void main ( String [] args ) { DiceRoller gui = new DiceRoller (); creates the window Page 9
At this point, nothing happens when we click the button. Add to and modify the provided code so that clicking the 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. Page 10
Page 11