Generic Programming David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Java Review Interface, Casting, Generics, Iterator.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Generic programming in Java
Java Generics.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Review Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Review What have we done during the course? Where to learn more? What is for the.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Exceptions David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 The Role of Exceptions Definition: a method succeeds if it terminates in.
Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives.
Generic Programming David Rabinowitz. June 14, 2006 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
Generics. Type parameters The definition of “ArrayApplier” in applier.java and applier2.java allows any function from int to int. But suppose you want.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
The Java Collections Package C. DeJong Fall 2001.
Chapter 19 Java Data Structures
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Java Generics.
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
1-1 Generic Types in Java Format for a generic (parameterized) type and instantiation of a generic type.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Effective Java: Generics Last Updated: Spring 2009.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Chapter 18 Java Collections Framework
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
Data structures Abstract data types Java classes for Data structures and ADTs.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
G ENERICS I N J AVA BY: Ankit Goyal Sankalp Singh.
Practical Session 4 Java Collections. Outline Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack The.
1 Interfaces in Java’s Collection Framework Rick Mercer.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CMSC 330: Organization of Programming Languages Java Generics.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
CPS What's in Compsci 100? l Understanding tradeoffs: reasoning, analyzing, describing…  Algorithms  Data Structures  Programming  Design l.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
FINAL REVIEW. Stronger vs Weaker (one more time!) Requires more? Promises more? (stricter specifications on what the effects entail) weaker stronger.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
CSC 222: Object-Oriented Programming
Sixth Lecture ArrayList Abstract Class and Interface
Generic Programming David Rabinowitz.
Chapter 19 Java Data Structures
Introducing Java Generics and Collections
Generic Programming David Rabinowitz.
Java Generics.
structures and their relationships." - Linus Torvalds
Generic programming in Java
Object Oriented Programming in java
CS2011 Introduction to Programming I Methods (II)
structures and their relationships." - Linus Torvalds
Presentation transcript:

Generic Programming David Rabinowitz

March 3rd, 2004 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack receives only Object s The problem: We need different stacks for int, String and Method. We don’t want the problems of: Stack.add(“clearly not a method”); … Method m = (Method)stack.pop();

March 3rd, 2004 Object Oriented Design Course 3 Solution (?) Let’s create IntStack, StringStack and MethodStack. All of them will rely on the original Stack as internal implementation. Are there any problems? A lot of code duplication It’s hard to add new types

March 3rd, 2004 Object Oriented Design Course 4 Another problem We want to use a swap function between two int s. void swap(int& a, int&b) { int temp = a; a = b; b = temp; } What about swap(double&, double&) and swap(Method&, Method&) ?

March 3rd, 2004 Object Oriented Design Course 5 The actual solution Generic programming. Write the code once, and worry about the type at compile time The code is suitable to all types Easy to add types later No code duplication Demands from types

March 3rd, 2004 Object Oriented Design Course 6 So how do we do it? swap (T& a, T& b) { T temp = a; a = b; b = temp; } Looks simple?

March 3rd, 2004 Object Oriented Design Course 7 Uses Containers list set vector map Algorithms sort search copy

March 3rd, 2004 Object Oriented Design Course 8 C++ Templates The most known use of generic programming STL – Standard Template Library Containers vector, set, hash_map Algorithms for_each, swap, binary_search, min, max

March 3rd, 2004 Object Oriented Design Course 9 What about Java? Until now Java had large collection set Set, List, Map, Iterator, and more sort(), search(), fill(), copy(), max(), min() One major problem – the collections are not type safe No problem to do Map.put(“key”, “4”); Integer i = (Integer)map.get(“key”);

March 3rd, 2004 Object Oriented Design Course 10 Java Generics JSR 14 Added as one of the new features of Java 1.5 (“Tiger”) Done in the compiler only Converts String s = vector.get(3) to String s = (String)vector.get(3)

March 3rd, 2004 Object Oriented Design Course 11 How to use generics ? List myIntList = new LinkedList (); myIntList.add(new Integer(0)); Integer x = myIntList.iterator().next();

March 3rd, 2004 Object Oriented Design Course 12 And what about the collection ? public interface List { void add(E x); Iterator iterator(); } public interface Iterator { E next(); boolean hasNext(); }

March 3rd, 2004 Object Oriented Design Course 13 Subtyping List ls = new ArrayList (); List lo = ls; lo.add(new Object()); // attempts to assign an Object // to a String! String s = ls.get(0);

March 3rd, 2004 Object Oriented Design Course 14 Subtyping (cont.) Foo is subtype of Bar Foo extends Bar Foo implements Bar C is a generic container C Results that C is not subtype of C

March 3rd, 2004 Object Oriented Design Course 15 Generic Algorithms (1) How to print entire Collection? Do we have to use Collection ? Use wildcard void printCollection(Collection c){ for (Object e : c) { System.out.println(e); }

March 3rd, 2004 Object Oriented Design Course 16 Generic Algorithms (2) What happens when we want to use specific method? public void drawAll(List shapes) { for (Shape s: shapes) { s.draw(this); } What about subtyping? List

March 3rd, 2004 Object Oriented Design Course 17 Generic Algorithms (3) The solution public void drawAll(List shapes) {…}

March 3rd, 2004 Object Oriented Design Course 18 More About Wildcards Collection c = new ArrayList (); c.add(new Object()); public void addRectangle(List shapes) { shapes.add(0, new Rectangle()); }

March 3rd, 2004 Object Oriented Design Course 19 This is no all! Java Generics are one of the important language features of Java 1.5 More information in generics-tutorial.pdf Yep, it is required …

March 3rd, 2004 Object Oriented Design Course 20 C# Generics Are going to be added to.NET 2.0 Very similar to Java public struct Point { public T X; public T Y; } Point point; point.X = 1; point.Y = 2; See for more information

March 3rd, 2004 Object Oriented Design Course 21 Summary Software Correctness & Fault Tolerance Design by Contract When is a class correct? Speed, Testing, Reliability, Documentation, Reusability, Improving Prog. Languages Exceptions What happens when the contract is broken? Neutrality, Weak Safety, Strong Safety