Download - Understanding Recursive Classes
UNDERSTANDING RECURSIVE CLASSES
CMSC 150
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) { … }
}
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
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
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) { … }
}
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
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) { ...}
}
8
StringList: In Action
StringList aList = new StringList(); aList = new StringList("mandolin", aList);
aList true
""
toString()
getLine()
addr: 32
32
0
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
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
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
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
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
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
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
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
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
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
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
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
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
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() { ... }
}
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()
24
HistoryList: contains() method
public boolean contains(String site)
{
if (empty)
{
return false;
}
else if (firstWebSite.equals(site))
{
return true;
}
return restOfWebSites.contains(site);
}
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()
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()
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()
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()
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()
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
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);
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);
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);
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);
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);
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);
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);
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
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
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
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
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