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

Similar documents
CS 61B Discussion 5: Inheritance II Fall 2014

Chapter 4 Defining Classes I

CS 251 Intermediate Programming Exceptions

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?

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

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

Java Input/Output. 11 April 2013 OSU CSE 1

CSC Java Programming, Fall Java Data Types and Control Constructs

Problem 1: Building and testing your own linked indexed list

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

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.

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CSE 143 Au04 Midterm 2 Sample Solution Page 1 of 7

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

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

Introduction Unit 4: Input, output and exceptions

40) Class can be inherited and instantiated with the package 41) Can be accessible anywhere in the package and only up to sub classes outside the

Exception-Handling Overview

Core Java Interview Questions and Answers.

CS106A Final Exam Review Session. Saturday Dec. 10, 2016 Nick Troccoli

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Recreation. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

CS-152: Software Testing

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

Programming Assignment Comma Separated Values Reader Page 1

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

CS 351 Design of Large Programs Threads and Concurrency

Array. Prepared By - Rifat Shahriyar

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Principles of Computer Science I

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

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

File I/O Array Basics For-each loop

Exercise Session Week 8

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Pace University. Fundamental Concepts of CS121 1

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

CS 251 Intermediate Programming Java I/O Streams

Data Types and the while Statement

5/23/2015. Core Java Syllabus. VikRam ShaRma

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Writing your own Exceptions. How to extend Exception

Programming II (CS300)

1007 Imperative Programming Part II

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

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

Exercises and Labs. Part I. Exercises

Exercise Session Week 8

Lab 2: File Input and Output

Exceptions. Examples of code which shows the syntax and all that

1 Shyam sir JAVA Notes

COMP-202. Exceptions. COMP Exceptions, 2011 Jörg Kienzle and others

ArrayList. Introduction. java.util.arraylist

HST 952. Computing for Biomedical Scientists Lecture 8

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Declarations and Access Control SCJP tips

Checked and Unchecked Exceptions in Java

CmpSci 187: Programming with Data Structures Spring 2015

Exceptions and Error Handling

Programming II (CS300)

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Full file at

Sri Vidya College of Engineering & Technology Question Bank

ITI Introduction to Computing II

1B1b Classes in Java Part I

Exceptions in Java

1.00 Lecture 30. Sending information to a Java program

17. Handling Runtime Problems

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CSE 331 Summer 2016 Final Exam. Please wait to turn the page until everyone is told to begin.

ITI Introduction to Computing II

ICOM 4015-Advanced Programming. Spring Instructor: Dr. Amir H. Chinaei. TAs: Hector Franqui, Jose Garcia, and Antonio Tapia. Reference: Big Java

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

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

CS 231 Data Structures and Algorithms, Fall 2016

Active Learning: Streams

Java Exceptions Java, winter semester

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

Exceptions and Design

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Streams and File I/O

Full file at

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

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

CSCI 200 Lab 3 Using and implementing sets

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

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

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

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

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

More on Objects in JAVA TM

Transcription:

Answer Key 1. General Understanding (10 points) Answer the following questions with yes or no. think before you decide. Read the questions carefully and (a) (2 points) Does the interface java.util.sortedset implement java.util.collection? (b) (2 points) Does the initializer Object o = new Integer(5); compile? (Assume o was not declared before?) (c) (2 points) Is branch coverage testing a white box technique? (d) (2 points) Does the expression (new Integer(11) == new Integer(11)) evaluate to true? (e) (2 points) Is String a reference type? Answer: (The explenations given here were not required.) (a) (2 points) no. (An interface never implements anything. It can only extend another interface.) (b) (2 points) yes. (The type Integer is compatible with the type Object.) (c) (2 points) yes. (To know the different branches of program execution, we have to know the implementation.) (d) (2 points) no. (Each new operator creates a different object and returns a reference to it. Therefore, == here compares two different references.) (e) (2 points) yes. (Strings are objects.) 2. Interfaces (10 points) 1

