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

Slides:



Advertisements
Similar presentations
CS 102 Computers In Context (Multimedia) 01 / 21 / 2009 Instructor: Michael Eckmann.
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 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 330 Programming Languages 10 / 14 / 2008 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 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 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 07 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 04 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 27 / 2008 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 330 Programming Languages 09 / 06 / 2007 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 11 / 08 / 2006 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 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 12 / 10 / 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 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 04 / 2008 Instructor: Michael Eckmann.
CS 102 Computers In Context (Multimedia)‏ 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 21 / 2006 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.
CS 330 Programming Languages 10 / 07 / 2008 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.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 22 / 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.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Presentation transcript:

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

Michael Eckmann - Skidmore College - CS Spring 2009 Today’s Topics Introduction of myself Review the syllabus List of Topics we'll cover Testing & Debugging Start Java Review (References,...)‏ discussion of what we'll do in lab tomorrow

Michael Eckmann - Skidmore College - CS Spring 2009 Reading homework Chapter 2 Reference Types Section 3.3 Javadoc First programming assignment to be assigned tomorrow.

Michael Eckmann - Skidmore College - CS Spring 2009 Who is your instructor? I'm Mike Eckmann and this is my fifth year at Skidmore. Before that I was at Lehigh University in PA. I studied Mathematics and Computer Engineering and Computer Science all at Lehigh University. I was employed as a programmer (systems analyst) for eight years.

Michael Eckmann - Skidmore College - CS Spring 2009 Syllabus Office hours Text book Class room Assignments –Hw's & programs Collaboration policy Grading schema Attendance Readings The most up-to-date syllabus will be found on the course web page.

Michael Eckmann - Skidmore College - CS Spring 2009 Suggestions on how to succeed in this (or any? CS) course Keep up on the reading before the class period that we cover it. Participate in class -- ask questions in class (best), via or in my office. Start the programs as soon as they are assigned. You will run into snags. Debugging can take a long, long time. These are based on my observations of successful students in past classes.

Michael Eckmann - Skidmore College - CS Spring 2009 Testing & Debugging This semester our programs can be larger and more complex than in cs106. Therefore it's a good idea to first design your code and then code it and then think about how to test your programs for accuracy. Determining good test data to input to our programs is the first meaningful step in determining if our code is correctly written.

Michael Eckmann - Skidmore College - CS Spring 2009 Testing & Debugging Any ideas on what makes good test data? How have you tested your programs in the past?

Michael Eckmann - Skidmore College - CS Spring 2009 Testing & Debugging Choose test data for which you know the correct output. Pick test cases which are likely to cause errors. What kinds of things do you think often cause errors?

Michael Eckmann - Skidmore College - CS Spring 2009 Testing & Debugging Choose test data for which you know the correct output. Pick test cases which are likely to cause errors. What kinds of things do you think often cause errors? –boundary values e.g. testing code that requires a month number (1 through 12) so we would try what numbers?

Michael Eckmann - Skidmore College - CS Spring 2009 Testing & Debugging Another necessary requirement of testing your code is to “fully exercise the code.” –what do you think that means?

Michael Eckmann - Skidmore College - CS Spring 2009 Testing & Debugging If you find out that one or more of your tests yield incorrect results, then what?

Michael Eckmann - Skidmore College - CS Spring 2009 Testing & Debugging If you find out that one or more of your tests yield incorrect results, then what? –Debug Maybe first read the code by eye and find out what is wrong. If that doesn't help, then could use a debugger that will allow you to run your program line by line or stop at certain places in your code and display values of variables, etc.

Michael Eckmann - Skidmore College - CS Spring 2009 Testing & Debugging Once you find the bug, fix it, then you should run all the tests again to make sure your fix didn't cause problems for other input that did work before.

Michael Eckmann - Skidmore College - CS Spring 2009 Prior experiences How many of you have had CS106 here? How many of you have programmed in Java before? How many of you have programmed in Python before?

Michael Eckmann - Skidmore College - CS Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 2009 Java “review” Exception handling, try/catch blocks File I/O StringTokenizer

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

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

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

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

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