COMP 250. Lecture 29. interfaces. Nov. 18, 2016

Similar documents
COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Java Comparable interface

COMP 250. inheritance (cont.) interfaces abstract classes

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

Programming II (CS300)

CSE 143. Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2

17. Java Collections. Organizing Data. Generic List in Java: java.util.list. Type Parameters ( Parameteric Polymorphism ) Data Structures that we know

Some examples and/or figures were borrowed (with permission) from slides prepared by Prof. H. Roumani. The Collection Framework

EXAMINATIONS 2016 TRIMESTER 2

Building Java Programs. Interfaces and Comparable reading: , 10.2, 16.4

Section Lists and Sets

Programming II (CS300)

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

11/7/18 JAVA GENERICS. Java Collections. Java Collections. Using Java Collections. Proposals for adding Generics to Java.

Dynamic Data Structures and Generics

Photo credit: Andrew Kennedy JAVA GENERICS

doubly linked lists Java LinkedList

Generic Programming Recursive Binary Search SESSION 9. Programming Languages for Objects

Solution to Test of Computer Science 203x Level Score: / 100 Time: 100 Minutes

CSE 143 Lecture 20. Circle

CMSC 202H. Containers and Iterators

Model Solutions. COMP 103: Mid-term Test. 21st of August, 2014

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

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

Java Review: Objects

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

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Collections, Maps and Generics

Model Solutions. COMP 103: Test May, 2013

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

EXAMINATIONS 2017 TRIMESTER 2

[TAP:PMUHE] Stack vs Queue

Csci 102: Sample Exam

Interfaces. An interface defines a set of methods. An interface declaration contains signatures, but no implementations.

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

University of Maryland College Park Dept of Computer Science

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Interfaces and itera-on. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

CS/ENGRD 2110 SPRING 2018

Java Programming. Abstract classes and Interfaces

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

EXAMINATIONS 2005 END-YEAR. COMP 103 Introduction to Data Structures and Algorithms

Family Name:... Other Names:... ID Number:... Signature... Model Solutions. COMP 103: Test 1. 9th August, 2013

Model Solutions. COMP 103: Test April, 2013

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

COMP 103 Introduction to Data Structures and Algorithms

ITI Introduction to Computing II

The list abstract data type defined a number of operations that all list-like objects ought to implement:

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

SUMMARY INTRODUCTION COLLECTIONS FRAMEWORK. Introduction Collections and iterators Linked list Array list Hash set Tree set Maps Collections framework

COMP103 Test. 5 Sept, 2007

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

Array Based Lists. Collections

Name Section Number. CS210 Exam #4 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

CP222 Computer Science II. Linked Lists

Interfaces. Relatedness of types. Interface as a contract. The area and perimeter of shapes 7/15/15. Savitch ch. 8.4

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

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

Points off Total off Net Score. CS 314 Final Exam Spring 2016

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

CS 310: Array-y vs Linky Lists

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 PRACTICE EXAM

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

Pieter van den Hombergh Richard van den Ham. February 8, 2018

Design Patterns. James Brucker

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. June 8, 2017

For this section, we will implement a class with only non-static features, that represents a rectangle

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

CMSC 202. Containers

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016

Binghamton University. CS-140 Fall Interfaces

CSC 1052 Algorithms & Data Structures II: Lists

JAVA COLLECTION FRAMEWORK & SETS

Binghamton University. CS-140 Fall Interfaces

Lists using LinkedList

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

COMP 103 : Test. 2018, Sept 12//(Corrected)

EXAMINATIONS 2010 END YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

CMPSCI 187: Programming With Data Structures. Lecture #21: Array-Based Lists David Mix Barrington 26 October 2012

Data Structures Brett Bernstein

Notes on access restrictions

182 review 1. Course Goals

Data Structures and Algorithms Notes

CMPSCI 187: Programming With Data Structures. Review for Final Exam David Mix Barrington 10 December 2012

Transcription:

COMP 250 Lecture 29 interfaces Nov. 18, 2016 1

ADT (abstract data type) ADT s specify a set of operations, and allow us to ignore implementation details. Examples: list stack queue binary search tree priority queue (heap) hash map graph 2

Java API API = application program interface Gives class methods and some fields, and comments on what the methods do. e.g. https://docs.oracle.com/javase/7/docs/api/java/util/linkedlist.html 3

Java interface reserved word (don t confuse with I in API) like a class, but only the method signatures are defined 4

