CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Introduction to Computation and Problem Solving Class 32: The Java® Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
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.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Frameworks and Collections Horstmann ch QUIZ What is the difference between a program library and a framework? 1.There is no difference 2.When.
Working With Collections in the AP ™ Java Subset 2003 ACTE Convention © 2003 by Kenneth A. Lambert.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
15-Jun-15 Lists in Java Part of the Collections Framework.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
05 - Containers DRAFT COPY © S. Uchitel, 2004 Container OrderedDuplicates BagsNOYES SetsNONO ListsYESYES MapsNO.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
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.
Java's Collection Framework
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.
CS2110: SW Development Methods
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
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.
The Java Collections Framework (JCF) Introduction and review 1.
Data structures and algorithms in the collection framework 1 Part 2.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Computer Science 209 Software Development Java 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.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
The Java Collections Framework Based on
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
Chapter 8 (Horstmann’s Book) Frameworks Hwajung Lee.
Chapter 8 Frameworks. Frameworks Framework is a set of cooperating classes and interface types that structures the essential mechanisms of a particular.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Java Programming: From the Ground Up Chapter 17 The Java Collections Framework.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Introduction to Collections
Introduction to Collections
Introduction to Collections
Collections Framework
Introduction to Collections
Part of the Collections Framework
ITEC324 Principle of CS III
Java Generics & Iterators
CS 151: Object-Oriented Design October 8 Class Meeting
Presentation transcript:

CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 2 Frameworks  A framework consists of a set of cooperating classes. These classes implement the essential mechanisms for a particular problem domain. Example: Swing is a framework for what problem domain?  GUI programming  A framework is not a design pattern. Not a general design rule. A framework can use multiple design patterns. _

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 3 Application Framework  A set of classes that implement services that are common to a particular type of application. Examples: web application, enterprise application, database application, banking application  An application programmer builds an application by: Subclassing framework classes. Adding new classes that provide custom functionality.  Inversion of control The framework classes control the execution flow, not the application-specific classes. _

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 4 Applet Framework  An applet is a Java program that runs inside a web browser. Runs as a client-side application using the browser’s JVM. Its GUI is embedded in a web page. Its.class and.jar files are downloaded from a server along with the web page. _

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 5 Applet Class  An applet application extends the Applet class.  Methods init() : Called only once when the applet is first loaded. Initialize data structures and GUI elements. start() : Called when the applet is first loaded and whenever the user restores the browser window containing the applet. Start or restart animations or other computationally intensive tasks. stop() : Called whenever the user leaves the browser window containing the applet or the browser terminates. Stop computationally intensive tasks. destroy() : Called when the browser terminates. Relinquish resources obtained by init(). paint() : Called whenever the applet window needs repainting to update the view.

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 6 Applet Class, cont’d  The applet framework takes care of interacting with the browser, parsing parameters, determining when the applet is visible, etc. The application programmer only has to provide the classes that implement custom behavior.  Inversion of control means that the framework takes care of control flow. The application programmer only has to provide the functionality to initialize, start, stop, destroy, and paint, _ Applet demo

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 7 Applet Class, cont’d  On the web page that contains the applet: <applet code="numbercruncher.program13_3.PiBorweinApplet.class" codebase="./" align="middle" width="600" height="400" archive="PiBorwein.jar" name="PiBorweinApplet">

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 8 The Java Collections Framework  A set of classes that implement useful data structures: linked lists hash tables sets trees  Also useful algorithms coded as static methods: Collections.binarySearch() Collections.sort() Collections.reverseOrder() Collections.swap()  And more! It’s a framework that make it easy for you to add new classes that work well with existing collections.

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 9 Collections Framework Interfaces  Collection The most general collection interface type.  Set An unordered collection that does not permit duplicate elements.  SortedSet A set whose elements are visited in sorted order.  List An ordered collection. _

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 10 Collections Framework Classes  HashSet A set implementation that uses hashing to locate the set elements.  TreeSet A sorted set implementation that stores the elements in a balanced binary tree.  LinkedList and ArrayList Two implementations of the List interface type.

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 11 Collections Framework Class Diagram From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 12 All Collection Classes are Generic Types  The type parameter denotes the type of the objects in the collection. Example: ArrayList = new ArrayList ();

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 13 The Collection Interface Type  A class that implements interface Collection holds objects in some way. Different collection classes may have different ways to store and access the objects in their collections. collection class = data structure  The interface declares many methods: boolean add(E obj) boolean addAll(Collection c) void clear() boolean contains(E obj) boolean containsAll(Collection c) boolean equals(E obj) int hashCode() boolean isEmpty() Iterator iterator() boolean remove(E obj) boolean removeAll(Collection c) boolean retainAll(Collection c) int size() E[] toArray() E[] toArray(E[] a) Don’t confuse interface Collection with class Collections.

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 14 The AbstractCollection Class  Because the Collection interface has so many methods, it would be a pain to implement.  Therefore, Java helpfully provides the AbstractCollection class that supplies a default implementation for each method. Except for size() and iterator(). Example: public E[] toArray() { E[] result = new E[size()]; Iterator e = iterator(); for (int i = 0; e.hasNext(); i++) { result[i] = e.next(); } return result; }

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 15 The Iterator Interface  We recall the Iterator interface:  Interface Collection extends interface Iterable which has only one method:  Any class that implements interface Iterable can be used in a “for each” loop. Therefore, all collections can. Example: boolean hasNext() E next() void remove() Iterator iterator() for (Employee e : employeeList) {... } Don’t confuse Iterator and Iterable.

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 16 Add a New Collection Class  Add a new BoundedQueue class to the collections framework. The new class extends AbstractCollection It will work well with the existing classes of the framework. From: Object-Oriented Design & Patterns, John Wiley & Sons, Demo Bounded

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 17 The Set Interface Type  Interface Set adds no new methods! But it’s not a marker interface. (Why not?)  By definition, a set has these restrictions: Set collections don’t store duplicate objects. Set collections are unordered.  Having a separate Set interface allows a method to require a set object with its restrictions rather than another type of collection object. _ public interface Set extends Collection {}

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 18 The List Interface Type  A list is an ordered collection of objects, each of which is accessed with an integer index.  Interface List extends interface Collection and adds these methods: boolean add(int index, E obj) boolean addAll(int index, Collection c) E get(int index) int indexOf(E obj) int lastIndexOf(E obj) ListIterator listIterator() ListIterator listIterator(int index) E remove(int index) E set(int index, int E) List subList(int fromIndex, int toIndex)

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 19 The ListIterator Interface Type  Interface ListIterator extends interface Iterator and adds these methods: int nextIndex() int previousIndex() boolean hasPrevious() E previous() void add(E obj) void set(E obj)

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 20 List Class Diagram From: Object-Oriented Design & Patterns, John Wiley & Sons, Hey, what’s this for?

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 21 Lists and Binary Searches  Does it make sense to perform a binary search on an ArrayList object? a LinkedList object?  You can access each element of an array list directly using an index. It takes the same amount of time no matter which element. Therefore, it makes perfectly good sense to perform a binary search on an ArrayList object.  You can access an element of a linked list only by “chasing pointers” from the head of the list down to the element you want. Different elements have different access times depending on how far away they are from the head. Therefore, it does not make sense to perform a binary search on a LinkedList object.

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 22 The RandomAccess Interface  RandomAccess is a marker interface.  Class ArrayList implements RandomAccess, but not LinkedList. Therefore, before doing a binary search, you can test for RandomAccess : if (list instanceof RandomAccess) { // do a binary search } else { // do a linear search }

SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 23 Views  A view is a collection object. Its class implements one of the interfaces of the collections framework. It permits only restricted access to the collection.  A view allows you to access the objects in the collection in a certain way. You can limit the view (restricted access). Or you can enhance the collection. Example: A list has a richer interface than a plain old array! String[] strings = {"Kenya", "Thailand", "Portugal"}; List view = Arrays.asList(strings); anotherCollection.addAll(view); Don’t get confused with the view in model-view-controller.