19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

764 Chapter 20 Recursion<br />

(a) Correct path<br />

(b) Illegal path<br />

FIGURE 20.13<br />

The program finds a path from the upper-left corner <strong>to</strong> the bot<strong>to</strong>m-right corner.<br />

**20.27 (Koch snowflake fractal) The text presented the Sierpinski triangle fractal. In<br />

this exercise, you will write an applet <strong>to</strong> display another fractal, called the Koch<br />

snowflake, named after a famous Swedish mathematician. A Koch snowflake is<br />

created as follows:<br />

1. Begin with an equilateral triangle, which is considered <strong>to</strong> be the Koch fractal<br />

of order (or level) 0, as shown in Figure 20.14a.<br />

2. Divide each line in the shape in<strong>to</strong> three equal line segments and draw an outward<br />

equilateral triangle with the middle line segment as the base <strong>to</strong> create a<br />

Koch fractal of order 1, as shown in Figure 20.14b.<br />

3. Repeat Step 2 <strong>to</strong> create a Koch fractal of order 2, 3, . . . , and so on, as shown<br />

in Figure 20.14c–d.<br />

(a) (b) (c) (d)<br />

FIGURE 20.14<br />

A Koch snowflake is a fractal starting with a triangle.<br />

VideoNote<br />

Search a string in a direc<strong>to</strong>ry<br />

**20.28 (Nonrecursive direc<strong>to</strong>ry size) Rewrite Listing 20.7, Direc<strong>to</strong>rySize.java, without<br />

using recursion.<br />

*20.29 (Number of files in a direc<strong>to</strong>ry) Write a program that prompts the user <strong>to</strong> enter<br />

a direc<strong>to</strong>ry and displays the number of the files in the direc<strong>to</strong>ry.<br />

**20.30 (Find words) Write a program that finds all occurrences of a word in all the files<br />

under a direc<strong>to</strong>ry, recursively. Pass the parameters from the <strong>com</strong>mand line as<br />

follows:<br />

java Exercise20_30 dirName word<br />

**20.31 (Replace words) Write a program that replaces all occurrences of a word with a<br />

new word in all the files under a direc<strong>to</strong>ry, recursively. Pass the parameters<br />

from the <strong>com</strong>mand line as follows:<br />

java Exercise20_31 dirName oldWord newWord

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!