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

Similar documents
CSE 143 Sp03 Midterm 2 Sample Solution Page 1 of 7. Question 1. (2 points) What is the difference between a stream and a file?

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.

CSE 143 Au03 Final Exam Page 1 of 15

CSE 143 Au03 Midterm 2 Page 1 of 7

CSE 143 Au04 Midterm 2 Sample Solution Page 1 of 7

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

Java Input/Output. 11 April 2013 OSU CSE 1

Darshan Institute of Engineering & Technology for Diploma Studies

CSE 143 Au03 Midterm 2 Sample Solution Page 1 of 7

CS 251 Intermediate Programming Java I/O Streams

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

File. Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices

CISC 323 (Week 9) Design of a Weather Program & Java File I/O

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

Java Input / Output. CSE 413, Autumn 2002 Programming Languages.

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

23 Error Handling What happens when a method is called? 23.1 What is Exception Handling? A.m() B.n() C.p()

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

PIC 20A Streams and I/O

Active Learning: Streams

Lab 5: Java IO 12:00 PM, Feb 21, 2018

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

CIS 120 Final Exam May 3, Name (printed): Pennkey (login id):

Input from Files. Buffered Reader

Principles of Computer Science I

Special error return Constructors do not have a return value What if method uses the full range of the return type?

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

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

HST 952. Computing for Biomedical Scientists Lecture 8

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

IT101. File Input and Output

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

Answer Key. 1. General Understanding (10 points) think before you decide.

CS 61B Discussion 5: Inheritance II Fall 2014

Object-Oriented Design. March 2005 Object Oriented Design 1

CIS Fall Data Structures Midterm exam 10/16/2012

Byte and Character Streams. Reading and Writing Console input and output

Programming Languages and Techniques (CIS120)

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

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

Exceptions and Working with Files

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

CS 3 Introduction to Software Engineering. 5: Iterators

File I/O Array Basics For-each loop

Chapter 10. IO Streams

File Operations in Java. File handling in java enables to read data from and write data to files

CSE 142 Sp02 Final Exam Version A Page 1 of 14

Simple Java Input/Output

CN208 Introduction to Computer Programming

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

Recursive Objects. Singly Linked List (Part 2)

CSE373 Fall 2013, Midterm Examination October 18, 2013

Programming Assignment Comma Separated Values Reader Page 1

Quiz 1. In class on Tuesday (April 14) Review learning goals (from lecture) Review lectures 1 4, PSA 1 You should be able to:

CSCI E-119 Section Notes. Section 5 Solutions

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Recitation: Loop Jul 7, 2008

Remedial Java - io 8/09/16. (remedial) Java. I/O. Anastasia Bezerianos 1

Chapter 12. File Input and Output. CS180-Recitation

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Programming Languages and Techniques (CIS120)

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

CSC 1214: Object-Oriented Programming

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

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

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

JAVA Programming Language Homework VI: Threads & I/O

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Introduction to Computer Science II (ITI 1121) Final Examination

Lecture 11.1 I/O Streams

1.00 Lecture 30. Sending information to a Java program

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

16-Dec-10. Consider the following method:

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

CSPP : Introduction to Object-Oriented Programming

6.092 Introduction to Software Engineering in Java January (IAP) 2009

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

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

Chapter 4: Loops and Files

CSE 143 SAMPLE MIDTERM

Introduction IS

Exercise 4: Loops, Arrays and Files

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Introduction to Computing II (ITI 1121) Final Examination

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Some Patterns for CS1 Students

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

CSE 143 Sp04 Final Exam Sample Solution Page 1 of 12

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

Introduction Unit 4: Input, output and exceptions

Chapter 4: Loops and Files

Transcription:

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