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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 20 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 14 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 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 09 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 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 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 04 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 29 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 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 206 Introduction to Computer Science II 12 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 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.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 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.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
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.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
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.
Written by: Dr. JJ Shepherd
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Presentation transcript:

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

Michael Eckmann - Skidmore College - CS Fall 2009 Today’s Topics What this course will cover More Java Review (References, FileIO, etc.)‏ Short lab assignment

Michael Eckmann - Skidmore College - CS Fall 2009 Course Topics This course has an emphasis on common data structures and algorithms that work on them. After a short review of Java we learn a bit about how to analyze algorithms for efficiency. We then cover data structures such as linked lists, stacks, queues, priority queues, heaps, graphs, trees (binary and other), search trees, and hash tables. We also cover algorithms that work on those data structures such as breadth first search, depth first search, Dijkstra's algorithm, various sorting algorithms and tree balancing algorithms.

Michael Eckmann - Skidmore College - CS Fall 2009 Homework Assignment If you're new to eclipse, I have posted startup instructions on how to run the IDE, create new programs and compile/run them within the IDE. The first programming assignment is now available on our assignments page.

Michael Eckmann - Skidmore College - CS Fall 2009 Java review - References A reference variable (aka reference) is a variable that stores the memory address at which an object lives. References are variables of class types and arrays. Differentiate references from variable of the eight primitive types. null is the value a reference has when it does not refer to an object.

Michael Eckmann - Skidmore College - CS Fall 2009 Java review - References Assignment operator = e.g. Card c1 = new Card(5,0); // Five of Diamonds Card c2 = new Card(6,1); // Six of Clubs Card c3 = new Card(6,1); // Six of Clubs c1 = c3; // assign reference c3 to c1 When a reference is assigned to another reference what happens?

Michael Eckmann - Skidmore College - CS Fall 2009 Java review - References Assignment operator = e.g. Card c1 = new Card(5,0); // Five of Diamonds Card c2 = new Card(6,1); // Six of Clubs Card c3 = new Card(6,1); // Six of Clubs c1 = c3; // assign reference c3 to c1 When a reference is assigned to another reference what happens? Both references refer to the SAME object. That is, the memory address is copied. c1 and c3 are aliases for the same object.

Michael Eckmann - Skidmore College - CS Fall 2009 Java review - References Equality comparison operator == e.g. Card c1 = new Card(5,0); // Five of Diamonds Card c2 = new Card(6,1); // Six of Clubs Card c3 = new Card(6,1); // Six of Clubs if (c2 == c3) System.out.println(“c2 and c3 are the same card”); When reference variables are compared for equality with ==, what happens?

Michael Eckmann - Skidmore College - CS Fall 2009 Java review - References Equality comparison operator == e.g. Card c1 = new Card(5,0); // Five of Diamonds Card c2 = new Card(6,1); // Six of Clubs Card c3 = new Card(6,1); // Six of Clubs if (c2 == c3) // if c2 and c3 are aliases System.out.println(“c2 and c3 are the same card”); When reference variables are compared for equality with ==, what happens? If both references refer to the SAME object, then it evaluates to true. That is, if the memory addresses are the same, (if c2 and c3 are aliases for the same object) then true is evaluated. So will the above if condition be true?

Michael Eckmann - Skidmore College - CS Fall 2009 Java review - References Not equal comparison operator != e.g. Card c1 = new Card(5,0); // Five of Diamonds Card c2 = new Card(6,1); // Six of Clubs Card c3 = new Card(6,1); // Six of Clubs if (c2 != c3) // if c2 and c3 are not aliases System.out.println(“c2 and c3 are not the same”); When reference variables are compared with !=, what happens? If both references refer to the SAME object, then it evaluates to false. That is, if the memory addresses are the same, (if c2 and c3 are aliases for the same object) then false is evaluated. So will the above if condition be false?

Michael Eckmann - Skidmore College - CS Fall 2009 Java review - References What if we do the following: e.g. Card c1 = new Card(5,0); // Five of Diamonds Card c2; System.out.println(“The card is “ + c1.toString()); System.out.println(“Another card is “ + c2.toString());

Michael Eckmann - Skidmore College - CS Fall 2009 Java review - References What if we do the following: e.g. Card c1 = new Card(5,0); // Five of Diamonds Card c2; System.out.println(“The card is “ + c1.toString()); System.out.println(“Another card is “ + c2.toString()); Problem with c2.toString() because c2 DOES NOT refer to an object. Its value is null.

Michael Eckmann - Skidmore College - CS Fall 2009 Java review - References Card c1 = new Card(5,0); // Five of Diamonds Card c2; System.out.println(“The card is “ + c1.toString()); System.out.println(“Another card is “ + c2.toString()); Either c2 needs to be assigned a non-null value from another reference c2 = c1; Or it should be assigned to a newly instantiated object. c2 = new Card(10, 3); // Ten of Spades

Michael Eckmann - Skidmore College - CS Fall 2009 Java review - comparing Comparing variables/data of primitive types vs. comparing references of class types. ==,, =, != vs..equals( ) and.compareTo( )‏ What do these methods return? How do we use them?

Michael Eckmann - Skidmore College - CS Fall 2009 Java review – method calls some methods return nothing (are of type void)‏ and some methods return something (of some particular type). for example: System.out.println("Hi"); doesn't return anything, it just prints the text on the screen int i; i = Integer.parseInt("12"); does return something, an integer. Therefore it makes sense to "capture" what is being returned in a variable of type int.

Michael Eckmann - Skidmore College - CS Fall 2009 Java review – method calls It wouldn't make any sense to call that method on its own line like so: Integer.parseInt("12"); why? b/c all it does is convert that "12" to 12 and return it but nothing is "capturing" the return value (i.e. you are not using it in any way or assigning it to anything.)‏

Michael Eckmann - Skidmore College - CS Fall 2009 Java review – writing our own classes instance variables vs. static class variables public vs. private constructor(s)‏ set and get methods toString()‏

Michael Eckmann - Skidmore College - CS Fall 2009 Writing our own classes Create a class named TextBook with data: – Title – Author (last name, first name)‏ – Edition (should be numeric)‏ Make the class implement the Comparable interface - implement compareTo method with correct signature Implement equals method with the same signature as in Object Provide set and get methods for each instance variable. - there should be no way for someone to set the edition to be < 1. Two textbooks should be equal if all instance variables are the same.

Michael Eckmann - Skidmore College - CS Fall 2009 Writing our own classes The ordering of the textbooks is handled in compareTo --- that is, compareTo holds the logic about what makes one textbook less than another or what makes one greater than another and what makes textbooks equal. The rules you are to follow are: - Order the textbooks by author. - If same author, then order by title. - If same author and title, then order by edition. We will NOT 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.

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.