09-stack_queue - CSE143 Lecture9 Interfaces;StacksandQueues...

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

View Full Document Right Arrow Icon
CSE 143 Lecture 9 Interfaces; Stacks and Queues 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 Document Right Arrow Icon
2 Related classes Consider the task of writing classes to represent shapes such  as  Circle Rectangle , and  Triangle . Certain operations are common to all shapes: perimeter area
Background image of page 2
3 Shape area and perimeter Circle: area π   2 perimeter = 2  π   r Rectangle: area w h perimeter = 2 w  + 2 h Triangle: 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 Document Right Arrow Icon
4 Printing shape information We'd like our client code to be able to print the area and  perimeter of our shapes. Of course, we would use methods. .. public static void printCircleInfo(Circle c) { System.out.println("area = " + c.area()); System.out.println("perimeter = " + c.perimeter()); } public static void printTriangleInfo(Triangle t) { System.out.println("area = " + t.area()); System.out.println("perimeter = " + t.perimeter()); } public static void printRectangleInfo(Rectangle r) { System.out.println("area = " + r.area()); System.out.println("perimeter = " + r.perimeter()); } Redundancy—ewwwwwww!!!!  What can we do about this?
Background image of page 4
5 Printing shape information What about having shapes inherit from a superclass  Shape   that has the  area  and  perimeter  methods so we can have  polymorphic  methods? public class Rectangle extends Shape { . .. } public class Triangle extends Shape { . .. } public class Circle extends Shape { . .. } public static void printShapeInfo(Shape s) { System.out.println("area = " + s.area()); System.out.println("perimeter = " + s.perimeter()); } What is wrong with this solution? - What does the  Shape  class look like? 
Background image of page 5

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

View Full Document Right Arrow Icon
6 Inheritance doesn’t apply public class Shape { public double area() { // what goes here? } public double perimeter() { // what goes here? } } This doesn’t work—different shapes compute the area and  perimeter differently!
Background image of page 6
Interfaces (9.5) interface A list of methods that a class can promise to implement. Inheritance gives you an is-a relationship  and  code sharing. Lawyer  can be treated as an  Employee  and inherits its code. Interfaces give you an is-a relationship  without  code sharing. Rectangle  object can be treated as a  Shape  but inherits no code. Analogous to non-programming idea of roles or certifications: "I'm certified as a CPA accountant. This assures you I know how to do taxes, audits, and consulting." "I'm 'certified' as a Shape, because I implement the Shape interface. This assures you I know how to compute my area and perimeter."
Background image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 36

09-stack_queue - CSE143 Lecture9 Interfaces;StacksandQueues...

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

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