problem of the day simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
TRANSCRIPT
![Page 1: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/1.jpg)
Problem of the Day
Simplify this equation:
(x - a) * (x - b) * (x - c) * … * (x - z)
![Page 2: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/2.jpg)
Problem of the Day
Simplify this equation:
(x - a) * (x - b) * (x - c) * … * (x - z)
(x - a) * (x - b) * (x - c) * …*(x – x)*…*(x - z)
![Page 3: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/3.jpg)
Problem of the Day
Simplify this equation:
(x - a) * (x - b) * (x - c) * … * (x - z)
(x - a) * (x - b) * (x - c) * …*(x – x)*…*(x - z)
(x - a) * (x - b) * (x - c) * …* 0 *…*(x - z)
= 0
![Page 4: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/4.jpg)
LECTURE 30:ITERATOR AND ITERABLE
CSC 212 – Data Structures
![Page 5: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/5.jpg)
List ADT
Collection which we can access all elements Add element after an existing one Collection can remove any element it
contains Loop over all elements without removing
them List ADTs differ in how they provide
access ArrayList’s indices give quick access to
specific position Good at working at relative location with
LinkedList
![Page 6: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/6.jpg)
List ADT
Collection which we can access all elements Add element before an existing one Collection can remove any element it
contains Loop over all elements without removing
them List ADTs differ in how they provide
access ArrayList’s indices give quick access to
specific position Good at working at relative location with
LinkedList
![Page 7: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/7.jpg)
List ADT
Collection which we can access all elements Add element before an existing one Collection can remove any element it
contains Loop over all elements without removing
them List ADTs differ in how they provide
access ArrayList’s indices give quick access to
specific position Good at working at relative location with
LinkedList
![Page 8: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/8.jpg)
List ADT
Collection which we can access all elements Add element before an existing one Collection can remove any element it
contains Loop over all elements without removing
them List ADTs differ in how they provide
access ArrayList’s indices give quick access to
specific position Good at working at relative location with
LinkedList
Must violate ADT to access List’s elements
![Page 9: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/9.jpg)
List ADT
Collection which we can access all elements Add element before an existing one Collection can remove any element it
contains Loop over all elements without removing
them List ADTs differ in how they provide
access ArrayList’s indices give quick access to
specific Good at working at relative location with
LinkedList
Must violate ADT to access List’s elements
![Page 10: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/10.jpg)
Iterators
Provides data access without knowing more As an ADT, methods independent of data
storage Access elements 1-by-1 by calling next()method
Like Comparator; does not store data it uses Iterator class specific to instance holding
data But means that code using Iterator
independent Why is this important?
![Page 11: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/11.jpg)
Iterators
Provides data access without knowing more As an ADT, methods independent of data
storage Access elements 1-by-1 by calling next()method
Like Comparator; does not store data it uses Iterator class specific to instance holding
data But means that code using Iterator
independent Why is this important?
Breaks up code into separate class for each function
Each part replaceable when better approach found
![Page 12: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/12.jpg)
Example of Perfect Design
![Page 13: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/13.jpg)
Iterator Interface
package java.util;public interface Iterator<E> {
boolean hasNext();
E next() throws NoSuchElementException;
void remove() throws UnsupportedOperationException;
}
![Page 14: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/14.jpg)
Iterator Needs a Cursor
Uses cursor like the one in Eclipse, Word, PPT, … Like in Eclipse: cursor marks location to be
used Data EXTERNAL to ITERATOR, like
normal cursor When run, application uses and moves
cursor around Cursor not part of document and not found
in file Only matters when editing: cursor useful
only with data
![Page 15: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/15.jpg)
Limit of Iterator
Interface provides remove(), but… …implementing it is a royal pain in the Support not required by the interface Instead throw UnsupportedOperationException
Relying on remove() risky, since it is optional When in doubt, skip it
![Page 16: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/16.jpg)
Limits to Iterator
ITERATOR’s cursor differs from cursor in Word, PPT Cannot add or modify data; (mostly) used to
read data Instance goes through data once, cursor only
advances Moves forward item-by-item only, cannot jump
around
![Page 17: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/17.jpg)
Limits to Iterator
ITERATOR’s cursor differs from cursor in Word, PPT Cannot add or modify data; (mostly) used to
read data Instance goes through data once, cursor only
advances Moves forward item-by-item only, cannot jump
around
![Page 18: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/18.jpg)
Limits to Iterator
Iterator’s methods are very, very limited Iterator instance has no data; limits possible
uses While iterating, can be tempting to
modify data Requires actual instance, ITERATOR does not
store data If underlying instance changes, results
undefined May work, may crash, or may do both; caveat
emptor
![Page 19: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/19.jpg)
Why Should You Care?
ITERABLE
![Page 20: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/20.jpg)
Iterable Interface
So simple makes Iterator look complex
Java’s prettiest feature relies on this interface
![Page 21: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/21.jpg)
Iterable Interface
So simple makes Iterator look complex
Java’s prettiest feature relies on this interface
![Page 22: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/22.jpg)
Iterable Interface
So simple makes Iterator look complex
Java’s prettiest feature relies on this interface
package java.lang;
public interface Iterable<E> { public Iterator<E> iterator();
}
![Page 23: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/23.jpg)
Iterable Usage
Occasionally use Iterator; Iterable preferable Many ADT definitions include Iterable in
definition Makes it possible to interchange ADT when
processing
Iterable support built-in to Java language Like Comparable, interface in java.lang
package
For-each loops through Iterable data directly Translates code to use Iterator (without
extra code)
![Page 24: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/24.jpg)
For-Each for the Win
Slightly different loop than normal for loopfor (type variableName : IterableVar)
List<Integer> idx;OrderedList<Costume> profFirst;ArrayList<Scanner> whyNot;
// Lots of code is here including assignments to our lists
for (Integer i : idx ) { … }for (Costume cost : profFirst) { … }for (Scanner stupid : whyNot) { … }
![Page 25: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/25.jpg)
For-Each Rocks The Hizzy
Integer findSum(Iterable<Integer> able) {Integer retVal = 0; for (Integer datum : able) { retVal += datum; }return retVal;
}
able could be (almost) ANY COLLECTION class ARRAYLIST & LINKEDLIST are examples;
many will follow
![Page 26: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/26.jpg)
What Type Should Cursor Be?
![Page 27: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/27.jpg)
What Type Should Cursor Be?
![Page 28: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/28.jpg)
Implementing Iterator
Very implementation specific issues for cursor To iterate over an ARRAYLIST, cursor is index Cursor is node for LINKEDLIST’s ITERATOR
Iterator’s methods always use similar algorithm General outline identical, since interface
defines task But implementation changes since cursor use
differs
![Page 29: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/29.jpg)
What Type Should Cursor Be?
![Page 30: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/30.jpg)
Same Algorithm, But...
Algorithm next()if hasNext() then E retVal = value at cursor’s location Advance cursor to refer to next location return retValelse throw new NoSuchElementExceptionendif
end
![Page 31: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/31.jpg)
Same Algorithm, But...
Algorithm next()if hasNext() then E retVal = value at cursor’s location Advance cursor to refer to next location return retValelse throw new NoSuchElementExceptionendif
end
![Page 32: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/32.jpg)
Implementing Iterator (Start)public class ALIterator<E> { private ArrayList<E> theList; private int cursor; public ALIterator(ArrayList<E> list) { theList = list; cursor = 0; }}
public class LLIterator<E> { private LinkedList<E> theList; private DNode<E> cursor; public LLIterator(LinkedList<E> list) { theList = list; cursor = }}
![Page 33: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/33.jpg)
Implementing Iterator (Start)public class ALIterator<E> { private ArrayList<E> theList; private int cursor; public ALIterator(ArrayList<E> list) { theList = list; cursor = 0; }}
public class LLIterator<E> { private LinkedList<E> theList; private DNode<E> cursor; public LLIterator(LinkedList<E> list) { theList = list; cursor = }}
![Page 34: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/34.jpg)
Implementing the Iterator
Iterator class needs access to List’s internal fields Implementationless ADT concept must be
violated Code specific to implementation and not
reusable All CSC212 design concepts get thrown out
window
![Page 35: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/35.jpg)
Implementing the Iterator
Iterator class needs access to List’s internal fields Implementationless ADT concept must be
violated Code specific to implementation and not
reusable All CSC212 design concepts get thrown out
window
![Page 36: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/36.jpg)
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
LIST
![Page 37: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/37.jpg)
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
LIST
ALIteratorcursor 0theList
![Page 38: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/38.jpg)
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
LIST
ALIteratorcursor 1theList
![Page 39: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/39.jpg)
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
LIST
ALIteratorcursor 2theList
![Page 40: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/40.jpg)
LIST
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
ALIteratorcursor 3theList
![Page 41: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/41.jpg)
LIST
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
ALIteratorcursor ?theList
![Page 42: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/42.jpg)
LIST
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
ALIteratorcursor 4theList
![Page 43: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/43.jpg)
Implementing for LinkedList
cursor is next node in LinkedList Need to know class specifics to use
node types No methods in LinkedList provide
required info
head tail
LLIterator cursor theList
![Page 44: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/44.jpg)
Implementing for LinkedList
cursor is next node in LinkedList Need to know class specifics to use
node types No methods in LinkedList provide
required info
head tail
LLIterator cursor theList
![Page 45: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/45.jpg)
Implementing for LinkedList
cursor is next node in LinkedList Need to know class specifics to use
node types No methods in LinkedList provide
required info
head tail
LLIterator cursor theList
![Page 46: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/46.jpg)
Implementing for LinkedList
cursor is next node in LinkedList Need to know class specifics to use
node types No methods in LinkedList provide
required info
head tail
LLIterator cursor theList
![Page 47: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/47.jpg)
Implementing for LinkedList
cursor is next node in LinkedList Need to know class specifics to use
node types No methods in LinkedList provide
required info
head tail
LLIterator cursor theList
![Page 48: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/48.jpg)
Comparing Implementations
ArrayList Iterator LinkedList Iterator
Check if cursor valid:
cursor!=theList.size()
Get element at cursor:
theList.get(cursor) orarray[cursor]
Advancing cursor:
cursor += 1
Check if cursor valid:
cursor!=null
Get element at cursor:
cursor.getElement()
Advancing cursor:
cursor = cursor .getNext ()
![Page 49: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/49.jpg)
Your Turn
Get into your groups and complete activity
![Page 50: Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)](https://reader036.vdocuments.site/reader036/viewer/2022062515/56649f575503460f94c7c0b4/html5/thumbnails/50.jpg)
For Next Lecture
Read 8.1 – 8.4 before Friday’s lecture What is recursion & how does it work? What happens to locals if method called
again? Why did we spend so much time on tracing
methods?
Week #11 assignment available on Angel
No class on Monday; I will be out of town As a result, I am delaying quiz until
Wednesday