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.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Advertisements

Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
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.
TCSS 342, Winter 2005 Lecture Notes
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Building Java Programs
CS 171: Introduction to Computer Science II Stacks Ymir Vigfusson.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Topic 3 The Stack ADT.
Chapter 3 Introduction to Collections – Stacks Modified
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
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.
12-1 Priority Queues, Sets, and Bags Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 12 Monday, March 12 Handout #22.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Iteration Abstraction SWE Software Construction Fall 2009.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Ordered Structures Wellesley College CS230 Lecture 11
Sections 3.4 Formal Specification
Building Java Programs
Stacks and Queues.
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stacks and Queues.
Building Java Programs
Iteration Abstraction
Stacks and Queues.
תוכנה 1 בשפת Java שיעור מספר 5: "זרוק לו עצם"
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
slides created by Alyssa Harding
Stacks Abstract Data Types (ADTs) Stacks
Object-Oriented Programming: Classes, Inheritance, and Interfaces
slides created by Alyssa Harding
Object-Oriented Programming: Classes, Inheritance, and Interfaces
Ordered Structures Wellesley College CS230 Lecture 10
Stacks and Queues.
Presentation transcript:

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 Handout #16 Note: This lecture includes slides from Lectures 06 & 07 that were not covered in those lectures

08 2 What is an Abstract Data Type (ADT)? An Abstract Data Type (ADT) is a contract that specifies the behavior of methods that operate on a data structure without exposing the representation of the data structure. In general, there are many different implementations of a given ADT. In this lecture: We explore ways of specifying and implementing ADTs in Java. We study some sample ADTs: –Immutable Points –Comparables –Iterators –Stacks

08 3 Immutable Point Contract // Constructor Method public IPoint (int x, int y); /* Create an immutable point with coordinates x and y */ // Instance Methods public int getX(); // Return the x-coordinate of this point. public int getY(); // Return the y-coordinate of this point public String toString(); // Return string of form (x,y)

