Generics 5/11/2019.

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
 2006 Pearson Education, Inc. All rights reserved Generics Many slides modified by Prof. L. Lilien (even many without an explicit message). Slides.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
 2006 Pearson Education, Inc. All rights reserved Generics.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Java Generics.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
 2005 Pearson Education, Inc. All rights reserved Generics.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Object-Oriented Programming Simple Stack Implementation.
CIS 270—Application Development II Chapter 18-Generics.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Generic(Parameterized ) types Mehdi Einali Advanced Programming in Java 1.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
 2003 Prentice Hall, Inc. All rights reserved. 1 Ders Notu 8 - Template İçerik 11.1 Giriş 11.2 Fonksiyon Template ları 11.3 Overloading Fonksiyon Templates.
 Pearson Education, Inc. All rights reserved. 1 Ch 18 Generics OBJECTIVES In this chapter you will learn:  To create generic methods that perform.
Click to edit Master text styles Stacks Data Structure.
Collections (Stack, queue, tree, graph). References as Links References can be used to create a variety of linked structures, such as a linked list.
Sections 3.4 Formal Specification
Chapter 22 - C++ Templates
CSCI 3333 Data Structures Stacks.
Generic(Parameterized ) Types
Chapter 20 Generic Classes and Methods
Chapter 14 Templates C++ How to Program, 8/e
Stacks.
Stack and Queue APURBO DATTA.
Advanced Programming Behnam Hatami Fall 2017.
Stacks.
More Data Structures (Part 1)
Advanced Programming in Java
Stacks.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
ADT list.
Introduction to Programming
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
More Data Structures (Part 1)
template< class T > class Stack { public:
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Generic Programming.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Arrays Arrays are represented by objects but there is no class that array objects are instances of. Variables of array type are declared using bracket.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Generic Classes and Methods
Corresponds with Chapter 5
A type is a collection of values
CHAPTER 3: Collections—Stacks
Presentation transcript:

Generics 5/11/2019

Motivation Let us consider the following program: public static void printArray( Integer[] inputArray ) { // display array elements for ( Integer element : inputArray ) System.out.printf( "%s ", element ); System.out.println(); } // end method printArray public static void printArray( Double[] inputArray ) { // display array elements for ( Double element : inputArray ) System.out.printf( "%s ", element ); System.out.println(); } // end method printArray public static void printArray( Character[] inputArray ) { // display array elements for ( Character element : inputArray ) System.out.printf( "%s ", element ); System.out.println(); } // end method printArray 5/11/2019

Can We Write One Method Yes. Generics is the solution. How Type Parameter: ID specifies a generic type name public static void printArray( inputArray ) { // display array elements for ( E element : inputArray ) System.out.printf( "%s ", element ); System.out.println(); } // end method printArray <E> E[] public static void main( String args[] ) { // create arrays of Integer, Double and Character Integer[] integerArray = { 1, 2, 3, 4, 5, 6 }; Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; Character[] characterArray = { 'H', 'E', 'L', 'L', 'O' }; System.out.println( "Array integerArray contains:" ); printArray( integerArray ); // pass an Integer array System.out.println( "\nArray doubleArray contains:" ); printArray( doubleArray ); // pass a Double array System.out.println( "\nArray characterArray contains:" ); printArray( characterArray ); // pass a Character array } // end main 5/11/2019

Another Example: find largest number Consider the following method: public static void main( String args[] ) { System.out.printf( "Maximum of %d, %d and %d is %d\n\n", 3, 4, 5, maximum( 3, 4, 5 ) ); System.out.printf( "Maximum of %.1f, %.1f and %.1f is %.1f\n\n", 6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) ); System.out.printf( "Maximum of %s, %s and %s is %s\n", "pear", "apple", "orange", maximum( "pear", "apple", "orange" ) ); } // end main public static T maximum( T x, T y, T z ) { T max = x; // assume x is initially the largest if ( y.compareTo( max ) > 0 ) max = y; // y is the largest so far if ( z.compareTo( max ) > 0 ) max = z; // z is the largest return max; // returns the largest object } // end method maximum < T extends Comparable< T > > 5/11/2019

Generic Class Some Classes such as a Stack can have a specific element type such as Integer, Student etc. public class Stack< E > { private final int size; // number of elements in the stack private int top; // location of the top element private E[] elements; // array that stores stack elements // no-argument constructor creates a stack of the default size public Stack() this( 10 ); // default stack size } // end no-argument Stack constructor // constructor creates a stack of the specified number of elements public Stack( int s ) size = s > 0 ? s : 10; // set size of Stack top = -1; // Stack initially empty elements = ( E[] ) new Object[ size ]; // create array } // end one-argument Stack constructor 5/11/2019

Generic Class (Cont’) // push element onto stack; if successful, return true; // otherwise, throw FullStackException public void push( E pushValue ) { if ( top == size - 1 ) // if stack is full throw new FullStackException( String.format( "Stack is full, cannot push %s", pushValue ) ); elements[ ++top ] = pushValue; // place pushValue on Stack } // end method push // return the top element if not empty; else throw EmptyStackException public E pop() if ( top == -1 ) // if stack is empty throw new EmptyStackException( "Stack is empty, cannot pop" ); return elements[ top-- ]; // remove and return top element of Stack } // end method pop } // end class Stack< E > 5/11/2019

Generic Class (Cont’) public class StackTest { private double[] doubleElements = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 }; private int[] integerElements = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; private Stack< Double > doubleStack; // stack stores Double objects private Stack< Integer > integerStack; // stack stores Integer objects // test Stack objects public void testStacks() doubleStack = new Stack< Double >( 5 ); // Stack of Doubles integerStack = new Stack< Integer >( 10 ); // Stack of Integers testPushDouble(); // push double onto doubleStack testPopDouble(); // pop from doubleStack testPushInteger(); // push int onto intStack testPopInteger(); // pop from intStack } // end method testStacks } 5/11/2019