understanding recursive classes

Post on 22-Feb-2016

23 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

UNDERSTANDING RECURSIVE CLASSES

CMSC 150: Lecture 22

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) { … }}

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

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

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) { … }}

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

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) { … }

}

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

aList true

""

toString()

getLine()

addr: 32

32

0

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

aList true

""

toString()

getLine()

toString()

getLine()

false

addr: 32

32addr: 48

0

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

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

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

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

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

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

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

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

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

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

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

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

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() { … }}

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()

HistoryList: contains() method

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

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()

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()

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()

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()

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()

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

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

""

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

""

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

""

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

""

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

""

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

""

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

""

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

""

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

""

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

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

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

top related