Title: CS 5
1CS 5
C SCIENCE 5!
NO CS 5 next week
Final 2 Labs up to you
C SKILLS 5?
- Looking back / Looking ahead
ACM results
Week 12 (this week)
Recursion
Week 14
Software engineering algorithms
Week 15
Is there science in CS ?
due Sunday, 11/21 at midnight
M/T sections
due Monday, 11/22 at midnight
W/Th sections
Recitation for HW12 -- Friday 800am
Reading weeks 12/13 Recursion
2Recursion factorial example
public static double factorial(int N) if (N lt
2) return 1.0 else return
N factorial(N-1)
Base Case
Recursive Step
3Natural scenes
mountain terrain
seascapes
the 2d algorithm...
4public double plyN(Board b, int N)
N Ply
double s new double7 for (int c0 clt7
c) if (b.allowsMove(c))
b.addMove(c,me()) if (b.isOver())
sc evaluate(b) else
Player foe new Player(you(),N-1,2)
double s2 foe.findScores(b)
sc 100 - getMax(s2)
b.removeMove(c) else sc
-1.0 return s
self-reference?
5Problem 2 / Final Project
A film database application 5 classes
class name
data member
data member type
??
6Hw 12 Problem 2 -- Two tasks
- Implement the main menu (in the CS5App class)
Please choose an action from the following
list 0 Display All Directors 1 Display
All Films 2 Display Films by Title 3
Display Films by Director 4 Display Films by
Year 5 Display Films by Rating (G, PG, ) 6
Display Films by Review (0 to 10) 7 Add a New
Film 8 Save film database to file 9 Load
film database from file 42 Quit
- Create skeleton versions of the necessary
classes...
7Problem 2 - skeleton code comments
Methods
Classes
Data Members
all private !
public static void main(String args) public
static void printMenu() public static void
saveDB(String file) public static void
readDB(String file) public static void readFilm()
CS5App
static FilmDB F static DirectorDB D
Film
public Film(String title, int year, String
rating, double review, Director dir) public
String getTitle() public int getYear() public
String getRating() public double getReview()
public void display() public void save()
String title int year String rating double
review Director dir
// comment here public String getRating()
return
8Methods
Classes
Data Members
all private !
Director
String fname String lname FilmDB filmDB
public Director(String fn, String ln, int
capacity) public String getFullName() public
String getFirstName() public String
getLastName() public FilmDB getFilmDB()
// comment here! public FilmDB getFilmDB()
return
DirectorDB
similar to FilmDB below see the assignment
description
FilmDB
public FilmDB(int capacity) public boolean
isFull() public int getCount() public void
addFilm(Film f) public boolean
checkForFilm(String fulltitle) public void
saveAllFilms() public void displayAllFilms()
public void displayFilmsByTitle(String
titlepiece) public void displayFilmsByYear(int
year) public void displayFilmsByRating(String
rating) public void displayFilmsByReview(double
minreview)
int count Film Films
9static
static stuff belongs to a class, not an object
USING STATIC METHODS
printMenu()
in the CS5App class
getMax(double s)
in the Player class
Math.sqrt(Math.PI Math.E)
If static stuff is in another class, use that
classs name!
H.pl(Im a static method)
STATIC DATA
static double PI, E
in the Math class
seen globally in all of the classs methods
static FilmDB F
in the CS5App class
NONSTATIC DATA
char checker
in the Player class
data that is different for every object of the
class type
String title
in the Film class
10Self-referential data ?
A film database application
class Director
class Director private String fname
private String lname private FilmDB filmDB
// method skeletons
FilmDB filmDB
String fname
FilmDB
filmDB
String lname
class Film
class Film private String title private
int year private Director dir // method
skeletons
String rating
String title
double review
Director
int year
Director dir
dir
class FilmDB
class FilmDB private int count private
Film films // method skeletons
int count
Film
films0
films1
Film films
films
11Recursion
is defining something in terms of itself
factorial
5! 5 4 3 2 1
N! N (N-1) (N-2) 3 2 1
12Recursion -- warning !
public static double factorial(int N) return
N factorial(N-1)
No base case -- the calls to factorial will never
stop!
Make sure you have a base case, then worry about
the recursive step...
13Recursion factorial example
public static double factorial(int N) if (N lt
2) return 1.0 else return
N factorial(N-1)
Base Case
Recursive Step
shorter?
14Recursion factorial example
public static double factorial(int N) if (N lt
2) return 1.0 else return N
factorial(N-1)
Base Case
Recursive Step
Slightly shorter version without curly braces...
Even shorter versions soon!
15Recursion -- how it works
factorial(5)
5 factorial(4)
4 factorial(3)
3 factorial(2)
2 factorial(1)
1.0
5 4 3 2 1.0 is 120.0
16Recursion -- why ?
Exploits self-similarity
Less work !
Produces short, elegant code
Skeptical ? use the ? operator
double factorial(int N)
thought
the ratio
code
17Recursion Mantra
Let recursion do the work for you.
I could do that in my sleep!
Says stunned CS 5 student
I nodded off at the keyboard and when I woke up,
my program was done!
Delighted CodeWarrior user plans on more Sunday
night naps!
18Recursion for concise coding
public static void main(String args)
double d exp(2.0,10)
public static double exp(double x, int n)
if (n lt 0) return 0.0 return
power(x,n)/fac(n) exp(x,n-1)
19Recursion with arrays
double A 4, 7, -10, 4, 1
array initialization
4
7
-10
4
1
0
1
2
3
4
sum(A, 0, 4) returns 6.0 sum(A, 3, 4)
returns 5.0
Base Case
Recursive Step
20Recursion with arrays
Recursively summing an array
double sum(double A, int L, int U)
21Recursion -- not just numbers
Self-similarity elsewhere...
Natural phenomena
Relationships
What is an ancestor ?
Names -- acronyms
GNU
how much stem?
22A String of recursive methods
How many As are in a particular string?
RJACKSON GATTACA
Base Case
Recursive Step
23substring
String s RECURSION s.substring(3, 5)
is the string UR s.substring(0, 3) is the
string is the string
ECURSION is the string
CURSE
1
3
4
6
8
0
2
5
7
Hint Use
24A String of recursive methods
returns the number of As in the String s
int countAs(String s)
25Quiz
Names
returns a string that is the same as s, except
without any As
String removeAs(String s)
returns true if s is in alphabetical order, false
otherwise (ties OK)
boolean isAlph(String s)
Extra whats a six-letter English word for which
isAlph returns true?
26Quiz
A
5.1
4.2
7.7
42
1.7
page 2
A0
A4
A1
A2
A3
write recursive code to return the minimum value
in the array A between indices L and U
(inclusive).
min(A,0,3) should return 4.2
double min(double A, int L, int U)
27Recursion can do anything!
returns a string that is the same as s, except
without any As
String removeAs(String s)
removeAs(GATTACA) returns GTTC
returns true if s is in alphabetical order, false
otherwise (ties OK)
boolean isAlph(String s)
isAlph(BART) returns false
isAlph(LOOPY) returns false
isAlph(BERT) returns true
28Problem 1
Write recursive methods for
double factorial(int N)
29Problem 1
Create a menu that allows a user to call each
method
Welcome to the recursive roster! Options (1)
factorial (2) power (3) harmonic series (4)
palindrome checker (5) string reverser (6)
min mover (7) list sorter (8) longest common
subsequence Extra (9) quit
each of these will be a recursive method in the
CS5App class
30Lab Today
Take advantage of us!
Extra credit a biological application
Given two strings, what is a longest common
subsequence?
public static String LCS(String s1, String s2)
s1 hamburger
s1 GATTTCAAGTGAC
s2 cheeseburger
s2 CTTAGACATAGGT
LCS(s1,s2) returns hburger
LCS(s1,s2) returns TTCAAGG
31Testing N Ply
X
O
4
6
0
1
2
3
5
With a 4-ply lookahead, X will think everything
looks equivalent in this case
With a 5-ply lookahead, X will know to move to
column 3
32Recursion Mantra
Let recursion do the work for you.
33Natural scenes
mountain terrain
seascapes
the 2d algorithm...
34Recursive Data
Can data contain other data of the same type?
class Wart int idNumber Wart neighbor
Wart wally
int
idNumber
Wart
neighbor
35wally.neighbor .neighbor
wally.neighbor .neighbor.neighbor
wally
wally.neighbor
int
int
int
int
idNumber
idNumber
idNumber
idNumber
Wart
Wart
Wart
Wart
neighbor
neighbor
neighbor
neighbor
wally.neighbor .neighbor
wally.neighbor .neighbor.neighbor
wally
wally.neighbor
int
int
int
int
idNumber
idNumber
idNumber
idNumber
Wart
Wart
Wart
Wart
neighbor
neighbor
neighbor
neighbor
36Recursive Data
Can data contain other data of the same type?
class Wart int idNumber Wart neighbor
Wart wally
int
idNumber
Wart
neighbor
wally.neighbor .neighbor
wally.neighbor .neighbor.neighbor
wally
wally.neighbor
int
int
int
int
idNumber
idNumber
idNumber
idNumber
Wart
Wart
Wart
Wart
neighbor
neighbor
neighbor
neighbor
37Ridiculously short code!
Using the ? operator...
double factorial(int n) return nlt2 ? 1
nfactorial(n-1)
double sum(double arr, int L, int H) return
LgtH ? 0.0 arrLsum(arr,L1,H)
38addMove
X
newX
O
Class Board
Object b
changes b by adding checker X into row 3
b.addMove(3,X)
b before
b after
39Self-referential data ?
A film database application
class FilmDB
int count
Film
films0
films1
Film films
films
class Film
String rating
String title
double review
Director
int year
Director dir
dir
class Director
FilmDB filmDB
String fname
FilmDB
filmDB
String lname
40(Non) deterministic recursion
Deterministic recursion
Koch snowflake
Recursion with randomness
41Recursion can do anything!
returns a string that is the same as s, except
without any As
String removeAs(String s)
returns true if s is in alphabetical order, false
otherwise
boolean isAlph(String s)
42s GATTACA
double sum(double arr, int L, int H) return
LgtH ? 0.0 arrLsum(arr,L1,H)
No base case -- the calls to factorial will never
stop!
Make sure you have a base case !
When writing recursive code
Check that the base case is correct !!
As with mathematical induction, base cases are
critical...
43Computer Science 5
- Looking back / Looking ahead
Week 12 (this week)
Recursion
Week 14
Software engineering algorithms
Week 15
Is there science in CS ?
due Sunday, 11/24 at midnight
M/T sections
due Monday, 11/25 at midnight
W/Th sections
Recitation for HW12 -- Friday 800am
Reading Week 12 re recursion
44Recursion -- not just numbers
Self-similarity elsewhere...
Natural phenomena
Relationships
What is an ancestor ?
Names -- acronyms
EMACS
EINE
ZWEI
all stem!
45Extra credit N-ply search
Board Evaluation
advantage black
advantage red
Depth of Search
1-ply search
2-ply search
3-ply search
N-ply search
chooseMove1Ply
chooseMove2Ply
chooseMove3Ply
evaluate
(N-1) - ply ...
2 - ply
1 - ply
int chooseMove(Board b, char ox, int ply)
46Problem 2 / Final Project
CS5App (for main)
class name
data member
data member type