CSE 143 Lecture 14 (A) More Recursive Programming

Slides:



Advertisements
Similar presentations
CSE 303 Lecture 16 Multi-file (larger) programs
Advertisements

CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 11 Recursive Programming reading: slides created by Marty Stepp and Hélène Martin
Java File Structure.  File class which is defined by java.io does not operate on streams  deals directly with files and the file system  File class.
Building Java Programs Chapter 12 Lecture 12-2: recursive programming reading:
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
CSE 143 Lecture 13 Recursive Programming reading: slides created by Marty Stepp
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 14 Maps/Sets; Grammars reading: slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
Genome Revolution: COMPSCI 006G 15.1 Recursion and Recursive Structures l Definition in the dictionary: in mathematics an expression in which a value is.
Recursion Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Sequential files creation & writing
Lecture 19: Binary Trees reading: 17.1 – 17.3
Lecture 10: recursive programming reading:
CSE 143 Lecture 19 More Recursive Backtracking
Building Java Programs
slides created by Marty Stepp and Alyssa Harding
Building Java Programs
slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1
PC02 Term 2 Test Recursion and Sorting. PC02 Term 2 Test Recursion and Sorting.
Building Java Programs
CSE 341 Lecture 3 let expressions; pattern matching Ullman
Homework 8: Critters (cont.)
Lecture 10: recursive programming reading:
Interfaces.
CSE 143 Lecture 11 Recursive Programming reading:
CSE 143 Lecture 6 References and Linked Nodes reading: 16.1
CSE 143 Lecture 2 Collections and ArrayIntList
Building Java Programs
Building Java Programs
Input/output (I/O) import java.io.*;
CSE 143 Lecture 14 More Recursive Programming; Grammars
CSE 143 Lecture 11 Recursive Programming slides created by Marty Stepp
Building Java Programs
CSE 143 Lecture 4 Implementing ArrayIntList
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
Recursion Based on slides by Alyssa Harding
Input/output (I/O) import java.io.*;
Input/output (I/O) import java.io.*;
CSE 143 Lecture 18 More Recursive Backtracking
slides created by Alyssa Harding
CSE 143 Lecture 3 Implementing ArrayIntList reading:
Suggested self-checks: Section 7.11 #1-11
slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 13 Recursive Programming reading:
Building Java Programs
CSE 143 Lecture 18 More Recursive Backtracking
Input/output (I/O) import java.io.*;
Lecture 10: recursive programming reading:
CSE 143 Lecture 6 interfaces; eclipse; testing
CSE 143 Lecture 4 Implementing ArrayIntList reading:
CSE 143 Lecture 7 References and Linked Nodes reading: 16.1
slides created by Marty Stepp
slides created by Marty Stepp
CSE 143 Lecture 21 Advanced List Implementation
slides created by Alyssa Harding
Presentation transcript:

CSE 143 Lecture 14 (A) More Recursive Programming reading: 12.2 - 12.3, 12.5 slides created by Marty Stepp http://www.cs.washington.edu/143/

Exercise Write a method crawl accepts a File parameter and prints information about that file. If the File object represents a normal file, just print its name. If the File object represents a directory, print its name and information about every file/directory inside it, indented. cse143 handouts syllabus.doc lecture_schedule.xls homework 1-sortedintlist ArrayIntList.java SortedIntList.java index.html style.css recursive data: A directory can contain other directories.

File objects A File object (from the java.io package) represents a file or directory on the disk. Constructor/method Description File(String) creates File object representing file with given name canRead() returns whether file is able to be read delete() removes file from disk exists() whether this file exists on disk getName() returns file's name isDirectory() returns whether this object represents a directory length() returns number of bytes in file listFiles() returns a File[] representing files in this directory renameTo(File) changes name of file

Public/private pairs We cannot vary the indentation without an extra parameter: public static void crawl(File f, String indent) { Often the parameters we need for our recursion do not match those the client will want to pass. In these cases, we instead write a pair of methods: 1) a public, non-recursive one with the parameters the client wants 2) a private, recursive one with the parameters we really need

Exercise solution 2 // Prints information about this file, // and (if it is a directory) any files inside it. public static void crawl(File f) { crawl(f, ""); // call private recursive helper } // Recursive helper to implement crawl/indent behavior. private static void crawl(File f, String indent) { System.out.println(indent + f.getName()); if (f.isDirectory()) { // recursive case; print contained files/dirs for (File subFile : f.listFiles()) { crawl(subFile, indent + " ");