This preview shows pages 1–6. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides created by Marty Stepp http://www.cs.washington.edu/143/ 2 Linked vs. array lists We have implemented two collection classes: ArrayIntList LinkedIntList They have similar behavior, implemented in different ways. We should be able to treat them the same way in client code. But how? 9 173 42 value 3 2 1 index front 42 next data3 next data 17 next data 9 next data 3 Another example: Shapes Consider the task of writing classes to represent 2D shapes such as Circle , Rectangle , and Triangle . Certain operations are common to all shapes: perimeter: distance around the outside of the shape area: amount of 2D space occupied by the shape Every shape has these, but each computes them differently. 4 Shape area and perimeter Circle (as defined by radius r ): area = r 2 perimeter = 2 r Rectangle (as defined by width w and height h ): area = w h perimeter = 2 w + 2 h Triangle (as defined by side lengths a , b , and c ) area = ( s ( s a ) ( s b ) ( s c )) where s = ( a + b + c ) perimeter = a + b + c r w h a b c 5 Common behavior Suppose we have 3 classes Circle , Rectangle , Triangle . Each has the methods perimeter and area . We'd like our client code to be able to treat different kinds of shapes in the same way: Write a method that prints any shape's area and perimeter. Create an array to hold a mixture of the various shape objects. Write a method that could return a rectangle, a circle, a triangle, or any other kind of shape....
View Full
Document
 Spring '11
 john

Click to edit the document details