Do not turn to the next page until the start of the exam.

Similar documents
Do not turn to the next page until the start of the exam.

Final Exam CS 251, Intermediate Programming December 13, 2017

PIC 20A GUI with swing

CSC Java Programming, Fall Java Data Types and Control Constructs

CS 180 Final Exam Review 12/(11, 12)/08

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

First Name: AITI 2004: Exam 2 July 19, 2004

First Name: AITI 2004: Exam 2 July 19, 2004

PIC 20A Streams and I/O

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Midterm assessment - MAKEUP Fall 2010

Data Abstraction and Specification of ADTs

CS 251 Intermediate Programming GUIs: Components and Layout

PIC 20A Number, Autoboxing, and Unboxing

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Introduction to Programming Using Java (98-388)

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

CS 61B Discussion 5: Inheritance II Fall 2014

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Exceptions and Design

1.00/ Introduction to Computers and Engineering Problem Solving. Quiz 2 / November 10, 2005

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Basic Data Structures

Declarations and Access Control SCJP tips

What can go wrong in a Java program while running?

Basic Data Structures 1 / 24

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

CIS 110 Spring 2014 Introduction to Computer Programming 12 May 2014 Final Exam Answer Key

CS2113 Lab: Collections 10/29/2018

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

CS-152: Software Testing

Object-Oriented Programming Design Topic : Exception Programming

Name: Pennkey: CIS 120 Final Exam December 21, Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam.

Course Status Polymorphism Containers Exceptions Midterm Review. CS Java. Introduction to Java. Andy Mroczkowski

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

CSCI-140 Midterm Review October 10, 2015 Presented by the RIT Computer Science Community

CSCI-142 Exam 2 Review November 2, 2018 Presented by the RIT Computer Science Community

Throughout the exam, write concisely and underline key words or phrases. Have fun! Exam 2. Week 7 (Winter 2013). Dr. Yoder. Sec 031.

Review sheet for Final Exam (List of objectives for this course)

CS159. Nathan Sprague

Model Solutions. COMP 103: Test April, 2013

CS 211 Programming Practicum Spring 2017

Input from Files. Buffered Reader

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

About this exam review

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

CMSC 331 Second Midterm Exam

VALLIAMMAI ENGINEERING COLLEGE

Give one example where you might wish to use a three dimensional array

MIT AITI Swing Event Model Lecture 17

More Data Structures (Part 1) Stacks

CS 180 Fall 2006 Exam II

CS 3 Introduction to Software Engineering. 3: Exceptions

Come & Join Us at VUSTUDENTS.net

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Full file at

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

1.00 Introduction to Computers and Engineering Problem Solving. Final Examination - May 19, 2004

CSC 1351 The Twelve Hour Exam From Hell

CIS 120 Final Exam 15 December 2017

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

1. (5 points) In your own words, describe what an instance is.

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

Data Structures. COMS W1007 Introduction to Computer Science. Christopher Conway 1 July 2003

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CS165 Practice Final Exam

Midterm Exam CS 251, Intermediate Programming March 12, 2014

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Midterm Exam 2 CS 455, Spring 2011

Algorithms & Datastructures Laboratory Exercise Sheet 1

CS56 final (E03) W15, Phill Conrad, UC Santa Barbara Wednesday, 03/18/2015. Name: Umail umail.ucsb.edu. Circle one: 4pm 5pm 6pm

CS61BL Summer 2013 Midterm 2

Interfaces & Generics

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Fun facts about recursion

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Stacks Goodrich, Tamassia

Window Interfaces Using Swing Objects

Associate Professor Dr. Raed Ibraheem Hamed

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

Lecture 5: Java Graphics

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Stacks and Queues. Chapter Stacks

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

CS165 Practice Final Exam Answer Key

CIS 120 Final Exam December 17, Name (printed): Pennkey (login id):

Stack Implementation

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15

Stacks Fall 2018 Margaret Reid-Miller

Question 1. (2 points) What is the difference between a stream and a file?

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

RAIK 183H Examination 2 Solution. November 10, 2014

Transcription:

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