270 likes | 390 Views
CompSci 105 SS 2005 Principles of Computer Science. Please try to Understand All concepts involved. Lecture 9: Implementing ADTs. Lecturer: Santokh Singh. Partitioning (Revision). ≥ p. p. <p. 0. 1. 2. 3. 4. 8. 3. 9. 1. 7. 3. 1. 7. 8. 9. 1. 3. 7. 8. 9.
E N D
CompSci 105 SS 2005 Principles of Computer Science Please try to Understand All concepts involved. Lecture 9: Implementing ADTs Lecturer: Santokh Singh
Partitioning (Revision) ≥p p <p 0 1 2 3 4 8 3 9 1 7 3 1 7 8 9 1 3 7 8 9 Textbook, pp. 83
Built-in Data Types 14 int x = 14; 1110
Abstract Data Type A collection of data … combined with … a set of operations on that data Textbook, p. 108
The Wall ADT Operations Program that uses the ADT ADT Implementation Textbook, p. 110
List ADT • Milk • Eggs • Butter • Apples • Bread • Chicken Operations? Textbook, p. 111ff
List ADT • createList() • isEmpty() • size() • add( index, item) • remove(index) • removeAll() • get(index) Operations Textbook, p. 113
List ADT • createList() • isEmpty() • size() • add( index, item) • remove(index) • removeAll() • get(index) aList.createList() aList.add(1, milk) aList.add(2, eggs) aList.add(3, butter) aList.remove(3) Using the ADT Operations Textbook, p. 114
List ADT • createList() • isEmpty() • size() • add( index, item) • remove(index) • removeAll() • get(index) for i=1 to aList.size() { data = aList.get(i) println(data) } Using the ADT Operations Textbook, p. 115
Abstract Data Types (ADTs) Examples Specifying ADTs Combining ADTs Implementing ADTs Interfaces Classes Private vs. Public Constructors Other Methods Exceptions and Inheritance
Defining ADTs • What needs to be stored? • What are the valid operations? Textbook, p. 119
Combining ADTs Dropbox ADT Implementation Program that uses the DropBox DropBox ADT Operations
Combining ADTs Dropbox ADT Implementation List ADT Implementation Program that uses the DropBox DropBox ADT Operations List ADT Operations
Abstract Data Types (ADTs) Examples Specifying ADTs Combining ADTs Implementing ADTs Interfaces Classes Private vs. Public Constructors Other Methods Exceptions and Inheritance
Circle ADT • setX() • setY() • setRadius() • getX() • getY() • getRadius() • getArea() Operations
Java Interface public interface Circle { void setX ( float x ); void setY ( float y ); void setRadius( float r ); float getX (); float getY (); float getRadius(); float getArea (); }
A method that uses Circle public boolean isBigger( Circle A, Circle B) { if ( A.getArea() > B.getArea() ) return(true); else return(false); }
Creating Circles? Circle A = new Circle(); Circle B = new Circle(); boolean b = isBigger( A, B );
Abstract Data Types (ADTs) Examples Specifying ADTs Combining ADTs Implementing ADTs Interfaces Classes Private vs. Public Constructors Other Methods Exceptions and Inheritance
public Class MyCircle implements Circle { float x, y, radius; void setRadius( float newRadius ) { radius = newRadius; } void getArea() { return ( Math.PI * radius * radius ); } }
public Class MyCircle implements Circle { // Constructors MyCircle() { x = 0.0; y = 0.0; radius = 1.0; } MyCircle(float newX, newY, newR ) { x = newX; y = newY; radius = newR; }
A method that uses Circle public boolean isBigger( Circle A, Circle B) { if ( A.getArea() > B.getArea() ) return(true); else return(false); }
Creating Circles Circle A = new MyCircle(); Circle B = new MyCircle(1, 1, 2); boolean x = isBigger( A, B );
public Class MyCircle implements Circle { : : printCircle( ) { System.out.println( x, y, radius ); } : :
public Class AreaCircle implements Circle { float x, y, radius, area; void setRadius( float newRadius ) { radius = newRadius; area = Math.PI * radius * radius ); } void getArea() { return ( area ); } }
Creating Circles Circle A = new MyCircle (); Circle B = new AreaCircle(1, 1, 2); boolean x = isBigger( A, B );