(a) (2 points) Please remind yourself of the Iterator interface, which is also documented in the enclosed API descriptions. Implement a small helper class IteratorPrinter, only providing a static method printelements(iterator it), which prints all elements you can get out of the iterator it to System.out (one element per line). (b) (6 points) The Iterator interface is meant to be used in combination with Collection types. Nevertheless, the task here is to write an implementation of this interface not for collections, but for arrays, more precisely for arrays the element type of which is a reference type (not a primitive type). Write a class ObjectArrayIterator implementing the Iterator interface ( implements Iterator ). Each ObjectArrayIterator object is bound to an underlying array. Using the next() method of an ObjectArrayIterator object, one can iterate over the elements of the underlying array. You have to carefully follow the contracts described in the MethodDetail section of the enclosed API description. This includes implementing what is required by the respective Throws: sections in the API description. The class should not support the remove() method. That means: you still have to implement remove(), but only in a certain way. (See the according Throws: section.) Beside the Iterator methods, you must implement a constructor public ObjectArrayIterator(Object[] a) which creates an ObjectArrayIterator for the array. (Hint: Store the array a in a field, and provide an additional nextelement field. In this exam, don t worry about independent iterations.) (c) (2 points) Now the ObjectArrayIterator class is to be used, in a very small (and silly) example. Implement a small class PrintArgs, the main method of which prints out all command line arguments, but only by calling the printelements method of the class IteratorPrinter. (So don t use a for or while in this class.) Hints: All arrays of type Type[], where Type is a reference type, are compatible with the array type Object[]. For example, you can pass an array of type String[] to a method with argument type Object[]. To throw an exception of type SomeException, use the following statement: throw new SomeException(); Both, the NoSuchElementException and the UnsupportedOperationException, are unchecked exception types, as they extend RuntimeException. Therefore, exceptions of both types neither have to be caught (using try catch) nor have to be declared in a method s head. Answer: (a) (2 points) 2

