420 likes | 490 Views
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 ( ) { … }
E N D
Understanding Recursive Classes CMSC 150
StringList: A Recursive Class public class StringList { // instance variables private booleanisEmpty; private String thisString; private StringListrestOfStringList; // constructors public StringList() { … } public StringList(StringnewString, StringListaList) { … } public String toString() { … } public String getLineStartingWith(String prefix) { … } }
StringList: A Recursive Class public class StringList { // instance variables private booleanisEmpty; private String thisString; private StringListrestOfStringList; // constructors public StringList() { ... } • public StringList(String newString, StringListaList) {...} • public String toString() {...} • public String getLineStartingWith(String prefix) {...} } Data
StringList: A Recursive Class public class StringList { // instance variables private booleanisEmpty; private String thisString; private StringListrestOfStringList; // constructors • public StringList() {...} • public StringList(String newString, StringListaList) {...} • public String toString() {...} • public String getLineStartingWith(String prefix) {...} • } Methods
StringList: A Recursive Class public class StringList { // instance variables private booleanisEmpty; private String thisString; private StringListrestOfStringList; // constructors public StringList() { … } public StringList(StringnewString, StringListaList) { … } • public String toString() { … } • public String getLineStartingWith(String prefix) { … } }
StringList: In Action • StringListaList = new StringList(); public class StringList { // instance variables private booleanisEmpty; private String thisString; private StringListrestOfStringList; // constructors public StringList() { ... } public StringList(String newString, StringListaList) { ...} public String toString() { ... } public String getLineStartingWith(String prefix) { ... } } addr: 32 aList 32 true "" 0 toString() getLine()
StringList: In Action • StringListaList = new StringList(); public class StringList { // instance variables private booleanisEmpty; private String thisString; private StringListrestOfStringList; // constructors public StringList() { ... } public StringList(String newString, StringListaList) { ... } public String toString() { ... } public String getLineStartingWith(String prefix) { ...} } addr: 32 aList 32 true "" 0 toString() getLine() Actually a reference to a String object, but for brevity…
StringList: In Action • StringListaList = new StringList(); • aList = new StringList("mandolin", aList); addr: 32 aList 32 true "" 0 toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList = newStringList("mandolin", aList); addr: 32 addr: 48 aList 32 true false "" 0 toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList = newStringList("mandolin", aList); addr: 32 addr: 48 aList 32 true false "" "mandolin" 0 toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList = newStringList("mandolin", aList); addr: 32 addr: 48 aList 32 true false "" "mandolin" 0 toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList = newStringList("mandolin", aList); addr: 32 addr: 48 aList 32 true false "" "mandolin" 0 32 toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList = newStringList("mandolin", aList); addr: 32 addr: 48 aList 32 true false "" "mandolin" 0 32 toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList= newStringList("mandolin", aList); addr: 32 addr: 48 aList 32 true false "" "mandolin" 0 32 toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList= new StringList("mandolin", aList); addr: 32 addr: 48 aList 32 true false "" "mandolin" 0 32 toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList= new StringList("mandolin", aList); addr: 32 addr: 48 aList 48 true false "" "mandolin" 0 32 toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList = new StringList("mandolin", aList); addr: 32 addr: 48 aList 48 true false "" "mandolin" 0 32 toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList = new StringList("mandolin", aList); • aList = new StringList("Lilly", aList); addr: 32 addr: 48 aList 48 true false "" "mandolin" 0 32 toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList = new StringList("mandolin", aList); • aList = new StringList("Lilly", aList); addr: 77 addr: 32 addr: 48 false aList 48 true false "" "mandolin" "Lilly" 0 32 48 toString() getLine() toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList = new StringList("mandolin", aList); • aList = new StringList("Lilly", aList); addr: 77 addr: 32 addr: 48 false aList 48 true false "" "mandolin" "Lilly" 0 32 48 toString() getLine() toString() getLine() toString() getLine()
StringList: In Action • StringListaList = new StringList(); • aList = new StringList("mandolin", aList); • aList = new StringList("Lilly", aList); addr: 77 addr: 32 addr: 48 false aList 77 true false "" "mandolin" "Lilly" 0 32 48 toString() getLine() toString() getLine() toString() getLine()
HistoryList: A Recursive Class public class HistoryList { // instance variables private booleanisEmpty; private String firstWebSite; private HistoryListrestOfWebSites; // constructors public HistoryList() { ... } public HistoryList(String newSite, HistoryListaList) { ... } public boolean contains(String site) { ... } public String toString() { ... } public HistoryListgetMatches(String prefix) { ... } public booleanisEmpty() { ... } }
HistoryList: In Action • HistoryListaList = new HistoryList(); • aList = new HistoryList("cnn.com", aList); • aList = new HistoryList("mlb.com", aList); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
HistoryList: contains() method public booleancontains(String site) { • if (empty) • { • return false; • } • else if (firstWebSite.equals(site)) • { • return true; • } • return restOfWebSites.contains(site); }
HistoryList: contains() • booleaninList = aList.contains("cnn.com"); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
HistoryList: contains() • booleaninList = aList.contains("cnn.com"); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
HistoryList: contains() • booleaninList = aList.contains("cnn.com"); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
HistoryList: contains() • booleaninList = aList.contains("cnn.com"); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
HistoryList: contains() • booleaninList = aList.contains("cnn.com"); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
if (empty){ • return false; • } else if (firstWebSite.equals(site)) { • return true; • } • return restOfWebSites.contains(site); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
if (empty){ • return false; • } else if (firstWebSite.equals(site)) { • return true; • } • return restOfWebSites.contains(site); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
if (empty){ • return false; • } else if (firstWebSite.equals(site)) { • return true; • } • return restOfWebSites.contains(site); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
if (empty){ • return false; • } else if (firstWebSite.equals(site)) { • return true; • } • return restOfWebSites.contains(site); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
if (empty){ • return false; • } else if (firstWebSite.equals(site)) { • return true; • } • return restOfWebSites.contains(site); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
if (empty){ • return false; • } else if (firstWebSite.equals(site)) { • return true; • } • return restOfWebSites.contains(site); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
if (empty){ • return false; • } else if (firstWebSite.equals(site)) { • return true; • } • return restOfWebSites.contains(site); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
if (empty){ • return false; • } else if (firstWebSite.equals(site)) { • return true; • } • return restOfWebSites.contains(site); addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
if (empty){ • return false; • } else if (firstWebSite.equals(site)) { • return true; • } • return restOfWebSites.contains(site); true addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()
if (empty){ • return false; • } else if (firstWebSite.equals(site)) { • return true; • } • return restOfWebSites.contains(site); true addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() true
if (empty){ • return false; • } else if (firstWebSite.equals(site)) { • return true; • } • return restOfWebSites.contains(site); true addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" true 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() true
HistoryList: contains() • booleaninList = aList.contains("cnn.com"); true addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" true 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() true
HistoryList: contains() • booleaninList = aList.contains("cnn.com"); true true addr: 87 addr: 42 addr: 58 false aList 87 true false "" "cnn.com" "mlb.com" true 0 42 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() true