Based on slides by Alyssa Harding & Marty Stepp

Slides:



Advertisements
Similar presentations
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Advertisements

Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides created by Marty Stepp
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Building Java Programs
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-4: Interfaces reading: self-check: exercises: #11.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
1 CSE 331 Introduction; Review of Java and OOP slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
CSE 143 Lecture 9 Interfaces; Stacks and Queues slides created by Marty Stepp
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh) reading: 9.5, 11.1, slides created by Marty Stepp
EE 422C Interfaces Day 5. 2 Announcements SVN has Project 2. –Partly due next week. –SVN update to get it in your repository. See Piazza for Grocery List.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs Interfaces reading: , 16.4.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Lecture 21: Interfaces and Abstract classes
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2
Building Java Programs
CSE 373 Implementing a Stack/Queue as a Linked List
Stacks and Queues.
Building Java Programs
Interfaces.
A tree set Our SearchTree class is essentially a set.
slides created by Marty Stepp
Stacks and Queues.
Building Java Programs Chapter 14
Building Java Programs
Building Java Programs
Lecture 1: Welcome to CSE 373
Interfaces EE 422C.
Data Structures and Algorithms
Stacks and Queues.
slides created by Marty Stepp
slides created by Marty Stepp and Ethan Apter
Interfaces CS163 Fall 2018.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
Building Java Programs
A tree set Our SearchTree class is essentially a set.
Building Java Programs
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
Review: Classes, Inheritance, and Collections
Stacks and Queues CLRS, Section 10.1.
slides created by Marty Stepp and Hélène Martin
Shapes Consider the task of writing classes to represent 2D shapes such as Circle, Rectangle, and Triangle. Certain operations are common to all shapes:
slides created by Marty Stepp
slides created by Marty Stepp
Lecture 8: Complex Linked List Code reading: 16.2 – 16.3
Binary search (13.1) binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from consideration. How.
CSE 142 Lecture Notes Inheritance, Interfaces, and Polymorphism
CSE 373 Implementing a Stack Reading: Weiss Ch. 3; 3.6; 1.5
Lecture 20: Interfaces and Abstract classes
slides created by Alyssa Harding
Generics, Stack, Queue Based on slides by Alyssa Harding
slides created by Marty Stepp
slides adapted from Marty Stepp
Building Java Programs
slides created by Marty Stepp
Lecture 7: Linked List Basics reading: 16.2
CSE 143 Lecture 6 interfaces; eclipse; testing
slides created by Marty Stepp
CSE 143 Lecture 21 Advanced List Implementation
slides created by Marty Stepp
Stacks and Queues.
Presentation transcript:

Based on slides by Alyssa Harding & Marty Stepp CSE 143 Lecture 9 Interfaces Stacks, Queues Based on slides by Alyssa Harding & Marty Stepp

ArrayIntList and LinkedIntList classes ArrayIntList and LinkedIntList both store lists of ints. Both classes are very similar in terms of WHAT they can do. Both classes have these methods in common: public int size() {...} public int get(int index) {...} public String toString() {...} public int indexOf(int value) {...} public void add(int value) {...} public void add(int index, int value) {...} public void remove(int index) {...} The two classes differ in HOW they implement these methods 2

An IntList interface } // Represents a list of integers. public interface IntList { public int size(); public int get(int index); public String toString(); public int indexOf(int value); public void add(int value); public void add(int index, int value); public void remove(int index); } public class ArrayIntList implements IntList { ... public class LinkedIntList implements IntList { ...

Interfaces as Type Anywhere we want a IntList, we can use an ArrayIntList or a LinkedIntList: IntList list1 = new ArrayIntList(); IntList list2 = new LinkedIntList(); Declare the variable with the most abstract type possible This lets the compiler know that our variable is guaranteed to have the functionality of an IntList Instantiate the variable with the implementation type This lets the compiler know how to perform the methods, as an ArrayIntList or a LinkedIntList. 4

Interfaces as Type However, we cannot create an instance of an Interface IntList list1 = new IntList(); IntList is abstract; cannot be instantiated But we can declare variables, parameters, return types of type IntList (or Arrays or collections of them) 5

“Favor Interfaces” Tip 52 from Joshua Bloch's “Effective Java” "Refer to objects by their interfaces." “You should favor the use of interfaces rather than classes to refer to objects. If appropriate interface types exist, parameters, return values, variables and fields should all be declared using interface types." "The only time you really need to refer to an object's class is when you're creating it.” 6

An Aside: Interfaces vs. Inheritance interface: A list of methods that classes can promise to implement. Inheritance gives you an is-a relationship and code sharing. A Lawyer object can be treated as an Employee, and Lawyer inherits Employee's code. Interfaces give you an is-a relationship without code sharing. A LinkedIntList object can be treated as a IntList but inherits no code. Analogous to non-programming idea of roles or certifications: "I'm certified as a CPA accountant. The certification assures you that I know how to do taxes, perform audits, and do consulting." "I'm a Shape. I know how to compute my area and perimeter."

Abstract Methods All methods in interfaces are abstract methods. abstract method: A header without an implementation. The actual bodies are not specified, because we want to allow each class to implement the behavior in its own way. A class can declare that it "implements" an interface. The class promises to contain each method in that interface. (Otherwise it will fail to compile.) Example: public class Bicycle implements Vehicle { // Implementations for abstract methods // declared in Vehicle interface. ... }

Shape Interface (Section 9.5) // Describes features common to all shapes. public interface Shape { public double area(); public double perimeter(); } Saved as Shape.java public class Circle implements Shape { ... public class Rectangle implements Shape { ...

Interface requirements public class Banana implements Shape { // haha, no methods! } If we write a class that claims to be a Shape but doesn't implement area and perimeter methods, it will not compile. Banana.java:1: Banana is not abstract and does not override abstract method area() in Shape ^

Interfaces + polymorphism Interfaces benefit the client code author. They allow client code to take advantage of polymorphism (the same code is able to work with different types of objects). public static void printInfo(Shape s) { System.out.println("The shape: " + s); System.out.println("area : " + s.area()); System.out.println("perim: " + s.perimeter()); System.out.println(); } Any shape can be passed as the parameter to the method. Shape circ = new Circle(12.0); Shape tri = new Triangle(5, 12, 13); printInfo(circ); printInfo(tri);

Stacks and Queues

Stacks and queues Sometimes it is good to have a collection that is less powerful, but is optimized to perform certain operations very quickly. Today we will examine two specialty collections: stack: Retrieves elements in the reverse of the order they were added. queue: Retrieves elements in the same order they were added. queue stack

Abstract data types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed on it. Describes what a collection does, not how it does it We don't know exactly how a stack or queue is implemented, and we don't need to. We just need to understand the idea of the collection and what operations it can perform. (Stacks are usually implemented with arrays; queues are often implemented with a linked list.)

Stacks stack: A collection based on the principle of adding elements and retrieving them in the opposite order. Last-In, First-Out ("LIFO") The elements are stored in order of insertion, but we do not think of them as having indexes. The client can only add/remove the last element added (the "top"). basic stack operations: push: Add an element to the top. pop: Remove the top element. analogy: trays of food at the sizzler

Queues queue: Retrieves elements in the order they were added. First-In, First-Out ("FIFO") Elements are stored in order of insertion but don't have indexes. Client can only add to the end of the queue, and can only remove the front of the queue. basic queue operations: enqueue: Add an element to the back. dequeue: Remove the front element. queue analogy: trays of food at the sizzler