Name: Checked: Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of Lab.

Similar documents
Name: Checked: Preparation: Investment Calculator with input and output to text files Submit through Blackboard by 8:00am the morning of Lab.

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

File I/O and Exceptions

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

CS112 Lecture: Exceptions. Objectives: 1. Introduce the concepts of program robustness and reliability 2. Introduce exceptions

CS159. Nathan Sprague

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

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

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

File I/O Array Basics For-each loop

COMP-202 Unit 9: Exceptions

Introduction to Computer Science II CS S-22 Exceptions

Administrivia. CPSC Winter 2008 Term 1. Department of Computer Science Undergraduate Events

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

Simple Java Input/Output

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

I/O Streams. COMP 202 File Access. Standard I/O. I/O Stream Categories

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

CS112 Lecture: Exceptions and Assertions

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

Input-Output and Exception Handling

BBM 102 Introduction to Programming II Spring Exceptions

When we reach the line "z = x / y" the program crashes with the message:

COMP-202 Unit 9: Exceptions

Exceptions. CSC207 Winter 2017

Programming II (CS300)

Exceptions - Example. Exceptions - Example

BBM 102 Introduction to Programming II Spring 2017

Unit 10: exception handling and file I/O

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

Defensive Programming

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 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.

Exceptions Programming 1 C# Programming. Rob Miles

Exceptions. Exceptions. Exceptional Circumstances 11/25/2013

Happy Cinco de Mayo!!!!

Exceptions and Libraries

10/8/2018 Programming Data Structures. class diagram for assignment 08 review: polymorphism review: exception new: File I/O

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Introduction Unit 4: Input, output and exceptions

Computer Science is...

Happy Cinco de Mayo!!!!

Excep&ons and file I/O

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

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

Programming II (CS300)

Exceptions and Design

ECE 122. Engineering Problem Solving with Java

CS159. Nathan Sprague

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

File class in Java. Scanner reminder. File methods 10/28/14. File Input and Output (Savitch, Chapter 10)

Starting Out with Java: From Control Structures Through Objects Sixth Edition

For more details on SUN Certifications, visit

Chapter 4: Loops and Files

Recitation: Loop Jul 7, 2008

Fall 2017 CISC124 10/1/2017

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Chapter 4: Loops and Files

CSE 143 Java. Exceptions 1/25/

CSCI 200 Lab 2 Inheritance, Polymorphism & Data Streams

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

Exception Handling. Exception Handling

CSCI 261 Computer Science II

Errors and Exceptions

More on Exception Handling

16-Dec-10. Consider the following method:

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

Programming Languages and Techniques (CIS120)

Full file at Chapter 2 - Inheritance and Exception Handling

ITI Introduction to Computing II

Check out FilesAndExceptions from SVN. Exam 2 Review File I/O, Exceptions Vector Graphics Project

ITI Introduction to Computing II

Fundamentals of Object Oriented Programming

Internal Classes and Exceptions

CS 170 Java Programming 1. Week 10: Loops and Arrays

HST 952. Computing for Biomedical Scientists Lecture 8

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Intro to Computer Science II. Exceptions

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

CS S-22 Exceptions 1. Running a web server, don t want one piece of bad data to bring the whole thing down

Java Input/Output. 11 April 2013 OSU CSE 1

An exception is simply an error. Instead of saying an error occurred, we say that an.

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

Fundamentos de programação

Advanced Java Concept Unit 1. Mostly Review

Lecture 19 Programming Exceptions CSE11 Fall 2013

COSC Exception Handling. Yves Lespérance. Lecture Notes Week 10 Exception Handling

Lab 2: File Input and Output

Object Oriented Programming. Week 7 Part 1 Exceptions

Input/Output (I/0) What is File I/O? Writing to a File. Writing to Standard Output. CS111 Computer Programming

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

Reminder. Topics CSE What Are Exceptions?! Lecture 11 Exception Handling

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.

Correctness and Robustness

Data Structures. 03 Streams & File I/O

Transcription:

Lab 14 Name: Checked: Objectives: Practice handling exceptions and writing text files. Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of Lab. 1. Review your code for Lab 11, DeckOfCards.java. Save a copy in your Lab 14 folder. 2. Modify the code so that it creates an output text file containing the cards of the deck, after they are shuffled. Reminders: Prompt the user to enter a file name. Create a PrintWriter object named outfile, using the name that the person entered as the filename. Modify the code so that instead of using System.out.println( ) it uses outfile.println( ) to output to the file. Don t forget to close() the file at the end (after all output to the file is done) Don t forget to import java.io.*; Remember to add throws IOException in the main() method heading. 3. Run the code three times, obtaining three text files. 4. Submit the three output files along with your code.

