CSC153, Class 42: Intro to Abstract Data Types Overview: * Ways of thinking about types * Organizing Data * Example: Arrays * An ADT Hierarchy * Collections Notes: * Questions on exam 3? ---------------------------------------- Exam Questions What do you mean in 2b? * Which of the methods mentioned above should throw an exception? When? + mayPrecede + add ---------------------------------------- /** * Procedure: * myProc * Purpose: * Yeadda yadda yadda. * Parameters: * ... */ For classes: /** * Short description of the purpose of the class. */ e.g. /** * Objects that can compare pairs of objects. */ Procedure documentation /** * Short sentence summarizing procedure. * More sentences describing procedure in more detail. * * @param param1 * Info about the parameter * @param param2 * Info about the parameter * @return * Info about the return value */ ------------------------------------------------------------ What is a type? * A data structure that stores values * Guidance to the kinds of operations you perform on the object * Sets In many object-oriented languages, we primarily think of types as providing guidance about the operations we perform. An interface provides information about the kinds of operations you can apply to things that implement those interfaces It doesn't provide information about implementation That means we can implement the same set of operations in many different ways (even for the same implicit type) Abstract notion: "Abstract Data Type" Particular implementation: "Data Structure" ------------------------------------------------------------ Example: IndexedCollectionsStartingWithIndexZero CONSTRUCTOR(int length) Alternative: lower bound and upper bound void put(int index, Object value) Object get(int index) int getLength() DESTRUCTION is implicit ------------------------------------------------------------ Implementation one: Contiguous memory and math Implementation two: Like lists Implementation three: Variables Implementation four: As a function Implementation five: As many similarly-named variables (we hope) Implementation six: As a tree ------------------------------------------------------------ Observation: One of the key aspects of a data structure is a note as to the expected efficiency of the operations ------------------------------------------------------------ Observation: We can organize our types somewhat hierarchically. Collections: Things that group data IndexedCollection: Things that group data by index NumericallyIndexedCollections: Things that group data that are indexed by number StringIndexCollections ... ExpandableCollection A list is Expandable/Contractable Nondestructively Iterable (has a notion of "current element" and you can move to the next element) Stacks and Queues aren't necessarily iterable But are expandable/contractable Sets are Noniterable Membership-testable Sets provide Create Add Delete member? Variants of Lists * Plain lists, as simple as possible * Ordered lists, client control where things go in the list * Sorted lists, elements are iterated in "sort order" Note: * Ordered lists need a concept of "place", so we'll pretend we've defined an accompanying Place class. What operations do you expect simple lists to provide? What operations do you expect ordered lists to add? * void append(Object val): Put it on the end of the current list * void reverse(): Reverse the list * void addAfter(Place p, Object val) * void addBefore(Place p, Object val)q What operations do you expect sorted lists to provide? GOALS FOR TOMORROW'S CLASS * Argue for one of the two views of Place * Think about operations that belong in all lists and not just ordered lists