14-interfaces-adts - CSE 143 Lecture 14 Interfaces;...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 17-3 42 value 3 2 1 index front 42 next data-3 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

Page1 / 18

14-interfaces-adts - CSE 143 Lecture 14 Interfaces;...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online