1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.

Slides:



Advertisements
Similar presentations
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Advertisements

Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Java Review Interface, Casting, Generics, Iterator.
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
Generic programming in Java
Inheritance Inheritance Reserved word protected Reserved word super
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Java Generics.
Chapter 3 Collections. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Chapter Objectives Define the concept and terminology related.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
CHAPTER 3 COLLECTIONS SET Collection. 2 Abstract Data Types A data type consists of a set of values or elements, called its domain, and a set of operators.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
Lecture 9 Concepts of Programming Languages
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
OOP Languages: Java vs C++
Topic 3 The Stack ADT.
Chapter 3 Introduction to Collections – Stacks Modified
Java Generics.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS 2430 Day 26. Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via .
Effective Java: Generics Last Updated: Spring 2009.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
G ENERICS I N J AVA BY: Ankit Goyal Sankalp Singh.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Introduction to Generics
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
CS 2430 Day 24. Announcements Quiz this Friday Program 5 posted on Monday Program 4 due date: Friday at 10pm Program 4 grace date: Wednesday at 10pm (don’t.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
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:
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
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.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Click to edit Master text styles Stacks Data Structure.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
ADT’s, Collections/Generics and Iterators
More on Java Generics Multiple Generic Types Bounded Generic Types
Chapter 3: Using Methods, Classes, and Objects
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Java Software Structures: John Lewis & Joseph Chase
ArraySet Methods and ArrayIterator
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Example: LinkedSet<T>
Presentation transcript:

1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet class ArraySet Underlying Data Structure Reading: L&C 3 rd : nd 3.2,3.4,3.5

2 Java Generics In Java 5.0, Sun added a generic feature that had been available in other languages such as C++ (where it is called “Templates”) Although the implementation is different, the user gains the benefit of stronger compiler type checking and simpler coding style (with a reduced number of explicit casts needed)

3 Parameterized Classes Defining a parameterized class: public class Generic { // use T in attribute declarations private T whatIsThis; // use T as a method’s parameter type public void doThis(T input) { … } // use T as a method’s return type public T doThat( … ) { return new T( … ); }

4 Parameterized Classes Instantiating a parameterized class Generic g = new Generic (); Use methods with objects of the actual type g.doThis(“Hello”); String s = g.doThat( … ); No casting of data types should be required Note: Error in text on pgs missing () (second edition page:70) Box box1 = new Box (); Box box2 = new Box ();

5 Parameterized Classes Note: Primitive types can not be used as a generic type: Generic g =... // OK Generic g =... // Compile error Solution ^^

6 Parameterized Classes Must use a known class - not dummy letters Generic g = new Generic (); // error Unless in a generic class where T is defined public class AnotherGenericClass { … Generic g = new Generic (); // OK … }

7 Parameterized Classes Don’t omit an identified in new code Generic g = new Generic(); // legacy code? Compiler will give incompatible type errors without an explicit cast (narrowing) String s = g.doThat( … ); // error String s = (String) g.doThat( … ); // OK Compiler will give unchecked warnings g.doThis(“Hello”); // warning

8 Parameterized Classes Can’t instantiate arrays of the generic type without using a “trick” T [] t = new T[10]; // compile error T [] t = (T []) new Object[10]; // OK Can’t instantiate arrays of a parameterized class without using a slightly different “trick” ArrayList [] a = (ArrayList []) new ArrayList[10]; –Not a good programming practice.

9 Parameterized Classes Static members can not be generic because there is only one copy of the code for all of the parameterized class objects instantiated with possibly different generic types public class BadClass { private static T count; // error public static T method() // error {…} }

10 Parameterized Classes Don’t invoke static methods of parameterized classes with a generic type specified BadClass b = new BadClass (); // the following is an error int n = BadClass.method(); // the method must be invoked as int n = BadClass.method();

11 Parameterized Methods Individual static methods in a non-parameterized class can be identified as generic public class Test { public static List reverse (List in) { // use the generic type T as usual List out = new ArrayList ();... return out; }

12 Parameterized Methods To invoke a parameterized method you use the parameterized types but still omit generic type indicator on the method call: ArrayList shelf = new ArrayList ();... List reverse = Test.reverse(shelf);

13 Set ADT Set Definition Operations Our Implementation (Using Array) Reading 2 nd :3.2,3.4,3.5 – 3 rd : 15.1,15.3 Making some changes to the books code –We are going to use the SetADT interface. We change the SetADT interface to extends the Iterable interface.

14 Interface SetADT SetADT is an interface defining the operations of the Set ADT: void add (T element) T removeRandom( ) T remove (T element) SetADT union(SetADT set) boolean contains(T target) boolean equals(SetADT set) boolean isEmpty() int size() Iterator iterator() String toString()

15 ArraySet Class The ArraySet class needs to implement the Set interface with a generic type class ArraySet implements SetADT It uses an underlying array as a data structure (as its name specifies) It needs to implement code for all the methods defined in the SetADT and Iterable interfaces.

16 ArraySet Underlying Data Structure private static Random rand = new Random(); private static final int DEFAULT_CAPACITY = 100; private int count; private T[ ] contents; contents[0]contents[1]contents[2]contents[3]contents[4]contents[5] Elements are kept contiguous null