csc 212 sequences & iterators. announcements midterm in one week will cover through chapter 5...

16
CSC 212 Sequences & Iterators

Upload: edgar-waters

Post on 19-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

CSC 212

Sequences & Iterators

Page 2: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Announcements

Midterm in one weekWill cover through chapter 5 of bookMidterm will be open book, open note (but,

closed computer) Will not be at office hours tomorrow

Or at next 3 lectures (lucky you!)Will hold all-day office hours Tuesday

Page 3: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Sequences

Sequences combine List and Vector ADTs Also enables conversion between the two

atRank – returns the position object for a given rank

rankOf – given a position, returns its rankpublic interface Sequence extends List, Vector {public Position atRank(int r) throws •••

public int rankOf(Position p) throws •••

}

Page 4: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Implementing Sequences

Like all ADTs, Sequences do not require any specific implementationWe discuss 2 implementations today:

Array-based Sequences Doubly linked list-based Sequences

Page 5: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Linked-list base Sequence

Node class implements Position interfaceMakes position based calls simpleUsing/finding rank requires searching through

list

nodes/positions

elements

Page 6: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Array-based Sequence

Ranks are easy to useBut, must

create class implementing Position

Use array in circular mannerJust like with

queues

0 1 2 3

positions

elements

S

lf

Page 7: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Group Work

public class Node implements Position { protected Object elem; protected Node prev; protected Node next; ... }

public class LLSequence implements Sequence { protected Node head; protected int size; ...}

public class ArrPos implements Position { protected Object elem; protected int rank;

...

}public class ArrSequence implements Sequence { protected ArrPos arr[]; protected int first; protected int last;

...}

Write atRank(r) & rankOf(p) for each class:

Page 8: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Iterator ADT

Often process data in a collection in orderPosition defines current element But need collection to get next itemCannot easily determine when end is reached

Cannot iterate through data without accessing the list/sequenceLimits modularity & reusability

Page 9: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Iterator Interface

public interface Iterator {boolean hasNext();Object next();

} Must call next() to get initial item

Future calls to next() step through items Should either of these throw an exception?

Page 10: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Getting Iterators

Extend the List (and therefore Sequence) ADT with two methods:

/** Iterate through the * positions in the list */public Iterator positions();/** Iterate through elements in * the list */public Iterator elements();

Page 11: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Using Iterators

Loops can now rely on using Iterators Improves modularity

As long as new collection provides an iterator, can change ADT used for collection at will

Improves reuse Code relies only on iterator existence and not on

any specific implementation details

Page 12: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Review Material for the Midterm

Topics we have discussed: Programming in Java Class hierarchy and inheritance

Field hiding, method overriding, method chaining What assignments are legal and when is typecasting needed

Big-Oh notation Constant, Linear, and Logarithmic time

Recursion

Page 13: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Review Material for the Midterm

More topics:Writing/Using Linked ListsStacks & Queues

Differences between LIFO and FIFO

Vectors, Lists, & Sequences How rank and position works Iterators

Page 14: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Ideas for Reviewing

(Re-)Read book sections you do not understand

Look over daily quizzes and the solutionsMake sure you understand why the answers

are what they are

Page 15: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Ideas for Reviewing

Will post practice midterm on course webpage Will update with more questions on SaturdayPractice contains questions that could be on

an actual midtermAlso posting (separate) answer key

Page 16: CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed

Daily Quiz

Due Friday at 4PM E-mail me a question you think would be

good for the midterm and its solution I usually include questions from studentsProvides a good focus for your studying