Interfaces & Generics

Similar documents
Stacks and Queues. David Greenstein Monta Vista

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CMSC 132: Object-Oriented Programming II. Stack and Queue

Data Structures G5029

Data Structures And Algorithms

Introduction to Generics in Java 5

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

csci 210: Data Structures Stacks and Queues

CS350: Data Structures Stacks

Topic 6: Inner Classes

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Chapter 2. Stack & Queues. M hiwa ahmad aziz

No Aids Allowed. Do not turn this page until you have received the signal to start.

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

CE204 Data Structures and Algorithms Part 2

Name CPTR246 Spring '17 (100 total points) Exam 3

CSCD 326 Data Structures I Stacks

Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Thursday, April 25, 13

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Data Abstraction and Specification of ADTs

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

== isn t always equal?

CS61B Spring 2016 Guerrilla Section 2 Worksheet

CMPS 390 Data Structures

Midterm Exam (REGULAR SECTION)

Queues. Stacks and Queues

CT 229 Object-Oriented Programming Continued

Stacks Fall 2018 Margaret Reid-Miller

Linked Structures. See Section 3.2 of the text.

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

Standard ADTs. Lecture 19 CS2110 Summer 2009

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

More Data Structures (Part 1) Stacks

No Aids Allowed. Do not turn this page until you have received the signal to start.

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CSC 1052 Algorithms & Data Structures II: Queues

CSE 143 SAMPLE MIDTERM

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

CS/ENGRD 2110 SPRING 2018

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

INTERFACES IN JAVA. Prof. Chris Jermaine

Stacks and Queues

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CSC 1052 Algorithms & Data Structures II: Linked Queues

CSC148 Week 2. Larry Zhang

final int a = 10; for(int i=0;i<a;i+=2) { for(int j=i;j<a;j++) { System.out.print("*"); } System.out.println(); }

CS 61B Spring 2017 Guerrilla Section 3 Worksheet. 25 February 2017

Examination Questions Midterm 1

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Inheritance. Transitivity

Basic Data Structures

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

Data Structures and Object-Oriented Design III. Spring 2014 Carola Wenk

Stacks and queues. CSCI 135: Fundamentals of Computer Science Keith Vertanen.

Distributed Systems Recitation 1. Tamim Jabban

CS24 Week 4 Lecture 2

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

CS2012 Programming Techniques II

Basic Data Structures 1 / 24

Programming II (CS300)

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

CS 61B Spring 2017 Guerrilla Section 3 Solutions

Stacks and Queues. Fundamentals of Computer Science.

Announcements Queues. Queues

Data Structures and Algorithms, Winter term 2018 Practice Assignment 3

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Carlos III University of Madrid Telematics Engineering Department Systems Programing Exam, Ordinary Call, June 2013

No Aids Allowed. Do not turn this page until you have received the signal to start. Read this entire page or you ll miss the bonus question.

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Apply to be a Meiklejohn! tinyurl.com/meikapply

CSE 2123: Collections: Priority Queues. Jeremy Morris

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

Derive From A Linear List Class. Stacks. Derive From ArrayLinearList. Derive From ArrayLinearList. Derive From ArrayLinearList.

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

Midterm Exam 2 CS 455, Spring 2011

03/29/2004. A dispenser has three essential features: adding, removing, accessing. Dispensers

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java

Programming II (CS300)

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

1 Short Answer (7 Points Each)

Abstract Data Types. Abstract Data Types

Week 4 Stacks & Queues

Exceptions and Design

CSC212:Data Structure

INTERFACES IN JAVA. Prof. Chris Jermaine

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Tutorial #11 SFWR ENG / COMP SCI 2S03. Interfaces and Java Collections. Week of November 17, 2014

Composite Pattern - Shapes Example - Java Sourcecode

Transcription:

Interfaces & Generics CSC207 Winter 2018

The Programming Interface The "user" for almost all code is a programmer. That user wants to know:... what kinds of object your class represents... what actions it can take (methods)... what properties your object has (getter methods)... what guarantees your methods and objects require and offer... how they fail and react to failure... what is returned and what errors are raised 2

