OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.

Slides:



Advertisements
Similar presentations
Review Generics and the ArrayList Class
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
The ArrayList Class and the enum Keyword
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
Chapter 81 JavaBeans JavaServer Pages By Xue Bai.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
25-Jun-15 Vectors. 2 Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: Arrays have special syntax; Vector.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Fundamental Programming Structures in Java: Strings.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Vectors l Linked Data Structures Dynamic Data Structures.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Java 2 More Java Then Starbucks ;-). Important Terms Primitive Data – Basic, built-in values (characters & numbers) Data Type – Used to talk about values.
Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Lists and the Collection Interface Review inheritance & collections.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Genericity Ranga Rodrigo Based on Mark Priestley's Lectures.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
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.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Chapter 7: Characters, Strings, and the StringBuilder.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
MCQ Which is the correct syntax for placing the string "boat" into an ArrayList name recVehicles in position 3 (index 2) for the first time? a)recVehicles.set(3,
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
CSE 1201 Object Oriented Programming ArrayList 1.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
The ArrayList Data Structure Standard Arrays at High Speed!
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
Sixth Lecture ArrayList Abstract Class and Interface
Array, Strings and Vectors
Unit-2 Objects and Classes
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Arrays & Strings.
Can store many of the same kind of data together
Arrays and Collections
Object Oriented Programming in java
Grouped Data Arrays, and Array Lists.
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
ArrayLists 22-Feb-19.
Review: libraries and packages
Presentation transcript:

OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword

OO Concepts Casting: Casting can be used to convert one primitive data type to another. For example: double x = 3.45; float y = (float)x; The above example converts a double value to a float value and assigns it to the float variable named y. It is also possible to Cast Objects from one type to another.

OO Concepts Casting Objects: The relationship between the Class Object and the Class String in an inheritance hierarchy is as follows: Object String

OO Concepts Casting Objects: Because the String Class and the Object Class are in the same inheritance hierarchy a String is-a type of Object. Because of this relationship, Java allows us to declare a String as follows: Object a = new String(“Hello”); The object being referenced by ‘a’ is a String, but its type has been downplayed.

OO Concepts Casting Objects: The String has been downplayed and so we cannot use the String in its full capacity. The following example will fail: Object a = new String(“Hello”); int theLength = a.length(); Length is a method specific to the String Class. It cannot be used because the String has been downplayed to an Object type.

Casting Objects Casting Objects: If we wish to use the object reference ‘a’ to its full capacity, we must cast the object back to its specific type. Object a = new String(“Hello”); //Cast the a object reference a back to a String String b = (String)a; int theLength = b.length(); It is important to understand that object casts can only be be done within an inheritance hierarchy.

Casting Objects The inheritance hierarchy for String: java.lang.Object | +--java.lang.String The inheritance hierarchy for Integer: java.lang.Object | +--java.lang.Number | +--java.lang.Integer Because Integer and String are not in the same hierarchy we cannot cast a String to an Integer or vice versa.

OO Concepts Casting Objects: The Java language allows us to downplay objects to make the language more flexible. For example we can declare an Array as type Object and store any kind of an object in the array: Object[ ] a = new Object[2]; a[0] = new String(“Hello”); a[1] = new Integer(10);

OO Concepts Casting Objects: We can create methods that accept arguments of type Object. Methods that accept Objects can accept any type of Object such as Strings or Integers. String a = new String(“Hello”); Integer b = new Integer(10); Public void add(Object aValue) { //Method body }

OO Concepts Vectors: The Vector Class is a commonly used Class from the Core Java API. A Vector is an indexed list of object, much like an array. You use Vectors when you need greater flexibility than arrays provide. It can be found in the java.util package. The main advantage is that a Vector can grow and shrink in size as required, but an array has a fixed size.

OO Concepts Vectors: Once that array is created, it has a set size, and additional elements cannot be added. You may think of a vector as a dynamic array that automatically expands as more elements are added to it. Vectors can only accommodate objects. Primitives are not allowed. If you wish to store individual numbers in a Vector you will have to wrap them in their associated wrapper Class first.

OO Concepts Vectors: All vectors are created with some initial capacity. When space is needed to accommodate more elements, the capacity is automatically increased. Like arrays, elements in a vector have an associated index. For these reasons vectors are commonly used in Java programming.

OO Concepts Vectors: Vector a = new Vector(); A Vector object has been created with an initial capacity of ten and is being referenced by the variable a. Add a String object to the Vector String b = new String(“Hello”); a.addElement(Object obj); “Hello” Index: 0

OO Concepts Vectors: String d = new String(“Good Day”); a.addElement(d); To Use an Object stored in a Vector use the following syntax: String e = (String)a.elementAt(0); A cast is required to use the String in its full capacity. Remember, all objects are downplayed when placed in a Vector. Hello Good Day Index: 0 1

OO Concepts Vectors & Casting: Casts are commonly used with vectors. When retrieving a value from a vector, its type is only known as the generic type Object Because of this, you must use a cast to cast it back to the type of the object that you inserted into the vector.

Vectors The following are some important methods we will be using with the vector Class: Method:Description Vector()A constructor commonly used to create a vector Object with an initial capacity of 10 elements. addElement(Object obj)Adds an object to the vector. insertElementAt(ObjectAdds obj to the vector at index obj, int index) elementAt(int index)Returns the element at the specific index