UNDERSTANDING RECURSIVE CLASSES CMSC 150: Lecture 22.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Why not just use Arrays? Java ArrayLists.
Chapter 7. Binary Search Trees
Singly linked lists Doubly linked lists
JAVA Revision Lecture Electronic Voting System Marina De Vos.
Linear Lists – Array Representation
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Lecture 5: Objects and Classes, cont’d Continue with the Player example Introduce GOAL as a final static variable that applies to all objects of the class.
Generalized De Morgan’s Theorem Lecture L5.4 Section 5.1.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Crossword Puzzle Solver Michael Keefe. Solver structure.
Chapter 7 Stacks II CS Data Structures I COSC 2006
08 1 Abstract Data Types Problem Sets: PS2 due due Monday, Feburary 26 PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 08 Monday, February 26.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Overriding toString()
TeachScheme, ReachJava Adelphi University Friday morning July 16, 2010.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
A: A: double “4” A: “34” 4.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Week 14 - Wednesday.  What did we talk about last time?  Heapsort  Timsort  Counting sort  Radix sort.
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
Lecture 3: More on Classes Textbook: Chapter 2 Recap: –Classes vs. Objects –Q: What comes first a class or an object? –Q: Do we need a class to create.
Section 2.3 Array-Based StringLog ADT Implementation.
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Section 3.7 Linked-Based Implementations
Re-Intro to Object Oriented Programming
Section 2.6 Linked List StringLog ADT Implementation
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Software Development Java Classes and Methods
Abstract Factory Pattern
Agenda Warmup AP Exam Review: Litvin A2
Generic array list and casting C&K s7.7
COMPUTER 2430 Object Oriented Programming and Data Structures I
Graphs and Cycles.
ارث بری 2 استفاده ی مجدد از کلاس توسط وراثت
Review Operation Bingo
Classes Variables That Are Not of a Built-in Type Are Objects
Lecture 2: Implementing ArrayIntList reading:
ارث بری 2 استفاده ی مجدد از کلاس توسط وراثت
String Methods: length substring
COMPUTER 2430 Object Oriented Programming and Data Structures I
Name Not Precise Enough
Queues: Implemented using Arrays
CS 302 Week 9 Jim Williams.
© A+ Computer Science - OOP © A+ Computer Science -
COSC 1030 Section 8 List.
Class Examples.
Simple Classes in Java CSCI 392 Classes – Part 1.
Tyler, Jonah, Abby, Gavin, Zain, Matt
Circular Queues: Implemented using Arrays
Ordered Structures Wellesley College CS230 Lecture 10
Objects with ArrayLists as Attributes
CSC 205 Java Programming II
Presentation transcript:

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

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

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

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

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:

StringList: In Action  StringList aList = new StringList(); aList true "" toString() getLine() Actually a reference to a String object, but for brevity… addr: 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) { … } }

StringList: In Action  StringList aList = new StringList();  aList = new StringList("mandolin", aList); aList true "" toString() getLine() addr:

StringList: In Action  StringList aList = new StringList();  aList = new StringList("mandolin", aList); aList true "" toString() getLine() toString() getLine() false addr: addr: 48 0

StringList: In Action  StringList aList = new StringList();  aList = new StringList("mandolin", aList); aList true "" toString() getLine() toString() getLine() "mandolin" false addr: addr: 48

StringList: In Action  StringList aList = new StringList();  aList = new StringList("mandolin", aList); aList true "" toString() getLine() toString() getLine() "mandolin" false addr: addr: 48

StringList: In Action  StringList aList = new StringList();  aList = new StringList("mandolin", aList); aList true "" toString() getLine() toString() getLine() "mandolin" false addr: addr: 48 32

StringList: In Action  StringList aList = new StringList();  aList = new StringList("mandolin", aList); aList true "" toString() getLine() toString() getLine() "mandolin" false addr: addr: 48 32

StringList: In Action  StringList aList = new StringList();  aList = new StringList("mandolin", aList); aList true "" toString() getLine() toString() getLine() "mandolin" false addr: addr: 48 32

StringList: In Action  StringList aList = new StringList();  aList = new StringList("mandolin", aList); aList true "" toString() getLine() toString() getLine() "mandolin" false addr: addr: 48 32

StringList: In Action  StringList aList = new StringList();  aList = new StringList("mandolin", aList); aList true "" toString() getLine() toString() getLine() "mandolin" false addr: addr: 48 32

StringList: In Action  StringList aList = new StringList();  aList = new StringList("mandolin", aList); aList true "" toString() getLine() toString() getLine() "mandolin" false addr: addr: 48 32

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: addr: 48 32

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: addr: toString() getLine() false addr: 77 "Lilly" 48

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: addr: toString() getLine() false addr: 77 "Lilly" 48

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: addr: toString() getLine() false addr: 77 "Lilly" 48

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

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: addr: false addr: 87 "mlb.com" 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()

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

HistoryList: contains()  boolean inList = aList.contains("cnn.com"); aList true "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: false addr: 87 "mlb.com" 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()

HistoryList: contains()  boolean inList = aList.contains("cnn.com"); aList true "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: false addr: 87 "mlb.com" 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()

HistoryList: contains()  boolean inList = aList.contains("cnn.com"); aList true "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: false addr: 87 "mlb.com" 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()

HistoryList: contains()  boolean inList = aList.contains("cnn.com"); aList true "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: false addr: 87 "mlb.com" 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()

HistoryList: contains()  boolean inList = aList.contains("cnn.com"); aList true "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: false addr: 87 "mlb.com" 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty()

true "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: 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

true "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: 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 "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: 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 "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: 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 "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: 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 "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: 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 "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: 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 "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: 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 "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: 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

"" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: 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

"" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: 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

HistoryList: contains()  boolean inList = aList.contains("cnn.com"); true "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: false addr: 87 "mlb.com" 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() aList true

HistoryList: contains()  boolean inList = aList.contains("cnn.com"); true "" contains() toString() getMatches() isEmpty() "cnn.com" false addr: addr: false addr: 87 "mlb.com" 58 contains() toString() getMatches() isEmpty() contains() toString() getMatches() isEmpty() aList true