CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

Slides:



Advertisements
Similar presentations
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Advertisements

Interfaces.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.
15-Jun-15 Lists in Java Part of the Collections Framework.
Professor Evan Korth (adapted from Sun’s collections documentation)
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 1 Topic 12 ADTS, Data Structures, Java Collections and Generic Data Structures.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Lists and the Collection Interface Review inheritance & collections.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
The Java Collections Framework Based on
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
Collections Dwight Deugo Nesa Matic
Object Oriented Programming in Java Habib Rostami Lecture 7.
CS-2852 Data Structures Week 4, Class 1 - Review Review! Thursday Exam I - Review Implementing ArrayList Big-O Iterators – High-level description Linked.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
Modern Programming Tools And Techniques-I
Implementing ArrayList Part 1
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
Programming in Java Lecture 11: ArrayList
Introduction to Collections
Lecture 26: Advanced List Implementation
Introduction to Collections
ArrayLists 22-Feb-19.
Introduction to Collections
Based on slides by Alyssa Harding & Marty Stepp
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
slides created by Alyssa Harding
Part of the Collections Framework
CSE 143 Lecture 21 Advanced List Implementation
Java Generics & Iterators
Presentation transcript:

CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz Agenda Abstract Data Types (ATDs) Interfaces Abstract Classes Generics Collection List

CS-2852 Data Structures, Andrew J. Wozniewicz Abstract Data Type A mathematical construct/model. A pure abstraction: no implementation details whatsoever. Preconditions & postconditions A set of values and a set of operations on those values. DEFINITION

CS-2852 Data Structures, Andrew J. Wozniewicz ADT Examples STACK: – Push(x) – x <- Pop() QUEUE: – Enqueue(x) – y <- Dequeue() LIST: – Add(x) – x <- Remove() – X <- getAt(n) – setAt(n,X) – b <- Contains(X) LIFO FIFO

CS-2852 Data Structures, Andrew J. Wozniewicz Abstract Classes Cannot be instantiated. May or may not include abstract methods. May or may not include concrete methods. A class that is declared abstract. DEFINITION

