cs 206 introduction to computer science ii 09 / 11 / 2009 instructor: michael eckmann

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

Upload: donald-caldwell

Post on 13-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

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

CS 206Introduction to Computer Science II

09 / 11 / 2009

Instructor: Michael Eckmann

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

Michael Eckmann - Skidmore College - CS 206 - Fall 2009

Today’s Topics• More Java Review (ArrayList, FileIO, etc.)

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

Michael Eckmann - Skidmore College - CS 206 - 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.

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

Michael Eckmann - Skidmore College - CS 206 - 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.

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

Michael Eckmann - Skidmore College - CS 206 - Fall 2009

Java “review”• Exception handling, try/catch blocks (import java.util.*;)

• File I/O (import java.io.*;)

• StringTokenizer

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

Michael Eckmann - Skidmore College - CS 206 - 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

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

Michael Eckmann - Skidmore College - CS 206 - 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.

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

Michael Eckmann - Skidmore College - CS 206 - 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.”

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

Michael Eckmann - Skidmore College - CS 206 - Fall 2009

File I/O• For file Input I recommend using a

BufferedReader wrapped around a FileReader

• For file Output I recommend using aPrintWriter wrapped around a FileOutputStream

• Let's look at example code.

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

Michael Eckmann - Skidmore College - CS 206 - 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.

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

Michael Eckmann - Skidmore College - CS 206 - 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:• http://math.hws.edu/TMCM/java/xSortLab/