understanding recursive classes

42
UNDERSTANDING RECURSIVE CLASSES CMSC 150

Upload: miroslav-solovyov

Post on 30-Dec-2015

27 views

Category:

Documents


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Understanding  Recursive Classes

UNDERSTANDING RECURSIVE CLASSES

CMSC 150

Page 2: Understanding  Recursive Classes

2

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

3

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

4

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

5

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

6

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

7

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

8

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

9

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

10

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

11

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

12

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

13

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

14

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

15

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

16

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

17

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

18

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

19

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

20

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

21

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

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

23

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

24

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

25

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

26

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

27

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

28

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

29

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

30

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

31

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

32

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

33

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

34

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

35

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

36

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

37

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

38

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

39

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

40

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

41

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

42

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