Interfaces An interface is (usually) a class with no implementation. It has just the method signatures and return types. It guarantees capabilities. It is like a contract between groups of programmers. Example: java.util.list "To be a List, here are the methods you must support. All interface methods are automatically public. A class can be declared to implement an interface. This means it defines a body for every method. A class can implement 0 or more interfaces (but may extend only 0 or 1 classes). An interface may extend another interface. 3

Interfaces - Stack Example Stacks have push, pop, and isempty methods. There are lots of implementations array, ArrayList, LinkedList, among others. We can describe how all of them work using an interface. /** A LIFO data structure. public interface Stack { /** Add o to the top of this Stack. * @param o The object to be pushed. void push(object o); /** Remove and return the top item of this Stack. * @return the former top item of the this Stack. Object pop(); /** Return the top item of this Stack. * @return the top item of the this Stack. Object top(); /** Return whether this Stack is empty. boolean isempty(); push pop First In, Last Out 4

A Stack implementation /** A Stack with fixed capacity. public class ArrayStack implements Stack { /** The index of the top element in this Stack. Also the number. private int top; /** contents[0.. top-1] contains the elements in this Stack. private Object[] contents; /** An ArrayStack with capacity for n elements. public ArrayStack(int n) { contents = new Object[n]; /** Add o to the top. (Ignore that we might overflow.) public void push(object o) { contents[top++] = o; /** Remove and return the top element of this Stack. public Object pop() { return contents[--top]; // What if top is 0? /** Return true iff this Stack is empty. public boolean isempty() { return top == 0; 5

Using a Stack You can t create instances of interfaces. This is broken: Stack s = new Stack(15); But you can write methods that use an interface: /** * Fill a stack with the integers 0 to n - 1 (inclusive), * with n - 1 at the top. * @param the Stack to fill * @param n the number of integers to put into the stack public static void fill(stack s, int n) { for (int i = 0; i!= n; i++) { s.push(new Integer(i)); That function will work with any class that implements Stack. 6

Queues (as an intro to generics) Queue ops: enqueue, head, dequeue, size. Let s also decide that all items in a queue must be the same type. /** A queue where all items must be of type T. public interface Queue<T> { /** Append o to me. void enqueue(t o); /** * Return my front item. enqueue * Precondition: size()!= 0. T head(); /** * Remove and return my front item. * Precondition: size()!= 0. T dequeue(); /** Return my number of items. int size(); First In, First Out dequeue 7

Queues (as an intro to generics) /** A queue where all items must be of type T. public class LinkedListQueue<T> implements Queue<T> { /** The items in me. Head is index 0, tail is index size() - 1. private LinkedList<T> contents = new LinkedList<T>(); @Override public void enqueue(t item) { contents.add(item); @Override public T head() { return contents.get(0); @Override public T dequeue() { return contents.removefirst(); @Override public int size() { return contents.size(); 8

Queues (as an intro to generics) public class QueueDemo { public static void fill(queue<integer> queue, int num) { for (int i = 0; i!= num; i++) { queue.enqueue(i); public static void main(string[] args) { // Here is where we decide which Queue implementation to use. Queue<Integer> queue = new LinkedListQueue<>(); fill(queue, 10); System.out.println(queue); 9

Generics: naming conventions The Java Language Specification recommends these conventions for the names of type variables: very short, preferably a single character but evocative all uppercase to distinguish them from class and interface names Specific suggestions: Maps: K, V Exceptions: X Nothing particular: T (or S, T, U or T1, T2, T3 for several) 10

Abstract Classes Similar to interfaces, but may have some implementation. A class declared abstract: may or may not have abstract methods (methods declared without implementation) can t be initiated, but can be extended A child class may implement some or all of the inherited abstract methods. If not all, it must be declared abstract. If all, it s not abstract and so can be instantiated. When to use interfaces vs. abstract classes: https://docs.oracle.com/javase/tutorial/java/iandi/abstract.html 11