08 4 Two Implementations of Immutable Points public class IPointVars { // Give classes (and constructors) different names private int x, y; // Represent coords with two int variables. public IPointVars (int x, int y) {this.x = x; this.y = y;} public int getX() { return x; } public int getY() { return y; } public String toString() { return "(" + getX() + "," + getY() + ")"; } } public class IPointArray { private int[] ints = new int[2]; // Represent coords with two-slot array. public IPointArray (int x, int y) {ints[0] = x; ints[1] = y;} public int getX() { return ints[0]; } public int getY() { return ints[1]; } public String toString() { return "(" + getX() + "," + getY() + ")"; } }

08 5 Manipulating Immutable Points public static int sumCoordsVars (IPointVars p) { return p.getX() + p.getY(); } public static int sumCoordsArray (IPointArray p) { return p.getX() + p.getY(); } The sumCoord methods are the same modulo the point type. Can we define a single sumCoords() method that works on instances of both classes? Yes! By using (1) an interface or (2) an abstract class.

08 6 An IPoint Interface public interface IPoint { public int getX(); public int getY(); public String toString(); } public class IPointVars implements IPoint { … same impl. as before …} public class IPointArray implements IPoint { … same impl. as before …} public static int sumCoords (IPoint p) { return p.getX() + p.getY(); } sumCoords(new IPointVars(1,2)) → 3 sumCoords(new IPointArray(3,4)) → 7 Gives new information to Java type checker An interface specifies the types of some instance method contracts.

08 7 An IPoint Abstract Class public abstract class IPoint { public abstract int getX(); public abstract int getY(); public String toString() { return "(" + getX() + "," + getY() + ")"; } } public class IPointVars extends IPoint { … same impl. as before except no toString() method …} public class IPointArray extends IPoint { … same impl. as before except no toString () method …} public static int sumCoords (IPoint p) { return p.getX() + p.getY(); } sumCoords(new IPointVars(1,2)) → 3 sumCoords(new IPointArray(3,4)) → 7 Use extends rather than implements with abstract class A class must be declared abstract if any of its methods are declared abstract. A method without a body is declared abstract. An abstract class may have concrete methods; these can use abstract methods.

08 8 Interfaces vs. Abstract Classes Interfaces are used solely to specify a contract (API/ADT) All methods in an interface must be public instance methods (no class methods or constructor methods) All variables in an interface must be public static final variables with initializers. E.g. public static final CLUBS = 0; public static final DIAMONDS = 1; … Interfaces may have subinterfaces: public interface ColoredIPoint extends IPoint { … } Classes can extend one class but implement any number of interfaces. public class Frobnitz extends Frob implements IPoint, Cloneable, Comparable In CS230, we will use interfaces to specify ADTs Abstract Types are used to specify a partially implemented class; they can also be used for an API. Unlike interfaces, abstract classes may include methods with code. Abstract classes may include (non-abstract) static methods. In CS230, we will use abstract classes to specify methods shared among different implementations of the same interface.

08 9 Interfaces and Abstract Classes in Java Some standard Java Interfaces: Cloneable: public Object clone(); Comparable : public int compareTo (T x); java.util.Iterator : public boolean hasNext(); public T next(); public void remove(); (optional) java.util.Iterable : public Iterator iterator(); java.util.Collection : … discussed in a later lecture … java.awt.event.ActionListener: … discussed in GUI lectures … Some CS230 Java Interfaces: Stack, Queue, Set, Bag, … Some standard Java Abstract Classes: InputStream java.awt.Component java.awt.AWTEvent

08 10 Cloneable & Comparable Example public class FancyIPointVars extends IPointVars implements Cloneable, Comparable { public FancyIPointVars (int x, int y) { super(x,y); } public Object clone() { return new FancyIPointVars(getX(), getY()); } // Compare lexicographically, first by x position, then by y position public int compareTo (IPoint p) { if (getX() < p.getX()) { return -1; } else if (getX() > p.getX()) { return 1; } else { return getY() – p.getY(); }

08 11 Cloneable & Comparable Example Continued FancyIPointVars p = new FancyIPointVars (3,4); FancyIPointVars p2 = (FancyIPointVars) p.clone(); p.compareTo(p2)  p.compareTo(new IPointVars(2,3))  p.compareTo(new IPointVars(3,0))  p.compareTo(new IPointVars(3,10))  p.compareTo(new IPointVars(4,0))  (new IPointVars(4,0)). compareTo(p) 

08 12 Iterator Example public static void displayIterator (Iterator elts) { while elts.hasNext() { System.out.println(elts.next().toString()); } Suppose v is a Vector with the following string elements: “aardvark”, “beetle”, “cat”, “dog”, “elephant” Then displayIterator(v.iterator()) displays the following on the console: aardvark beetle cat dog elephant

08 13 Iterator Removal Example public static void removeShort (Iterator elts) { while elts.hasNext() { if (elts.next().length() <= 3) { elts.remove(); } Then removeShort(v); displayIterator(v.iterator()) displays the following: aardvark beetle elephant

08 14 Vector Filtering Gotcha! How could we remove short strings from a vector without an iterator? Public static void removeShort (Vector v) { }

08 15 Stack Interface import java.util.*; // imports Iterator and Iterable /** Interface to mutable stacks with elements of type T. A stack is a last-in-first-out (LIFO) collection with duplicates. WARNING: This CS230 Stack is an interface, and is *different* from the class named java.util.Stack. */ public interface Stack extends Iterable { public void push(T elt); // Push elt on top of the stack. public T pop(); // Remove and return top element from stack. // Throw RuntimeException if stack is empty. public T top(); // Return top element of stack without removing it. // Throw RuntimeException if stack is empty. public boolean isEmpty(); // Return true if this stack is empty; else false public int size(); // Return the number of elements in this stack public void clear(); // Remove all elements from this stack public Stack copy(); // Return a shallow copy of this stack. public Iterator iterator(); // Return an iterator that yields this stack's // elements from top to bottom. }

08 16 Stack Examples 1 Stack s = new Stack ; s.isEmpty()  s.size()  s.top()  s.push(“B”); s.isEmpty()  s.size()  s.top()  s.push(“C”); s.isEmpty()  s.size()  s.top()  s.push(“B”); s.isEmpty()  s.size()  s.top()  s.push(“A”); s.isEmpty()  s.size()  s.top()  s.pop()  s.isEmpty()  s.size()  s.top() 

08 17 Stack Examples 2 s.push(“D”); s.isEmpty()  s.size()  s.top()  s.push(“A”); s.isEmpty()  s.size()  s.top()  s.push(“C”); s.isEmpty()  s.size()  s.top()  Stack s2 = s.copy(); s.isEmpty()  s.size()  s.top()  s2.isEmpty()  s2.size()  s2.top()  s.clear(); s.isEmpty()  s.size()  s.top()  s2.isEmpty()  s2.size()  s2.top()  displayIterator(s2.iterator()); displayIterator(s.iterator());

08 18 Better Printout of Stack Contents /** prints the contents of a Stack from top to bottom */ public static void printStack (Stack stk) { Stack tempStack = new Stack (); //tempStack to hold stk contents String S; System.out.print("Contents of Stack from top to bottom: "); while (!stk.empty()) { System.out.print(stk.pop().toString + “ “)l tempStack.push(S); } System.out.println(); // restore contents of stk while(!tempStack.isEmpty()) stk.push(tempStack.pop()); }

08 19