Date post: | 06-Mar-2016 |
Category: |
Documents |
Upload: | vishal-iyer |
View: | 265 times |
Download: | 1 times |
of 43
OO programming using Java
Education, Training and AssessmentWe enable you to leverage knowledge anytime, anywhere!
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Copyright Guideline 2013 Infosys Limited, Bangalore, India. All Rights Reserved.
Infosys believes the information in this document is accurate as of its publication date; suchinformation is subject to change without notice. Infosys acknowledges the proprietary rights ofother companies to the trademarks, product names and such other intellectual property rightsmentioned in this document. Except as expressly permitted, neither this documentation norany part of it may be reproduced, stored in a retrieval system, or transmitted in any form or byany means, electronic, mechanical, printing, photocopying, recording or otherwise, without theprior permission of Infosys Limited and/ or any named intellectual property rights holdersunder this document.
2
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Confidential Information This Document is confidential to Infosys Limited. This document contains information and data that
Infosys considers confidential and proprietary (Confidential Information).
Confidential Information includes, but is not limited to, the following: Corporate and Infrastructure information about Infosys Infosys project management and quality processes Project experiences provided included as illustrative case studies
Any disclosure of Confidential Information to, or use of it by a third party, will be damaging to Infosys. Ownership of all Infosys Confidential Information, no matter in what media it resides, remains with
Infosys.
Confidential information in this document shall not be disclosed, duplicated or used in whole or in part for any purpose other than reading without specific written permission of an authorized representative of Infosys.
This document also contains third party confidential and proprietary information. Such third party information has been included by Infosys after receiving due written permissions and authorizations from the party/ies. Such third party confidential and proprietary information shall not be disclosed, duplicated or used in whole or in part for any purpose other than reading without specific written permission of an authorized representative of Infosys.
3
3
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Course Information
Course Code: CCFP4.0-OOP
Course Name: OO programming using Java
Document Number: OOP-03
Version Number: V4.0
4
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Recap
Topics covered on OO Fundamentals
SDLC Overview
OO Concepts
Introduction to UML
OO Fundamentals Implementation in Java
Instance Variables
Methods
Access Specifiers
Classes & Objects
Coding Standards
5
OO Fundamentals Implementation in Java
Education, Training and AssessmentWe enable you to leverage knowledge anytime, anywhere!
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
OO Fundamentals Implementation in Java Object Oriented Fundamentals
Java Architecture
Variables in detail
Reference Variables & Objects in Memory
Methods Parameter Passing Techniques, Recursion
this reference
7
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Java Architecture (1 of 2) Java Architecture is composed of the following components:
8
JavaProgrammingLanguage
ObjectOrientation
Programmerfriendlyfeatures
Robust
Platformindependent
JavaByteCode
IntermediatecodegeneratedbytheJavacompiler
AlsocalledJavaclassfile
FacilitatesWriteOnce,RunAnywhere
JavaVirtualMachine
Loadsbytecodeandexecutesthem
Itisplatformdependent
Differentfordifferentplatforms
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Java Architecture (2 of 2)9
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Program Life Cycle10
Compilation & Execution:
ByteCode
Resides in secondary memory
ByteCode
Loaded to main memory (process)
On execution
Output
On execution
Generated on output devices
Inputdevices
Interacts with
Program
Resides in secondary memory
On compilation
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Program Execution and Main Memory During execution of a program, the storage of program and data is as follows:
The executable code is stored into the code /Text segment
The global variables are stored into data segment
The heap memory is used for dynamic memory allocation
The local variables are stored into the stack
11
CodeSegment
Datasegment
Heap
Stack
ExecutableCode
DynamicMemory
LocalVariables
Globalvariables
Note: Data segment and Global variables are not within the scope of the course
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Variables in Detail Local Variables
Variables that are declared inside a method are called local variables
Also referred as automatic, temporary or stack variables
Created when the method in which they are declared is executed
Destroyed when the method in which they are declared completes execution (i.ewhen it loses scope)
Local variables require explicit initialization
12
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Variables in Detail Instance Variables Variables that are used for representing the attributes of a class and declared
inside the class are called instance variables / member variables
Are not bound to a method but belong to the object
Lifetime depends on the lifetime of the object
Whenever an object is created, memory is allocated for the instance variables
Stored in the heap memory along with the object to which they belong
13
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Reference Variables & Objects in Memory
Garbage Collection
Dynamically allocated memory that is no longer needed should be de-allocated
In Java, de-allocation is done by a Garbage Collector
It is a system-level thread to keep track of memory allocations
Functions of Garbage Collector:
Checks for and frees memory no longer needed
Is run automatically by the JVM
14
Guided Activity: OO Fundamentals - Assignment 26
Methods Pass by Value & Pass by Reference
Education, Training and AssessmentWe enable you to leverage knowledge anytime, anywhere!
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Methods Parameter Passing Techniques
Parameter Passing Techniques
Pass-by value & Pass-by reference
16
Demo: OO Fundamentals - Assignment 27
Guided Activity: OO Fundamentals - Assignment 28
Recursive Methods
Education, Training and AssessmentWe enable you to leverage knowledge anytime, anywhere!
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Recursive method A method invoking itself is referred to as Recursion
Typically, when a program employs recursion the function invokes itself with a smaller argument
Computing factorial(5) involves computing factorial(4), computing factorial(4) involves computing factorial(3) and so on
Often results in compact representation of certain types of logic and is used as substitute for iteration
18
Demo : Recursion Assignment 29a
Guided Activity: Recursion - Assignment 29b
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Towers of Hanoi problem19
Problem was discovered by the French mathematician EdouardLucas in 1883.
Used to learn recursion
Rules :
Move only one disk at a time. Rings must be in decreasing size
No move should result in a larger disk on top of a smaller disk
For temporarily holding a disk, the third tower can be used
A B C A B C
Transferthe3disksfromtowerAtotowerB
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Towers of Hanoi - 3 discs
Solution:
20
A B C A B C
1
2
3
4
5
6
7
8
this reference
Education, Training and AssessmentWe enable you to leverage knowledge anytime, anywhere!
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
this reference
Implicit reference to refer the current object, i.e the object which invoked the method
Used to resolve ambiguity between instance variables and local variables when they have the same name, i.e it prevents instance variable hiding
this reference can be used in some cases to improve the readability of a program
22
Demo: OO Fundamentals - Assignment 30
Data Structures
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Data Structures Topics
Data Structures
Types
Linear
Nonlinear
24
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Data Structure Many problems require more complex data types which involve a collection
of primitive data elements (organized in specific arrangement)
Collection of primitive data elements are referred to as complex/non primitive data types
Non primitive data types have logical representation and also physical representation Example: Declaration of an array of integers in a program is a logical
representation. This needs a corresponding representation in memory with contiguous chunks of 4 bytes to hold all elements of the array. However, a programmer normally does not need to focus on this memory representation
A programmer defining a non primitive data structure will also have to define operations on them since the computer only defines operations on primitive types Example: When a programmer declares an array of integers, he should also define
the operations for array elements.
25
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Data Structure26
DataStructures
Homogenous
Heterogeneous
Linear
Nonlinear
Values are arranged in linear fashion and are accessed in sequence. E.g. Arrays, linked lists, stacks and queues
Values are not arranged in order but some hierarchical structure E.g. Trees, graph etc.,
Values of the same types of data are stored, as in an array or list
Data values of different types are grouped, as in records and classes
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Data structures can be linear and non linear27
Data Structures
Linear Data Structures can be Non Linear Data Structures
are lists that can be
Array
Linked List may implement
Stack
Queue
are
Tree
Graph
Hash table
Guided Activity: Introduction to Data Structures Assignment 31 and 32
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Arrays An Introduction Collection of homogeneous elements stored in continuous memory locations
Accessed in sequential as well as random
Uses index (position of the element) to locate an element
28
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Arrays Implementation in Java In Java, array is implemented as a collection of similar data in contiguous
locations of memory having the same name
Each variable in an array is called an array element
All the elements are of same type, but may contain different values
The position of each array element is known as array index or subscript
An array can either be one dimensional (1-D) or two dimensional (2-D) or Multi-dimensional
Declaration and initialization of one/two dimensional (1-D and 2-D) arrays:
29
data type[] array name = {value1, value2, value3,.,value n};
data type[][] arrayname = {value1, value2, value3,.,value n};
Example:int[] itemId={5001,5002,5003,5004,5005};
char[][] array1={ {'a','2','*'},{'\0','I','n'},{'I','n','f','y','\0'} };
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Assignments in Arrays 30
Demo : Data Structures - Arrays Assignment 33
Guided Activity: Data Structures- Arrays Assignment 34,35,36,37
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Linked List Collection of homogeneous data elements, not necessarily stored in continuous
memory locations
Memory location is logically referred to as node
Node contains two parts:
31
data linknode
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Linked List Operations32
Demo: Data Structures- Linked List - Assignment 38
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Stack Ordered collection of elements in which only one end used for insertion and for
deletion
Elements are removed from the stack in reverse order, in which they were inserted into it. So, stack is called Last-In-First-Out (LIFO) List.
Stack is an abstract data structure that can be implemented using either array or a linked list
33
Data Structures - Stack - Assignment 39
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Stack Operations 34
Demo: Data Structures - Stack - Assignment 40
Guided Activity: Data Structures - Stack - Assignment 41
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Queue Ordered collection of elements in insertion is done using one end and deletion
using the other end
Elements are removed from the queue in the order of their arrival into the queue. So, queue is called First-In-First-Out (FIFO) List.
Queue is an abstract data structure that can be implemented using either array or a linked list
35
Guided Activity: Data Structures- Queue Assignment 42
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Queue Operations 36
Demo: Data Structures - Queue - Assignment 43
Non LinearData Structures
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Trees Introduction Used to model certain types of real world scenarios
Directory structures, hierarchical classification or categories etc.,
Consists of a root node and zero or more levels of additional nodes
Non-root and non-leaf nodes are called internal nodes
Nodes that have no children are called leaf nodes
38
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Tree organization structure 39
Guided Activity: Advanced Data Structures- Trees - Assignment 44
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Binary Search Tree Trees, in general, do not restrict the number of child nodes that any node can
have
A commonly used tree is a binary tree where any node can have a maximum of two child nodes
Binary Search Tree is an application of binary tree where nodes are organized for efficient search and retrieval operations
40
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Binary Search Tree assignments41
Demo: Advance Data Structures- Binary Search Tree - Assignment 45
Guided Activity: Advanced Data Structures- Binary Search Trees -
Assignment 46
Copyright 2013-2014, Infosys Limited ConfidentialConfidential
Self study Arrays:
https://www.udacity.com/wiki/cs046/Lesson_7.2_-_ArrayLists_and_Arrays
http://www.homeandlearn.co.uk/java/java_arrays.html
: Linked List:
http://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html
Queues ( Circular and Priority Queues)
http://www.brucemerry.org.za/manual/structures/circular.html
https://www.udacity.com/course/viewer#!/c-cs258/l-48449993/m-48698513
Trees
http://www.bowdoin.edu/~ltoma/teaching/cs210/spring09/Slides/210-Trees.pdf
Graphs
http://en.wikipedia.org/wiki/Travelling_salesman_problem
42
2013 Infosys Limited, Bangalore, India. All Rights Reserved. Infosys believes the information in this document is accurate as of its publication date; such information is subject to changewithout notice. Infosys acknowledges the proprietary rights of other companies to the trademarks, product names and such other intellectual property rights mentioned in this document. Exceptas expressly permitted, neither this documentation nor any part of it may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, printing,photocopying, recording or otherwise, without the prior permission of Infosys Limited and/ or any named intellectual property rights holders under this document.
Thank You