CS 100, Summer 2001 Tuesday, 7/24 Lecture 15 ----------------------------------------------------------------------- Announcements: + Project 1 due 7/26 + Exercise 10 due today! + Exercise 11 due 7/25 + Pick up graded Exercise 9 (Partners and Plans) + reading: Savitch 12.1, 12.2, 12.3, 12.5 (p. 812-823) ----------------------------------------------------------------------- Topics: + File I/O + Arrays: searching + Arrays: sorting ----------------------------------------------------------------------- ---------------------------------------------------------------------- + Comments on Exercise 9 - Many were vague: e.g. "I'll read the assignment and plan out my time." That's what you were supposed to be doing for this exercise. ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Miscellaneous: - Do not return private objects; make a copy instead (e.g., getLocation returns a copy of pos, not pos itself!) This is for protection; if you return [a reference to] the object itself, then the program calling your method now has access to private members and can change them at will. ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Project 1 - You do not have to preserve case in the output. This is now going to be a bonus point. ---------------------------------------------------------------------- ---------------------------------------------------------------------- + File I/O - Input files: use BufferedReader - Output files: use PrintWriter - Use try/catch for IOException. You should not need to throw any exceptions yourself. - EXAMPLE: TextFileInputDemo.java - EXAMPLE: TextFileOutputDemo.java ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Arrays: Searching - Sequential search - Check each item until the desired one is found, or the end of the array is reached - Binary search - If they're in sorted order, you can do a binary search. This is like "20 questions": you can ignore half of the array each time through. - Which one do you think is faster? - Now let's see those ideas written in code - EXAMPLE: Searching.java, SearchingDemo.java ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Arrays: Sorting (audience participation!) - Five volunteers: sort by height - Start with full (unsorted) line. Find shortest student and swap with first person in line. Then find next shortest, put that student second. This is selection sort. (We're "selecting" the next student each time). - Start with empty line, then put each student one by one in the place they belong. This is insertion sort. (We're "inserting" each student to their proper place.) - Which one do you think is faster? - Many, many other sorting methods! (Mergesort, quicksort, radix sort, shell sort...) - In Java: - EXAMPLE: SelectionSort.java - EXAMPLE: See Exercise 11 for one version of Insertion sort ---------------------------------------------------------------------- ---------------------------------------------------------------------- PICK UP handed out Exercise 11 hand in your Exercise 10!