Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Today’s Topics Questions/comments Continue Java Review (writing our own classes, etc.)‏ File I/O StringTokenizer

3 Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Java review – writing our own classes instance variables vs. static class variables public vs. private constructor(s)‏ set and get methods toString()‏ Let's write the code for the TextBook class and make it implement the Comparable interface. Don't make assumptions about how the TextBook class will be used --- maybe it'll be used in an interactive program, maybe it'll be used by a program that takes no user input or output.

4 Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Good design ideas General guidelines to follow Be safe and be general –When creating a class to represent some kind of data, make all instance variables private (safety)‏ provide set methods with error checking in them (safety)‏ private get methods for “read” access do not get user input or write output inside an instantiable class => this allows your class to be general and be used in many situations (generality)‏ Create a separate class that contains the main method which also has methods/code to get input from the appropriate place (file or user) and to output to the appropriate place (file or screen).

5 Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Good design ideas for our 1 st programming assignment Student data class – instance variables to contain a name and a grade –No call to get user input, no calls to get file input, no calls to write output (to a file or the screen)‏ List of student data class – instance variable to store a list (ArrayList) of Student data –No call to get user input, no calls to get file input, no calls to write output (to a file or the screen)‏ Main program class –Contains the main method –Contains calls to get input from a file, get user input and write output to the screen and to a file.

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

7 Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Java “review” Exception handling, try/catch blocks File I/O StringTokenizer

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

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

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

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

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


Download ppt "CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann."

Similar presentations


Ads by Google