Understanding Recursive Classes

42
UNDERSTANDING RECURSIVE CLASSES CMSC 150: Lecture 22

description

Understanding Recursive Classes. CMSC 150: Lecture 22 . StringList : A Recursive Class. public class StringList { // instance variables private boolean isEmpty ; private String thisString ; private StringList restOfStringList ; // constructors - PowerPoint PPT Presentation

Transcript of Understanding Recursive Classes

Page 1: Understanding  Recursive Classes

UNDERSTANDING RECURSIVE CLASSES

CMSC 150: Lecture 22

Page 2: Understanding  Recursive Classes

StringList: A Recursive Classpublic class StringList { // instance variables private boolean isEmpty; private String thisString; private StringList restOfStringList;

// constructors public StringList() { … } public StringList(String newString, StringList aList) { … }

public String toString() { … } public String getLineStartingWith(String prefix) { … }}

Page 3: Understanding  Recursive Classes

StringList: A Recursive Classpublic class StringList { // instance variables private boolean isEmpty; private String thisString; private StringList restOfStringList;

// constructors public StringList() { … } public StringList(String newString, StringList aList) { … }

public String toString() { … } public String getLineStartingWith(String prefix) { … }}

Data

Page 4: Understanding  Recursive Classes

StringList: A Recursive Classpublic class StringList { // instance variables private boolean isEmpty; private String thisString; private StringList restOfStringList;

// constructors public StringList() { … } public StringList(String newString, StringList aList) { … }

public String toString() { … } public String getLineStartingWith(String prefix) { … }}

Methods

Page 5: Understanding  Recursive Classes

StringList: A Recursive Class

public class StringList { // instance variables private boolean isEmpty; private String thisString; private StringList restOfStringList;

// constructors public StringList() { … } public StringList(String newString, StringList aList) { … }

public String toString() { … } public String getLineStartingWith(String prefix) { … }}

Page 6: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList();

aList true

""

toString()

getLine()

public class StringList

{

// instance variables

private boolean isEmpty;

private String thisString;

private StringList restOfStringList;

// constructors

public StringList() { … }

public StringList(String newString, StringList aList) { … }

public String toString() { … }

public String getLineStartingWith(String prefix) { … }

}

addr: 32

32

0

Page 7: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList();

aList true

""

toString()

getLine()

Actually a reference to a String object, but for brevity…

addr: 32

32

0

public class StringList

{

// instance variables

private boolean isEmpty;

private String thisString;

private StringList restOfStringList;

// constructors

public StringList() { … }

public StringList(String newString, StringList aList) { … }

public String toString() { … }

public String getLineStartingWith(String prefix) { … }

}

Page 8: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList);

aList true

""

toString()

getLine()

addr: 32

32

0

Page 9: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList);

aList true

""

toString()

getLine()

toString()

getLine()

false

addr: 32

32addr: 48

0

Page 10: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

32

0

addr: 48

Page 11: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

32

0

addr: 48

Page 12: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

32

0

addr: 48

32

Page 13: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

32

0

addr: 48

32

Page 14: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

32

0

addr: 48

32

Page 15: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

32

0

addr: 48

32

Page 16: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

48

0

addr: 48

32

Page 17: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

48

0

addr: 48

32

