CS141: Topics for Week 9: Mar 12/14/16, 2018
Computer Science Headlines: ACM Tech News
Wed, March 14th, 2018: Enough! National School Walkout: Willamette University.
Interested students/faculty will exit their classrooms/residence halls at 9:45am and meet at Jackson Plaza. At 9:50am, we will hear from two speakers (Deb Patterson, a candidate in Salem's upcoming State Senate election, and one other speaker who is yet to be announced), then at 10:00am, we will walk over to the steps of the Oregon State Capitol. For the remaining time before 10:17am, we will peacefully protest with a number of rallying chants, then the walkout will end.
Please return to CS-141 by 10:30am.
Mon & Wed: Continue from Last Week
Reading:
- Chp 11: Input/Output and Exception Handling
Lab and Other Activities
-
Continue: Lab 7: Arrays
-
Practice with 2D arrays. Given the following code:
Line 1: Card[][] cards;
Line 2: cards = new Card[3][];
Line 3: System.out.println("cards.length = " + cards.length);
Line 4: cards[0] = new Card[2];
Line 5: cards[1] = new Card[4];
Line 6: cards[2] = new Card[3];
Line 7: for (int i=0; i < cards.length;i++) {
Line 8: System.out.println("i="+i+": cards[i].length = " + cards[i].length);
Line 9: for (int j=0; j < cards[i].length; j++) {
Line 10: cards[i][j] = new Card(i*j);
Line 11: System.out.println(" j="+j+": cards[i][j] = " + cards[i][j]);
Line 12: }
Line 13: }
Do the following:
- Show the picture in memory after executing lines 1,2,4,5,6,10.
- What is the output printed (i.e. at lines 3, 8, and 10)
- What type of variables are cards, cards[i] and cards[i][j]?
- If the line
System.out.println(cards[0][0]);
were inserted between
Lines 6 and 7, what output would be generated and why?
- If the line
System.out.println(cards[0][0].toString());
were inserted between
Lines 6 and 7, what output would be generated and why?
-
ArrayLists:
- Declaring, adding/removing elements, look at method list, copying, wrappers for primitive types.
- Diamond <> syntax
- Comparison with Arrays:
- Main advantages: Can change size. Easy to remove or add items at any location in list.
- Main disadvantages: Take up more memory. Slower. Can't contain primitives directly (must rely on "wrappers").
-
Reading and Writing Files - Sample Netbeans projects:
- Download and unzip: Files (zip file).
- After you unzip, you should see 2 Netbeans project folders:
- AphorismsProject: Reads a list of aphorisms into array.
- ArrayFileExample: Shows how to read and write numbers to a file.
- Open Netbeans and then open both of these projects.
- Reading ascii text files using the Scanner and File classes
- Writing to ascii text files using the PrintWriter class
- Opening and closing
- Binary data (anything other than ascii, e.g. images) - can't be represented on a console
- Checking for more data using in.hasNextInt(), in.hasNextDouble(). These return a boolean value.
- Reading words (tokens) vs lines using next()
- Converting Strings to numbers, e.g. using Double.parseDouble, Integer.parseInt()
- Exception Handling; Dealing with errors; How to gracefully survive a crash.
- Throwing an exception
- Catching an exception; Catching multiple exception types
- Types of Exceptions - see Javadoc for the Exception class
- The finally clause - cleaning up no matter what happens, e.g. closing a file
File
Ascii (text) file vs binary file
Opening a file
Closing a file
White space
|
Exception
Throwing an Exception
Catching an Exception
try-catch
Delimiter
|