Example: List interface interface List<T> { void add(t) void add(int, T) T remove(int) boolean isempty() T get( int ) int size() : https://docs.oracle.com/javase/7/docs/api/java/util/list.html 5

class ArrayList<T> implements List<T> { void add(t) {. void add(int, T) {. T remove(int) {. boolean isempty() {. T get( int ) {. int size() {. : Each of the List methods are implemented. (In addition, other methods may be defined and implemented.) 6

class LinkedList<T> implements List<T> { void add(t) {. void add(int, T) {. T remove(int) {. boolean isempty() {. T get( int ) {. int size() {. : Each of the List methods are implemented. (In addition, other methods may be defined and implemented.) 7

Example: how are Java interface s used? List<String> list; list = new ArrayList<String>(); list.add("hello"); list = new LinkedList<String>(); list.add( new String( hi ) ); 8

Example: how are Java interface s used? void someusefulmethod( List<String> list ){ : list.add("hello"); : list.remove( 3 ); The method can be called with a LinkedList or an ArrayList as the parameter. 9

Example: user defined interface interface Shape { double getarea(); double getperimeter(); class Rectangle implements Shape{ : class Circle implements Shape{ : 10

class Rectangle implements Shape{ double height, width; Rectangle( double h, double w ){ height = h; weight = w; double getarea(){ return height * width; double getperimeter(){ return 2*(height + width); 11

class Circle implements Shape{ double radius; Circle( double r ){ radius = r; double getarea(){ return MATH.PI * radius * radius; double getperimeter(){ return 2*MATH.PI * radius 12

Shape s = new Rectangle( 30, 40 ); s = new Circle( 2.5 ); See Assignment 4 for a similar example. 13

Java Comparable interface Suppose you want to define an ordering on instances of some class, and possibly allows some instances to be equal. Sorting, binary search trees, priority queues (heaps) each require a well defined ordering ( < ). 14

Java Comparable interface interface Comparable<T> { int compareto( T t ); e.g. The class String implements Comparable<String>. Q: What does that mean? 15

Java Comparable interface T implements Comparable<T> T t1, t2; Java API recommends that t1.compareto( t2 ) returns: 0, if t1.equals( t2 ) returns true positive number, if t1 > t2 negative number, if t1 < t2 16

Example: Rectangle Q: When are two Rectangle objects equal? A: Their heights are equal and their widths are equal. Q: How can we define a compareto() method for ordering Rectangle objects? 17

class Rectangle implements Shape, Comparable{ boolean equals( Rectangle r ) { return (this.height == r.height) && (this.width == r.width); int compareto( Rectangle r ){ double diff = this.getarea() - r.getarea(); // arbitrary if (diff > 0) return 1; else if (diff == 0.0) return 0; else return -1; // not consistent with Java API recommendation 18

class Rectangle implements Shape, Comparable{ boolean equals( Rectangle r ) { return this.getarea() == r.getarea(); int compareto( Rectangle r ){ double diff = this.getarea() - r.getarea(); // arbitrary if (diff > 0) return 1; else if (diff == 0.0) return 0; else return -1; // Consistent with Java API recommendation 19

Q: If a class implements Comparable, then why would we need an equals() method? A1: Every class has an equals() method. The default is that o1.equals( o2 ) if o1 and o2 are the same object, i.e. ==. A2: The equals() method is called by Java library methods, so you can t just rely on compareto(). 20

Java Iterator interface Motivation 1: we often want to visit all the objects in some collection. But sometimes this is awkward to do. e.g. LinkedList<T>.get(i) BST_Node<T>.getSuccessor() // not covered 21

Java Iterator interface Motivation 2: iterators. We sometimes want to have multiple Analogy: Multiple TA s grading a collection of exams. 22

Java Iterator interface interface Iterator<T> { boolean hasnext(); T next(); // returns current and // advances to next void remove(); // optional 23

Example: Singly linked lists private class SLL_Iterator<T> implements Iterator<T>{ // the client doesn t need to know the name. private SNode<T> cur; SLL_Iterator( SLinkedList<T> list){ cur = list.gethead(); // constructor public boolean hasnext() { return (cur!= null); public T next() { SNode<T> tmp = cur; cur = cur.getnext(); return tmp.getelement(); 24

Java Iterator interface Q: Who constructs the Iterator object for a collection? A: The collection does it. e.g. LinkedList, ArrayList, HashSet How? 25

Java Iterable interface interface Iterable<T> { Iterator<T> iterator(); If a class implements Iterable, then the class has an iterator() method. 26

LinkedList<Shape> list; Shape s; : Iterator<Shape> iter1 = list.iterator(); Iterator<Shape> iter2 = list.iterator(); s = iter1.next() s = iter2.next() s = iter1.next() s = iter2.next() s = iter2.next() The iterators iterate over LinkedList nodes, not Shapes. The next() method returns Shapes. 27

interface Iterable iterator() interface Iterator next() hasnext() class SinglyLinkedList iterator() : class SLLIterator next() hasnext() The iterator() method calls the constructor of the SLLIterator class. 28

ASIDE: Java enhanced for loop For any class that implements Iterable.. Example: LinkedList<String>... list = new LinkedList<String>(); for (String s : list) { System.out.println( s ); 29