Page 18: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList); aList = new StringList("Lilly", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

48

0

addr: 48

32

Page 19: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList); aList = new StringList("Lilly", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

48

0

addr: 48

32

toString()

getLine()

false

addr: 77

"Lilly"

48

Page 20: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList); aList = new StringList("Lilly", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

48

0

addr: 48

32

toString()

getLine()

false

addr: 77

"Lilly"

48

Page 21: Understanding  Recursive Classes

StringList: In Action StringList aList = new StringList(); aList = new StringList("mandolin", aList); aList = new StringList("Lilly", aList);

aList true

""

toString()

getLine()

toString()

getLine()

"mandolin"

false

addr: 32

77

0

addr: 48

32

toString()

getLine()

false

addr: 77

"Lilly"

48

Page 22: Understanding  Recursive Classes

HistoryList: A Recursive Class

public class HistoryList { // instance variables private boolean isEmpty; private String firstWebSite; private HistoryList restOfWebSites;

// constructors public HistoryList() { … } public HistoryList(String newSite, HistoryList aList) { … }

public boolean contains(String site) { … } public String toString() { … } public HistoryList getMatches(String prefix) { … } public boolean isEmpty() { … }}

Page 23: Understanding  Recursive Classes

HistoryList: In Action HistoryList aList = new HistoryList(); aList = new HistoryList("cnn.com", aList); aList = new HistoryList("mlb.com", aList);

aList true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

Page 24: Understanding  Recursive Classes

HistoryList: contains() method

public boolean contains(String site) { if (empty) { return false; } else if (firstWebSite.equals(site)) { return true; } return restOfWebSites.contains(site);}

Page 25: Understanding  Recursive Classes

HistoryList: contains() boolean inList = aList.contains("cnn.com");

aList true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

Page 26: Understanding  Recursive Classes

HistoryList: contains() boolean inList = aList.contains("cnn.com");

aList true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

Page 27: Understanding  Recursive Classes

HistoryList: contains() boolean inList = aList.contains("cnn.com");

aList true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

Page 28: Understanding  Recursive Classes

HistoryList: contains() boolean inList = aList.contains("cnn.com");

aList true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

Page 29: Understanding  Recursive Classes

HistoryList: contains() boolean inList = aList.contains("cnn.com");

aList true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

Page 30: Understanding  Recursive Classes

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

if (empty){ return false;} else if (firstWebSite.equals(site)) { return true;}return restOfWebSites.contains(site);

contains()toString()getMatches()isEmpty()

aList

Page 31: Understanding  Recursive Classes

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

if (empty){ return false;} else if (firstWebSite.equals(site)) { return true;}return restOfWebSites.contains(site);

Page 32: Understanding  Recursive Classes

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

if (empty){ return false;} else if (firstWebSite.equals(site)) { return true;}return restOfWebSites.contains(site);

Page 33: Understanding  Recursive Classes

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

if (empty){ return false;} else if (firstWebSite.equals(site)) { return true;}return restOfWebSites.contains(site);

Page 34: Understanding  Recursive Classes

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

if (empty){ return false;} else if (firstWebSite.equals(site)) { return true;}return restOfWebSites.contains(site);

Page 35: Understanding  Recursive Classes

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

if (empty){ return false;} else if (firstWebSite.equals(site)) { return true;}return restOfWebSites.contains(site);

Page 36: Understanding  Recursive Classes

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

if (empty){ return false;} else if (firstWebSite.equals(site)) { return true;}return restOfWebSites.contains(site);

Page 37: Understanding  Recursive Classes

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

if (empty){ return false;} else if (firstWebSite.equals(site)) { return true;}return restOfWebSites.contains(site);

Page 38: Understanding  Recursive Classes

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

if (empty){ return false;} else if (firstWebSite.equals(site)) { return true;}return restOfWebSites.contains(site);

true

Page 39: Understanding  Recursive Classes

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

if (empty){ return false;} else if (firstWebSite.equals(site)) { return true;}return restOfWebSites.contains(site);

true

true

Page 40: Understanding  Recursive Classes

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

if (empty){ return false;} else if (firstWebSite.equals(site)) { return true;}return restOfWebSites.contains(site);

true

true

true

Page 41: Understanding  Recursive Classes

HistoryList: contains() boolean inList = aList.contains("cnn.com");

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

true

true

true

Page 42: Understanding  Recursive Classes

HistoryList: contains() boolean inList = aList.contains("cnn.com");

true

""

contains()toString()getMatches()isEmpty()

"cnn.com"

false

addr: 42

87

0

addr: 58

42

false

addr: 87

"mlb.com"

58

contains()toString()getMatches()isEmpty()

contains()toString()getMatches()isEmpty()

aList

true

true

truetrue