Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Problem 1 Write a java class named my_WordReverse using recursion. It takes a sentence and returns the sentence in reverse order. Note: Space.

Similar presentations


Presentation on theme: "Recursive Problem 1 Write a java class named my_WordReverse using recursion. It takes a sentence and returns the sentence in reverse order. Note: Space."— Presentation transcript:

1

2 Recursive Problem 1 Write a java class named my_WordReverse using recursion. It takes a sentence and returns the sentence in reverse order. Note: Space is the only separator.

3 Something About Java Import the corresponding packages –import java.io.* –import java.util.* … If you use some IDE, be sure your program can run just with standard JDK. Check whether you use some other package: import com.borland.~~~

4 Something About Java Input Part BufferedReader keyboard = new BufferedReader(new InputStreamReader (System.in) ); –System ; final class –InputStreamReader; a bridge from byte streams to character streams –BufferedReader; for efficiency

5 Something About Java Get words from sentence –Use space as separator Use String.substring() String.indexOf() String.length() –Use StringTokenizer StringTokenizer st = new StringTokenizer("this is a test"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }

6 Something About Java Store Words into …… –Array –ArrayList –Vector

7 Recursive Algorithm Sample online –n recursive calls Swap the the last one and first one –n/2 recursive calls

8 Problem 2 Make Changes For a given amount of money, print out all possible solutions of making changes. Complete and No duplicate!

9 Something About Java Integer.parseInt(String s) Variable’s scope –http://java.sun.com/docs/books/tutorial/java/nut sandbolts/scope.htmlhttp://java.sun.com/docs/books/tutorial/java/nut sandbolts/scope.html

10 Recursive Algorithm 50c 25c 10c 10c?10c 5c

11 Recursive Algorithm Sample online Why complete and no duplicate? Another example Why it is wrong? Wrong use of “If – else” causes lost cases. For example, 20 cents No (1,0,10) and (1,1,5) Larger amount  More solutions lost!


Download ppt "Recursive Problem 1 Write a java class named my_WordReverse using recursion. It takes a sentence and returns the sentence in reverse order. Note: Space."

Similar presentations


Ads by Google