import java. util. ; public class IteratorPrinter { public static void printelements ( Iterator i ) { while ( i. hasnext ( ) ) System. out. println ( i. next ( ) ) ; (b) (6 points) import java. util. ; public class ObjectArrayIterator implements Iterator { (c) (2 points) private Object [ ] a ; private int nextelement ; public ObjectArrayIterator ( Object [ ] a ) { this. a = a ; nextelement = 0 ; public boolean hasnext ( ) { return nextelement < a. length ; public Object next ( ) { if (! hasnext ( ) ) throw new NoSuchElementException ( ) ; nextelement = nextelement + 1 ; return a [ nextelement 1 ] ; public void remove ( ) { throw new UnsupportedOperationException ( ) ; import java. util. ; public class PrintArgs { public static void main ( String [ ] args ) { Iterator it = new ObjectArrayIterator ( args ) ; IteratorPrinter. printelements ( it ) ; 3

3. Input/Output (10 points) Write a program LineAndColumn.java which seeks for the first occurrence of a particular character in a text file and prints out the line number and position within the line of that occurrence. The program in it s normal use takes two command line arguments, the first being the name of the text file, the second being the character to be searched. The program should complain if it gets to the wrong number of arguments, or if the second argument is a String longer than one character. Also, it should tell when there appears an input/output problem. (The program is assumed to be executed in the directory where the input file resides.) For example, consider a text file test.txt, containing just the following two lines: jw3xxv44js snnnms8@@lvdu Moreover, assume there is no file xxx.txt in the current directory. Here are example outputs: java LineAndColumn test.txt j prints out: the character j appears first in line 1 at position 0 java LineAndColumn test.txt m prints out: the character m appears first in line 2 at position 4 java LineAndColumn test.txt a prints out: the character a does not appear in the file java LineAndColumn test.txt prints out: wrong number of arguments java LineAndColumn test.txt bb prints out: second argument must be a single character java LineAndColumn xxx.txt j prints out: there appeared an input/output problem (It doesn t matter if your program considers the first line being line number 1 or line number 0. The same holds for the positions within the lines.) Hints: One can exit a void method at any point in the method body by using the statement return;. But use this only if you think it simplifies your code. Consider using a LineNumberReader. Consider the methods charat(i) and indexof(c) of the String class. (See Jia.) Answer: 4

import java. io. ; public class LineAndColumn { public static void main ( String [ ] args ) { if ( args. length! = 2 ) { System. out. println ( wrong number of arguments ) ; return ; if ( args [ 1 ]. length ( )! = 1 ) { System. out. println ( second argument must be + a single character ) ; return ; String file = args [ 0 ] ; char tofind = args [ 1 ]. charat ( 0 ) ; String line ; int indexinline ; boolean found = false ; try { LineNumberReader input = new LineNumberReader ( new FileReader ( file ) ) ; while ( ( ( line = input. readline ( ) )! = null ) &&! found ) { indexinline = line. indexof ( tofind ) ; if ( indexinline!= 1) { found = true ; System. out. println ( the character + tofind + appears first in line + input. getlinenumber () + at position + indexinline ) ; if (! found ) System. out. println ( the character + tofind + does not appear in the file ) ; input. close ( ) ; catch ( IOException e ) { System. out. println ( there appeared + an input/output problem ) ; 5

4. I/O and Collections (15 points) We consider a scenario where simple price lists are available in a text format. As an example, here is the content of the file pricelist.txt: butter 20 bread 18 jam 25 cola 15 milk 9 salami 30 (For simplicity, all prices are full SEK, so Öre are not considered.) You can assume all price lists having such a form. Each line starts with a word, followed by an arbitrary number of white spaces ( ), followed by digits representing a number. White spaces appear only between the words and the numbers. No TABs are used. Another form of text files are orders, such as the following file order.txt butter jam bread milk beer You can assume all orders having such a form. Each line contains one word, and the file does neither contain white spaces nor TABs. Moreover, for simplicity we assume that no word appears twice in an order. Write a java program which takes such a price list and an order and prints an according receipt ( kvitto ) to System.out. On our example files, it behaves like this: calling: java Receipt pricelist.txt order.txt results in the following output: bread for 18 jam for 25 butter for 2 0 beer not availabe milk for 9 = = = = = = = = total : 7 2 (The order in which items are listed in the output is not significant. Your program is allowed to list them in an arbitrary order.) To simplify your task, the program does not have to be robust this time. This means, you can receive full points even if your program does not react properly to unexpected situations, like a wrong number of arguments, non-existing files, or files having a different format than explained above. Nevertheless, the program has to compile. Therefore, you have to do as much exception handling as the compiler requires. 6

The program is not allowed to read through the input files again and again, whenever the price of an item has to be looked up. Instead, store items and prices in a suitable data structure. Hints: Consider using BufferedReader and StringTokenizer. The method readline() of the class BufferedReader returns a String, either containing the contents of the line, or null if the end of the stream has been reached. To iterate over a Collection (for example a Set), you can create an iterator by calling the iterator() method. The get(k) method of Map returns the value to which this map maps the specified key. It returns null if the Map contains no mapping for this key. The return type of get(k) is Object. The argument type of Integer.parseInt(str) is String. Answer: import java. io. ; import java. util. ; public class Receipt { public static void main ( String [ ] args ) throws IOException { String pricefile = args [ 0 ] ; String orderfile = args [ 1 ] ; BufferedReader pricereader = new BufferedReader ( new FileReader ( pricefile ) ) ; BufferedReader orderreader = new BufferedReader ( new FileReader ( orderfile ) ) ; // read the prices into the price map Map pricemap = new HashMap ( ) ; String line ; while ( ( line = pricereader. readline ( ) )! = null ) { StringTokenizer st = new StringTokenizer ( line ) ; pricemap. put ( st. nexttoken ( ), st. nexttoken ( ) ) ; pricereader. close ( ) ; // read the orders into the set of orders 7

Set orders = new HashSet ( ) ; while ( ( line = orderreader. readline ( ) )! = null ) orders. add ( line ) ; orderreader. close ( ) ; // iterate over the orders and sum up the prices int total = 0 ; Iterator it = orders. iterator ( ) ; while ( it. hasnext ( ) ) { Object item = it. next ( ) ; Object price = pricemap. get ( item ) ; if ( price == null ) System. out. println ( item + not available ) ; else { System. out. println ( item + for + price ) ; total = total + Integer. parseint ( ( String ) price ) ; System. out. println ( = = = = = = = = ) ; System. out. println ( total : + total ) ; 5. Threads (15 points) This assignment talks about print jobs, being produced by clients, collected in a print queue, and being printed by a print server. We represent the information to be printed by a (potentially very long) String. Consider the class PrintJob, wrapping stings which are going to be printed: class PrintJob { public String whattoprint ; public PrintJob followingjob ; public PrintJob ( String str ) { whattoprint = str ; followingjob = null ; As PrintJob objects have a reference to another PrintJob object, they can be chained up to a queue which is to be served by a print server. We now present an according class PrintQueue. This class allows to add strings, and to take them out again. It is 8

PrintQueue itself which does the wrapping and unwrapping of strings into (and from) print jobs. Therefore, it is appropriate to make PrintJob visible only in PrintQueue. Nobody else might use the class. This is achieved by moving the class PrintJob into the class PrintQueue, as a private static inner class. (private static inner classes act like normal classes, beside that they are visible only within code of their outer class.) Better ignore the critical point comments in the first reading. public class PrintQueue { private static class PrintJob { public String whattoprint ; public PrintJob followingjob ; public PrintJob ( String str ) { whattoprint = str ; followingjob = null ; private PrintJob firsttoprint = null ; private PrintJob lasttoprint = null ; private int length = 0 ; public void addtoqueue ( String str ) { PrintJob pjob = new PrintJob ( str ) ; if ( length == 0) firsttoprint = pjob ; else lasttoprint. followingjob = pjob ; lasttoprint = pjob ; //( critical point here?) length = length + 1 ; public String takefromqueue ( ) { while ( length == 0) { // waiting until no longer empty PrintJob forreturning = firsttoprint ; firsttoprint = firsttoprint. followingjob ; //( critical point here?) length = length 1; 9

if ( length == 0) lasttoprint = null ; return forreturning. whattoprint ; (a) (2 points) Describe briefly how addtoqueue and takefromqueue work. (b) (4 points) Now consider the PrintServer class: public class PrintServer extends Thread { private PrintQueue queue ; public PrintServer ( PrintQueue pq ) { queue = pq ; public void run ( ) { while ( true ) { String toprint = queue. takefromqueue ( ) ; // do the real printing... A PrintServer object stores a reference to its corresponding PrintQueue object. PrintServer extends Thread, because it is executed in parallel to clients. Implement a class Client, also extending Thread. Different clients run in parallel to each other and to the server. For simplicity, your Client should only add the same String again and again to the same PrintQueue. Both, the String and the PrintQueue, are given as arguments to the constructor. For not creating too many print jobs, execute Thread.sleep(100); after each adding. (sleep() is declared to possibly throw an InterruptedException, which is a checked exception! The same holds for wait(), see below.) (c) (3 points) Implement a tester class PrintTest, the main method of which creates a print queue, and starts three clients (with different strings to print) and one server. All clients and the server refer to the same print queue. (d) (3 points) With the given implementation, a PrintQueue can get corrupted, in at least two different ways. Explain how. What can be done to prevent this? Indicate clearly the necessary changes to the code. Hint: Look at the critical point comments now. (e) (3 points) The current implementation of takefromqueue potentially spends time in a exe- 10

cuting a waiting loop, if the queue is empty. This prevents other threads from executing immediately. In combination with synchronized, it can even lead to a deadlock. What should you do instead? Indicate clearly the necessary changes to the code. Answer: (a) (2 points) addtoqueue: takes a string as an argument it wraps the string into a PrintJob if there are no jobs in the print queue, this job becomes the front and end of the PrintQueue else this job is appended at the end of the PrintQueue the length is incremented takefromqueue: waits until jobs are in the queue remembers the first PrintJob, which is then discarded from the PrintQueue the length is decremented if the discarded job was the only one, then there is no last job left unwraps the string from the remembered job and returns it (b) (4 points) public class Client extends Thread { private PrintQueue queue ; private String toprintrepeatedly ; public Client ( PrintQueue pq, String str ) { queue = pq ; toprintrepeatedly = str ; public void run ( ) { while ( true ) { queue. addtoqueue ( toprintrepeatedly ) ; try { Thread. sleep ( 1 0 0 ) ; catch ( InterruptedException e ) { 11

(c) (3 points) public class PrintTest { public static void main ( String [ ] args ) { PrintQueue pqueue = new PrintQueue ( ) ; Client cl1 = new Client ( pqueue, Sent from client 1. ) ; Client cl2 = new Client ( pqueue, Sent from client 2. ) ; Client cl3 = new Client ( pqueue, Sent from client 3. ) ; PrintServer server = new PrintServer ( pqueue ) ; cl1. start ( ) ; cl2. start ( ) ; cl3. start ( ) ; server. start ( ) ; (d) (3 points) The addtoqueue could be interrupted right after setting lasttoprint to the new job, without having increased the length. The queue then might seem to be empty but is not, which might cause this job being overwritten by another job. The takefromqueue could be interrupted right after discarding the so far first job, without having decreased the length. The queue then might seem to contain one more job but actually is empty, causing a NullPointerException if another server uses the same queue to take out jobs. To prevent this, both methods should be synchronized. See the code in the answer to (e). (e) (3 points) public synchronized void addtoqueue ( String str ) { System. out. println ( adding : + str ) ; System. out. println ( ) ; PrintJob pjob = new PrintJob ( str ) ; if ( length == 0) firsttoprint = pjob ; else lasttoprint. followingjob = pjob ; lasttoprint = pjob ; length = length + 1 ; notifyall ( ) ; 12

public synchronized String takefromqueue ( ) { while ( length == 0) try { wait ( ) ; catch ( InterruptedException e ) { PrintJob forreturning = firsttoprint ;.... 13