Presentation is loading. Please wait.

Presentation is loading. Please wait.

Example Consider the following class specification for a class that stores a bunch of characters. /* class invariant *this bunch contains one or more char.

Similar presentations


Presentation on theme: "Example Consider the following class specification for a class that stores a bunch of characters. /* class invariant *this bunch contains one or more char."— Presentation transcript:

1 Example Consider the following class specification for a class that stores a bunch of characters. /* class invariant *this bunch contains one or more char *values stored in the order they are inserted */ /* post: this bunch consists of the char c */ public Bunch(char c) /* post: result == (number of chars in this) - loc */ public int countOfRemaining() /* pre: loc+1 <= number of chars in this * post: result == (loc+1)st char in this * and loc == loc@pre + 1 */ public char nextChar() /* post: loc == 0 */ public void startNextChar()... Bunch Class Specifications Bunch - int loc «constructor» + Bunch(char c) «query» + int countOfRemaining() + char nextChar() «update» + void startNextChar() + void insert(char c) The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

2 What happens when the following code executes? public void printEm( Bunch b) { b.startNextChar(); while (b.countOfRemaining() > 0) { System.out.print( b.nextChar() ); } Q: How would you rewrite this method, using a loop to print the bunch in reverse? A: public void printEmReversed( Bunch b) { ?????? } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

3 A recursive method is one that calls itself either directly or indirectly. Example /* post: all chars following loc have been output in reverse order */ public void printEmReversed(Bunch b) { char aChar; if ( b.countOfRemaining() > 0 ) { aChar = b.nextChar(); printEmReversed(b); System.out.print( aChar ); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

4 Example /* post: all chars following loc have been output in reverse order */ public void printEmReversed(Bunch b) { char aChar; if ( b.countOfRemaining() > 0 ) { aChar = b.nextChar(); printEmReversed(b); System.out.print( aChar ); } /* assert: someBunch is a bunch consisting of ‘P’, ‘I’, ‘G’ */ SomeBunch.startNextChar(); printEmReversed( someBunch ); The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

5 Each call results in a separate activation, and each activation has its own copy of local variables and parameter bindings. ACTIVATION 1 aChar == ? b public void printEmReversed(Bunch b) { char aChar; if ( b.countOfRemaining() > 0 ) { aChar = b.nextChar(); printEmReversed(b); System.out.print( aChar ); } someBunch : Bunch ‘P’ ‘I’ ‘G’ loc == 0 The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

6 How does the behavior of the following variation compare to the printEmReversed method from the preceding slides? /* post: all chars following loc have been output in reverse order */ public void printEmSomehow(Bunch b) { char aChar; if ( b.countOfRemaining() > 0 ) { aChar = b.nextChar(); System.out.print( aChar ); printEmSomehow(b); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

7 /* pre: n > 0 * post: result == the nth Fibonacci number */ public int nth_Fibonacci( int n) { } DEFINITION nth Fibonacci number Base The 1 st Fibonacci number is 0. The 2 nd Fibonacci number is 1. Recursive If A n-1 is the (n-1) st Fibonacci number and A n-2 is the (n-2) nd Fibonacci, then A n-1 + A n-2 is the nth Fibonacci. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

8 Before Fill (from location X )After Fill (in red) The task of coloring the white region from a point out to its surrounding colored boundary is sometimes called a fill algorithm. Consider a rectangular grid (rows and columns) of picture elements (pixels) where each pixel can be colored independently. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

9 Consider a rectangular grid (rows and columns) of picture elements (pixels) where each pixel can be colored independently. /* class invariant *rowCount >0 and colCount > 0 */ /* post: rowCount == rows and colCount == cols */ public PixelGrid(int rows, int cols) /* pre: 0 ≤ r ≤ rowCount-1 and 0 ≤ c ≤ colCount-1 * post: result == the pixel from row r and column c * is white in color */ public boolean isPixelWhite(int r, int c) /* pre: 0 ≤ r ≤ rowCount-1 and 0 ≤ c ≤ colCount-1 * post: The pixel in row r and column c is red */ public void setPixelRed(int r, int c)... PixelGrid Class Specifications PixelGrid - int rowCount - int colCount «constructor» + PixelGrid(int rows, int cols) «query» + boolean isPixelWhite(int r, int c)) «update» + void setPixelRed(int r, int c) columns rows 0 1 2 3 4 5 0 1 2 3 4 5 The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

10 Before Fill (from location X )After Fill (in red) /* pre: position (r, c) within grid is bordered by a closed figure of non-white cells * post: the old white region surrounding (r, c) with non-white border has been colored red */ public void fill(PixelGrid grid, int r, int c ) { if ( grid.isPixelWhite(r,c) ) { grid.setPixelRed(r, c); fill( grid, r-1, c ); fill( grid, r, c+1 ); fill( grid, r+1, c); fill( grid, r, c-1); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

11 public void fill(PixelGrid grid, int r, int c ) { if ( grid.isPixelWhite(r,c) ) { grid.setPixelRed(r, c); fill( grid, r-1, c ); fill( grid, r, c+1 ); fill( grid, r+1, c); fill( grid, r, c-1); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

12 Recursion can be subtle. Describe the behavior of each of the following. public void fill2(PixelGrid grid, int r, int c ) { if ( grid.isPixelWhite(r,c) ) { fill2( grid, r-1, c ); fill2( grid, r, c+1 ); fill2( grid, r+1, c); fill2( grid, r, c-1); grid.setPixelRed(r, c); } public void fill3(PixelGrid grid, int r, int c ) { if ( grid.isPixelWhite(r,c) ) { grid.setPixelRed(r, c); fill3( grid, r-1, c-1 ); fill3( grid, r-1, c ); fill3( grid, r-1, c+1 ); fill3( grid, r, c+1 ); fill3( grid, r, c-1 ); fill3( grid, r+1, c-1 ); fill3( grid, r+1, c ); fill3( grid, r+1, c+1 ); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

13 Anything that can be accomplished with a loop, can also be written recursively. while ( condition ) { loopBody; } General form of a while loop. Equivalent recursive method The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

14 Infinite recursion is analogous to an infinite loop, and possibly more likely. public void infWhile() { if ( condition ) { infWhile(); loopBody; } Every activation requires computer memory and computer time. If a convenient non-recursive algorithm exists, it is usually more efficient than a recursive option. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

15 /* pre: n >= 0 * post: result == n! */ public int factorial( int n ) { if ( n == 0) { return 1; } else { return n * factorial(n-1); } Recursive Method DEFINITION n factorial (written n!) Base 0! == 1 Recursive n! == n * (n-1)! for any integer n > 0 /* pre: n >= 0 * post: result == n! */ public int factorial2( int n ) { int result = 1; while ( n > 1 ) { result = result * n; n--; } A Better Solution Table-driven int[] factorialTable = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600 }; The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.


Download ppt "Example Consider the following class specification for a class that stores a bunch of characters. /* class invariant *this bunch contains one or more char."

Similar presentations


Ads by Google