Top Banner
Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
50

Problem of the Day Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

Dec 13, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

Example of Perfect Design

Page 13: Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

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)

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)

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)

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)

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)

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)

Why Should You Care?

ITERABLE

Page 20: Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

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)

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)

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)

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)

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)

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)

What Type Should Cursor Be?

Page 27: Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

What Type Should Cursor Be?

Page 28: Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

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)

What Type Should Cursor Be?

Page 30: Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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