understanding recursive classes cmsc 150: lecture 22

42
UNDERSTANDING RECURSIVE CLASSES CMSC 150: Lecture 22

Upload: aron-casey

Post on 21-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UNDERSTANDING RECURSIVE CLASSES CMSC 150: Lecture 22

UNDERSTANDING RECURSIVE CLASSES

CMSC 150: Lecture 22

Page 2: 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

public StringList() { … }

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

public String toString() { … }

public String getLineStartingWith(String prefix) { … }

}

Page 3: 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

public StringList() { … }

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

public String toString() { … }

public String getLineStartingWith(String prefix) { … }

}

Data

Page 4: 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

public StringList() { … }

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

public String toString() { … }

public String getLineStartingWith(String prefix) { … }

}

Methods

Page 5: 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

public StringList() { … }

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

public String toString() { … }

public String getLineStartingWith(String prefix) { … }

}

Page 6: UNDERSTANDING RECURSIVE CLASSES CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

StringList: In Action

StringList aList = new StringList(); aList = new StringList("mandolin", aList);

aList true

""

toString()

getLine()

toString()

getLine()

false

addr: 32

32

addr: 48

0

Page 10: UNDERSTANDING RECURSIVE CLASSES CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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 CMSC 150: Lecture 22

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