Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.

Slides:



Advertisements
Similar presentations
Classes And Objects II. Recall the LightSwitch Class class LightSwitch { boolean on = true; boolean isOn() { return on; } void switch() { on = !on; }
Advertisements

AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Java Review Interface, Casting, Generics, Iterator.
Computer Science 209 Software Development Equality and Comparisons.
1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Generic programming in Java
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Chapter 3 Collection Classes Anshuman Razdan Department of Engineering
CS102 Data Types in Java CS 102 Java’s Central Casting.
Java Syntax Primitive data types Operators Control statements.
Abstract Classes.
Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Information and Computer Sciences University of Hawaii, Manoa
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
Lists What to do?. Lists  A list is a linear arrangement of data elements.  Items are arranged in sequential (linear) order  Items are therefore ordered.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Java Type System and Object Model Horstmann ch , 7.7.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Chapter 7 Queues Introduction Queue applications Implementations.
Introduction to C# By: Abir Ghattas Michel Barakat.
100e Hosted by vhs © Don Link, Indian Creek School, 2004 Jeopardy.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Sixth Lecture ArrayList Abstract Class and Interface
Java Generics.
Unit-2 Objects and Classes
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Java Programming Language
Java’s Central Casting
Outline Creating Objects The String Class The Random and Math Classes
Presentation transcript:

Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators

Ch. 5 Java Objects and Iterators  Java’s Object type  Wrapper classes for primitive data types  Objects collection is actually references collection  Node object that contains object data  Iterator

Need for Java’s Object type and generics Bag Sequence Stack  ADTs and data structures where essentially the same design can be used for data of different types (or even mixed types)  for each type  re-write essentially the same code  minimal modification with respect to the type for this code

Java’s Object type  Java’s Object class type provides a possibility for writing one code and using it for any type (or all types

Object class and inheritance  Every class in Java is directly or indirectly (if the class extends another class) derived from the Object class,, i.e., extending the Object class. So, every object is an object of Object class String s=new String(“Something”); Object obj; obj = s;

Class type conversion  If an object is being treated as an object of Object class, using this object reference we can not access the methods and member variables defined in its original class.  The reference needs to be converted to the original type by casting String s=new String(“Something”); Object obj; obj = s; s= (String) obj;

Conversion of object reference  Widening conversion example: String str = new String (“Hello, World!”); String str = new String (“Hello, World!”); Object obj = str; Object obj = str; int stringLength = str.length( ); int stringLength = str.length( ); stringLength = obj.length( ); stringLength = obj.length( );  Narrowing conversion example: String string = (String) obj; String string = (String) obj; stringLength = string.length( ); stringLength = string.length( );

Java 1.5 Autoboxing  Java has 8 primitive (non-object) types of data –integers: int, long, short, byte –fractionals: double, float –logicals: boolean –characters: char

Wrapper Classes  There are 8 classes in Java specifically for making an object out of a value of any of the primitive data type, and providing additional processing tools:  Integer Float  Long Boolean  Short Character  ByteDouble

Boxing and Unboxing Autoboxing conversion – primitive value converted to a wrapper object of corresponding type Unboxing – wrapper object is converted to corresponding primitve

Autoboxing and Unboxing //boxing int i = 42; int j; Integer example; Example = new Integer(i); j = example.intValue(); //autoboxing example=i; //autoboxing j=example; //autounboxing

Objects example  Really collection of references to objects  class ArrayBag  object is a bag of objects of any type  implemented on an array of Object type  Object [ ] data;

Java Objects Java Objects  A collection of objects is actually a collection of references to objects  Each element of the data array is a reference to an object  Some of them may refer to the same object:

Same Object reference example  add the same reference into the bag several times  each time a copy of the reference is stored into a different position of the array  all are references to the same object, no new copy of the actual object has ever been made

object equality  == and != tests for objects  only test if two references refer to the same object  not if they have the same content  two distinct objects can have the same content.  provide test of equality of contents, write an equals method in which the object contents are compared

null value  legal value for any class type  most primitive data types have nothing similar.  code with objects needs to deal with it  source of NullPointer-Exception  frustration, or error in answers

Node object that contain object data  A node object can be used in a linked list  Instance variables of the IntNode class private int data; private int data; private IntNode link; private IntNode link;  Instance variables of the general Node class private Object data; private Object data; private IntNode link; private IntNode link;

Generic method vs Object Method static Object middle(Object[]data) { if (data.length ==) if (data.length ==) {return null;} {return null;} else else {return {return data[data.length];} data[data.length];}} static T middle(T []data) { if (data.length ==) {return null;} else {return data[data.length];} }

Generic method restrictions  Data type inferred must always be a class type (not a primitive)  May not call a constructor for the generic type  Nor may you create a new array of element of that type

GENERIC CLASSES public class ArrayBag implements Cloneable { private E[] data; private int manyItems; private int manyItems;} ArrayBag sbag= new ArrayBag ();

Generic Class Restrictions  Type used to instantiate must be a class [not primitive]  Within class can’t call constructor for generic type (nor make array of elements of that type)

Array Bag  A generic bag class  A silly program to use it

Generic Node public class IntNode { private int data; private int data; IntNode linke IntNode linke} public class Node { private E data; Node link }

Equals public static Node listSearch(Node head, E target) public static Node listSearch(Node head, E target) { Node cursor; Node cursor; if (target == null) if (target == null) { // Search for a node in which the data is the null reference. { // Search for a node in which the data is the null reference. for (cursor = head; cursor != null; cursor = cursor.link) for (cursor = head; cursor != null; cursor = cursor.link) if (cursor.data == null) if (cursor.data == null) return cursor; return cursor; } else else { // Search for a node that contains the non-null target. { // Search for a node that contains the non-null target. for (cursor = head; cursor != null; cursor = cursor.link) for (cursor = head; cursor != null; cursor = cursor.link) if (target.equals(cursor.data)) if (target.equals(cursor.data)) return cursor; return cursor; } return null; return null; }

Iterator and Enumeration

Iterator Code  A class that implements the generic Iterator must provide these three methods public boolean hasNext ( ); public boolean hasNext ( ); public E next ( ); public E next ( ); public void remove ( ); public void remove ( );

Interface vs. Class ` `interface methods are un-implemented originally ` ` objects of an interface cannot be created, until some class implements this interface and its methods ` `then an object of this class can be used as an object of this interface

Interface  common standard shared between the user/invoker of Java programs and the program designer  documentation of an interface specifies clearly the properties of an object of this interface  whatever class the object belongs user can use it confident of those properties