Part A: try/catch NumberFormat 1. Using the code for ResponseTimeExperiment, experiment with bad inputs until you cause NumberFormatException. Note the error message generated: 2. Is NumberFormatException a checked or unchecked exception? 3. With your partner, look up NumberFormatException in our lecture slides and the Java API. Note the classes in between: Exception NumberFormatException 4. Create a try block around the code that parses a response and uses it to compute the outcome. Use a catch clause for NumberFormatException that prints "FAIL: You were supposed to type in a number". 3. Add some more code in the catch clause so that in cases where the response was invalid, the output file will contain an asterisk ( * ) for that input. For example, the output file corresponding to a particular run might look as follows, indicating that for the 3 rd question the subject entered a non-numeric answer (as opposed to an incorrect answer). 1 2529 0 1875 * 1 2773

Part B: IOException 1. What if the file cannot be created? Can you figure out how to crash your program (ie, cause a kind of IOException to be thrown (most likely FileNotFoundException) This can happen if the name entered by the user cannot be used by your operating system to create a file with that name. Play around with the program using different inputs for your name (Hint: try various symbols and non-alpha characters), until you cause this exception and make a note of the input here: Bad file name that crashed the program: Message: 2. Now let s try/catch it: First, separate the PrintWriter declaration from the instantiation: PrintWriter outfile; // declaration outfile = new PrintWriter(... ); // instantiation Next, place the instantiation for the PrintWriter in a try block, and catch IOException (that will catch FileNotFoundException and others as well). In the catch, use the following code to handle the exception: outfile = new PrintWriter(System.out); print a warning on standard output, such as: WARNING: Unable to create file (*name of file*). Data output to System.out. To summarize: The experiment proceeds as before. If no IOException occurs, the file will be created; if that is impossible (i.e., an exception occurs), the program will recover from the exception: 1) issue a warning and 2) proceed as before, but the contents that would have normally gone to the file will instead be displayed on the screen. Part C: What if your subject is prone to obscenity? Sometimes programs need to respond to rude or offensive user behavior. Here we will handle this situation by throwing an exception, so that the program aborts when the user enters a forbidden word as their name. In the DeckOfCards program, we will check that the file name entered is not a banned word before creating a file with the name. Follow these steps: 1. Create a class BadWordException.java that extends Exception. Here is the code: //******************************************************************** // BadWordException.java Author: M A Papalaskari

// Represents an exceptional condition for using bad words. //******************************************************************** public class BadWordException extends Exception //----------------------------------------------------------------- // Sets up the exception object with a particular message. //----------------------------------------------------------------- BadWordException(String message) super(message); Save this code in your Lab 14 folder. Compile it. (You should not try to run it.) 2. Create a file excusemyfrench.txt of banned words. Try first with just a few words (such lists are available free on the web, you can download one later). 3. In the new version of DeckOfCards.java, in the heading of the main() method add: throws BadWordException, IOException 4. Now in DeckOfCards.java we can check that the name entered is not a banned word. We will do this after obtaining the name from the user, by running through the banned words file, comparing the name with each word in the file. If a match is found, we punish them by throwing an exception! (i.e., the program terminates). Here is some code to do this: Scanner badwords = new Scanner(new File("excusemyfrench.txt")); /**/ while (badwords.hasnext()) if (name.equals(badwords.next())) throw (new BadWordException("Watch your tongue")); 5. Note that we are also faced with the possibility of an IOException, this time for the input file excusemyfrench.txt (it may not exist or may not be accessible for some reason). Move the file temporarily to a different folder and run the program again to observe what happens. 6. Let s handle the problem of this missing file. Place the code that sets up the Scanner and checks for matches (code given in 4, above) inside a try block with a catch clause that issues a warning and omits the check for bad words: catch (IOException e) System.out.println("Warning: excusemyfrench.txt missing. "); System.out.println("Not checking input for obscenity"); In other words, if the file excusemyfrench.txt is missing, the program will function normally, except that it will not be checking for banned words.

Lab 14 Comments Name: Comments on this lab, please: What was the most valuable thing you learned in this lab? What did you like best about this lab? Was there any particular problem? Do you have any suggestions for improving this lab as an effective learning experience?