More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Object Oriented Programming with Java
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
The Java Collections Package C. DeJong Fall 2001.
Georgia Institute of Technology Making Text for the Web part 4 Barb Ericson Georgia Institute of Technology March 2006.
Chapter 19 Java Data Structures
Java Collections Framework A presentation by Eric Fabricant.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
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.
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,
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
(c) University of Washington14-1 CSC 143 Java Collections.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Effective Java: Generics Last Updated: Spring 2009.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
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.
Collections. The Plan ● Why use collections? ● What collections are available? ● How are the collections different? ● Examples ● Practice.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Maps Nick Mouriski.
Iteration Abstraction SWE Software Construction Fall 2009.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Building Java Programs Generics, hashing reading: 18.1.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 19 Java Data Structures
University of Central Florida COP 3330 Object Oriented Programming
Java Collections Overview
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
CSE 1020: The Collection Framework
Presentation transcript:

More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300

Abstract class and interface  Abstract class: Declaration omitting implementation Abstract class  Interface: a "contract" that spells out how software interacts. Interface  Example: sort contacts on your phone based on e- mail addresses CS300 2

Java vs C#: Classes / Interfaces 3 JavaC# Accessibility keywords public private protected static // Inheritance class FootballGame extends Competition {... } Accessibility keywords public private internal protected protected internal static // Inheritance class FootballGame : Competition {... } CS300

Java vs C#: Classes / Interfaces 4 JavaC# // Interface definition interface IAlarmClock {... } // Extending an interface interface IAlarmClock extends IClock {... } // Interface implementation class WristWatch implements IAlarmClock, ITimer {... } // Interface definition interface IAlarmClock {... } // Extending an interface interface IAlarmClock : IClock {... } // Interface implementation class WristWatch : IAlarmClock, ITimer {... } CS300

Static and Final  Final declaration cannot be changed  Classes, methods, fields, parameters and local variables can be final  Static: belongs to the class in which it is described, not an instance of a class  Example: Download QuietStatic.java and LoudStatic.java Create StaticClient for QuietStatic where you increment class member and instance member Fill in LoudStatic CS300 5

Exceptions  Handle unusual conditions CS300 6

Java vs C#: Exception Handling 7 JavaC# // Must be in a method that is declared to throw this exception Exception ex = new Exception("Something is really wrong."); throw ex; try { y = 0; x = 10 / y; } catch (Exception ex) { System.out.println(ex.getMessage()); } finally { // Code that always gets executed } Exception up = new Exception("Something is really wrong."); throw up; // ha ha try { y = 0; x = 10 / y; } catch (Exception ex) { // Variable "ex" is optional Console.WriteLine(ex.Message); } finally { // Code that always gets executed } CS300

Collection framework  One of the most powerful and convenient tools of Java  java.util package: ns.html ns.html CS300 8

Collection interface types  Collection: root type. Group of objects, not necessarily ordered, not necessarily addressable, possibly containing duplicates. You can:  Add/remove things from it  Get its size  Iterate over it CS300 9

Collection interface types  List: ordered collection, index from 0 to length -1. May contain duplicates. You can:  Add/remove things from it  Get its size  Iterate over it  Map an element to its index  Map an index to an element  Change an element at a specific index CS300 10

Collection interface types  Set: unordered collection. Does not contain duplicates. You can:  Add/remove things from it  Get its size  Iterate over it  Map: like a list. Instead of mapping a collection of objects to integer indexes, it maps them to a set of key objects  Examples: mapping of words to their definitions, dates to events, or URLs to cached content CS300 11

Collection interface types  What you can do with a Map:  Add/remove things from it  Get its size  Iterate over it  Map an element to its key  Map key to an element  Change an element at a specific key CS300 12

Collection interface types  Iterator: Returns elements from a collection from which it is derived, exactly once, in response to calls to its next method. CS Instead of:We prefer: for(int i = 0; i < list.size(); i++) { String s = list.get(i); //… } for(Iterator i = list.iterator; i.hasNext(); ) { String s = i.next(); //… } Or for(String s : list){ //… }

Collection implementation types  ArrayList: list implemented by an array.  Quick to index  Slow to change size  LinkedList: list that can change size quickly but it is slower to index. CS300 14

Collection implementation types  HashSet: set that is implemented as a hash. add, remove, contains, and size all execute at constant time assuming well behaved hash. May contain no more than one null.  HashMap: implementation of Map that uses hash table as its index. add, remove, contains, and size all execute at constant time assuming well behaved hash. May contain a single null key but any number of values may be null. CS300 15

Collection implementation types  TreeMap: ordered Map, objects in the map are sorted according to their natural order, or according to Comparator. CS300 16

Java Generics Without Generics:With Generics: public List makeList(){ // … } public void useList(List l){ Thing t = (Thing) l.get(0); // … } //… useList(makeList()); // useList has no guarantee that makeList created a list of Thing, compiler cannot verify that the cast in useList will work and the code might explode at runtime public List makeList(){ // … } public void useList(List l){ Thing t = (Thing) l.get(0); // … } //… useList(makeList()); CS300 17

References  Programming Android by Zigurd Mednieks, Laird Dornin, G. Blake Meike, Masumi Nakamura  Exceptions ng/Exception.html CS300 18