CS-2852 Data Structures, Andrew J. Wozniewicz Abstract Class Example public abstract class GraphicObject { // declare fields... // declare non-abstract methods... abstract void draw(); //<-a. method }

CS-2852 Data Structures, Andrew J. Wozniewicz Abstract Method Declared without an implementation. Followed by semicolon; no braces. Any class containing abstract methods must be declared abstract. An abstract method is one prefixed with the keyword abstract. DEFINITION abstract void moveTo(double deltaX, double deltaY);

CS-2852 Data Structures, Andrew J. Wozniewicz Why Abstract Classes?

CS-2852 Data Structures, Andrew J. Wozniewicz Why Abstract Classes? Can be subclassed (extended; inherited from). Forms a contract; a promise to deliver certain functionality. Subclasses are obligated to override methods that are marked as abstract. Allows for (partial) sharing of implementations.

CS-2852 Data Structures, Andrew J. Wozniewicz Interface Empty method bodies All interface methods are implicitly public and abstract Extensible, just like classes Multiple inheritance is supported A named collection of method declarations (without implementations). DEFINITION

CS-2852 Data Structures, Andrew J. Wozniewicz Example of an Interface public interface Comparable { boolean less(Object m); boolean greater(Object m); boolean lessEqual(Object m); boolean greaterEqual(Object m); }

CS-2852 Data Structures, Andrew J. Wozniewicz Implementing an Interface public class MyObject implements Comparable { boolean less(Object m) {...}; boolean greater(Object m) {...}; boolean lessEqual(Object m) {...}; boolean greaterEqual(Object m) {...}; }

CS-2852 Data Structures, Andrew J. Wozniewicz Using an Interface MyObject myObject = new MyObject(); if (myObject.less(x))...; Comparable c = null; c = myObject; if (c.less(x))...;

CS-2852 Data Structures, Andrew J. Wozniewicz Why Interfaces?

CS-2852 Data Structures, Andrew J. Wozniewicz Why Interfaces? A development contract. It ensures that a particular object satisfies a given set of methods. Coding to interfaces is a technique by which developers can expose certain methods of an object to other objects in the system. Ability to code to the interface in place of coding to the object itself.

CS-2852 Data Structures, Andrew J. Wozniewicz Advantages of Interfaces Design: the methods of an object can be quickly specified and published to all affected developers Development: the Java compiler guarantees that all methods of the interface are implemented with the correct signature and that all changes to the interface are immediately visible to other developers Integration: there is the ability to quickly connect classes or subsystems together, due to their well-established interfaces Testing: interfaces help isolate bugs because they limit the scope of a possible logic error to a given subset of methods.

CS-2852 Data Structures, Andrew J. Wozniewicz Interface Caveats Additional typing of code. Some (minimal) run-time overhead due to required code infrastructure. This overhead is insignificant when compared to the ease and benefit of using interfaces. USE THEM!

CS-2852 Data Structures, Andrew J. Wozniewicz Interfaces versus Abstract Classes Classes represent attributes (data, variables, fields) and capabilities/responsibil ities (operations, methods, functions) Single inheritance (is-a) Interfaces are only about capabilities (operations). Multiple inheritance for classes.

CS-2852 Data Structures, Andrew J. Wozniewicz Interfaces vs. Classes II You can implement many interfaces, but be only one class.

CS-2852 Data Structures, Andrew J. Wozniewicz When to use Interfaces? A strong sign that you need to introduce an interface is that you have very similar code in separate classes and you can't re-arrange the classes to inherit this behaviour from a common superclass. USE THEM!!!

CS-2852 Data Structures, Andrew J. Wozniewicz Generics A concept/mechanism that applies to type declarations and methods, and allows for type parameters. DEFINITION An invocation of a generic type is generally known as a parameterized type. Interfaces, methods, and ctors may be generic.

CS-2852 Data Structures, Andrew J. Wozniewicz Defining a Generic Class and Methods public class MyGenClass { public MyGenClass () {... } public void MyGenClass (T t) { // }

CS-2852 Data Structures, Andrew J. Wozniewicz Collection Interface Represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. No direct implementation in JDK. Inherits from Iterable. public interface Collection extends Iterable

CS-2852 Data Structures, Andrew J. Wozniewicz

The Collection Interface public interface Collection extends Iterable { // Basic operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator iterator(); // Bulk operations boolean containsAll(Collection c); boolean addAll(Collection c); //optional boolean removeAll(Collection c); //optional boolean retainAll(Collection c); //optional void clear(); //optional // Array operations Object[] toArray(); T[] toArray(T[] a); } UnsupportedOperationException

CS-2852 Data Structures, Andrew J. Wozniewicz List (“Sequence”) Interface public interface List extends Collection { // Positional access E get(int index); E set(int index, E element); //optional boolean add(E element); //optional void add(int index, E element); //optional E remove(int index); //optional boolean addAll(int index, Collection c); //optional // Search int indexOf(Object o); int lastIndexOf(Object o); // Iteration ListIterator listIterator(); ListIterator listIterator(int index); // Range-view List subList(int from, int to); }

CS-2852 Data Structures, Andrew J. Wozniewicz Bounded & Wildcard Type Parameters

CS-2852 Data Structures, Andrew J. Wozniewicz Bounded & Wildcard Type Parameters All classes, interfaces, and enum types can be used as type parameter bound, including nested and inner types. Neither primitive types nor array types be used as type parameter bound. BOUND WILDCARD

CS-2852 Data Structures, Andrew J. Wozniewicz Summary ATDs – Purely theoretical (math). Interfaces – A named set of methods. Abstract Classes – Partially implemented. Generics – Parametrized types/methods for safety/robustness, readability. Collection and List interfaces

Questions? Image copyright © 2010 andyjphoto.com