12a-interfaces - CSE143 Lecture12(A) Interfaces...

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

View Full Document Right Arrow Icon
CSE 143 Lecture 12 (A) Interfaces reading: 9.5, 11.1 slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Related classes 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.
Background image of page 2
3 Shape area and perimeter Circle (as defined by radius  ): area π   2 perimeter = 2  π   r Rectangle (as defined by width  w  and height  ): 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  +  r w h a b c
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 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. Make a  DrawingPanel  display many shapes on screen.
Background image of page 4
5 Interfaces (9.5) interface A list of methods that a class can promise to implement. Inheritance gives you an is-a relationship 
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

12a-interfaces - CSE143 Lecture12(A) 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