CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Advertisements

Lecture 15: I/O and Parsing
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 20 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 14 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 09 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 04 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 09 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 28 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 23 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Files and Streams CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
GENERICS AND FILE HANDLING Saumya Srivastava (977934) Divyangana Pandey (977790) Shubhi Saxena (978108) Arka Das (962969) AHD05/15-16 AJA 21.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning.
Introduction to Exceptions in Java CS201, SW Development Methods.
Streams and File I/O Chapter 14.
Input and Output in Java
Unit 6 Working with files. Unit 6 Working with files.
Input and Output in Java
Input and Output in Java
Presentation transcript:

CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2009 Today’s Topics More Java Review (ArrayList, FileIO, etc.)‏

Michael Eckmann - Skidmore College - CS Fall 2009 Rewrite TextBook w/ generics Recall that the compareTo method took in a reference to an Object as a parameter. We always passed a TextBook into it, and it had to be cast to a TextBook. If we accidentally passed a non-TextBook into it, the program would still compile but would cause a runtime error. With generics, we can write it so that it is designed to only have a TextBook passed into it. If we accidentally passed a non- TextBook into it, the error will now be caught at compile-time.

Michael Eckmann - Skidmore College - CS Fall 2009 Java review – ArrayList The ArrayList class in Java API is used to store object references in a dynamic (changing size) array type structure. You do not need to know how big your ArrayList will get, you can add references to it and it will change size. add(Object o) adds an object to the end of the list add(int idx, Object o) adds an object to the specific place (idx) in the list Etc. let's look at the Java API online for ArrayList and write some code using it and place TextBooks into it.

Michael Eckmann - Skidmore College - CS Fall 2009 Java “review” Exception handling, try/catch blocks (import java.util.*;)‏ File I/O (import java.io.*;)‏ StringTokenizer

Michael Eckmann - Skidmore College - CS Fall 2009 Exception handling Exception handling allows for a program to detect unwanted behaviour and then instead of crashing the program, “catch” the exception while the program is running and handle it by doing something to allow the program to keep running. Let's see an example with catching an exception from Integer.parseInt

Michael Eckmann - Skidmore College - CS Fall 2009 Exception handling Some methods “throw” exceptions that are required to be caught, while others like Integer.parseInt don't require catching the exception. When we write our own methods, we can throw exceptions back to the caller. For example, if we have a method that takes in one int parameter, if the value passed in is required to be >=0, then the first code we write inside the method could be to check if negative and if so, throw an exception.

Michael Eckmann - Skidmore College - CS Fall 2009 File I/O File Input and Output Reading, Writing, Appending There are many classes in Java to handle reading and writing to files. We're going to focus on a few that allow reading and writing to “text files” (human readable) as opposed to “binary files.” Also, we're only going to focus on files that are “sequential” as opposed to “random access.”

Michael Eckmann - Skidmore College - CS Fall 2009 File I/O For file Input I recommend using a BufferedReader wrapped around a FileReader For file Output I recommend using a PrintWriter wrapped around a FileOutputStream Let's look at example code.

Michael Eckmann - Skidmore College - CS Fall 2009 StringTokenizer StringTokenizer is a class that allows us to easily “divide” up a String into tokens which are separated by a delimiter character. Let's look at some example code.

Michael Eckmann - Skidmore College - CS Fall 2009 Programming Examples Let's write a simple insertion sort method to sort the Cards in the ArrayList. To remind ourselves of how insertion sort works, let's look at: