1 / 42

Understanding Recursive Classes

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

Download Presentation

Understanding Recursive Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Understanding Recursive Classes CMSC 150

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

  3. 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

  4. 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

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

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

  7. 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…

  8. StringList: In Action • StringListaList = new StringList(); • aList = new StringList("mandolin", aList); addr: 32 aList 32 true "" 0 toString() getLine()

  9. StringList: In Action • StringListaList = new StringList(); • aList = newStringList("mandolin", aList); addr: 32 addr: 48 aList 32 true false "" 0 toString() getLine() toString() getLine()

  10. StringList: In Action • StringListaList = new StringList(); • aList = newStringList("mandolin", aList); addr: 32 addr: 48 aList 32 true false "" "mandolin" 0 toString() getLine() toString() getLine()

  11. StringList: In Action • StringListaList = new StringList(); • aList = newStringList("mandolin", aList); addr: 32 addr: 48 aList 32 true false "" "mandolin" 0 toString() getLine() toString() getLine()

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

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

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

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

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

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

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

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

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

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

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

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

  24. HistoryList: contains() method public booleancontains(String site) { • if (empty) • { • return false; • } • else if (firstWebSite.equals(site)) • { • return true; • } • return restOfWebSites.contains(site); }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  39. 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

  40. 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

  41. 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

  42. 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

More Related