You are on page 1of 43

OO programming using Java

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

Copyright Guideline
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 change without notice. Infosys acknowledges the proprietary rights of
other companies to the trademarks, product names and such other intellectual property rights
mentioned in this document. Except as 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.

Copyright 2013-2014, Infosys Limited

Confidential

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
Copyright 2013-2014,
Infosys Limited

Confidential

Course Information

Course Code: CCFP4.0-OOP


Course Name: OO programming using Java
Document Number: OOP-03
Version Number: V4.0

Copyright 2013-2014, Infosys Limited

Confidential

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

Copyright 2013-2014, Infosys Limited

Confidential

OO Fundamentals
Implementation in Java

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

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

Copyright 2013-2014, Infosys Limited

Confidential

Java Architecture (1 of 2)
Java Architecture is composed of the following components:

JavaProgramming
Language

JavaByteCode

JavaVirtual
Machine

ObjectOrientation
Programmerfriendly
features
Robust
Platformindependent

Copyright 2013-2014, Infosys Limited

Intermediatecode
generatedbytheJava
compiler
AlsocalledJavaclass
file
FacilitatesWriteOnce,
RunAnywhere

Confidential

Loadsbytecodeand
executesthem
Itisplatform
dependent
Differentfordifferent
platforms

Java Architecture (2 of 2)

Copyright 2013-2014, Infosys Limited

Confidential

10

Program Life Cycle


Inputdevices
Compilation & Execution:

Interacts with

Resides in
secondary memory

On execution

On execution

Program

ByteCode
Loaded to main
memory (process)
Output

ByteCode

On compilation

Resides in
secondary memory

Copyright 2013-2014, Infosys Limited

Confidential

Generated on
output devices

11

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

CodeSegment

ExecutableCode

Datasegment

Globalvariables

Heap

DynamicMemory

Stack

LocalVariables

Note: Data segment and Global variables are not within the scope of the course

Copyright 2013-2014, Infosys Limited

Confidential

12

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.e
when it loses scope)

Local variables require explicit initialization

Copyright 2013-2014, Infosys Limited

Confidential

13

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

Copyright 2013-2014, Infosys Limited

Confidential

14

Reference Variables & Objects in Memory


Guided Activity: OO Fundamentals - Assignment 26

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

Copyright 2013-2014, Infosys Limited

Confidential

Methods Pass by Value & Pass


by Reference

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

16

Methods Parameter Passing Techniques


Parameter Passing Techniques
Pass-by value & Pass-by reference
Demo: OO Fundamentals - Assignment 27

Guided Activity: OO Fundamentals - Assignment 28

Copyright 2013-2014, Infosys Limited

Confidential

Recursive Methods

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

18

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

Demo : Recursion Assignment 29a

Guided Activity: Recursion - Assignment 29b

Copyright 2013-2014, Infosys Limited

Confidential

19

Towers of Hanoi problem


Problem was discovered by the French mathematician Edouard
Lucas 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

Transferthe3disksfromtowerAtotowerB
Copyright 2013-2014, Infosys Limited

Confidential

20

Towers of Hanoi - 3 discs


Solution:
A

Copyright 2013-2014, Infosys Limited

Confidential

this reference

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

22

this reference
Demo: OO Fundamentals - Assignment 30

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

Copyright 2013-2014, Infosys Limited

Confidential

Data Structures

24

Data Structures Topics


Data Structures
Types
Linear
Nonlinear

Copyright 2013-2014, Infosys Limited

Confidential

25

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.

Copyright 2013-2014, Infosys Limited

Confidential

26

Data Structure
Homogenous
Values of the same types of data are stored, as in an
array or list

Heterogeneous
DataStructures

Data values of different types are grouped, as in records


and classes

Linear
Values are arranged in linear fashion and are accessed in
sequence. E.g. Arrays, linked lists, stacks and queues

Nonlinear
Values are not arranged in order but some hierarchical
structure E.g. Trees, graph etc.,
Copyright 2013-2014, Infosys Limited

Confidential

27

Data structures can be linear and non linear


Data Structures
Linear Data Structures

can be

Non Linear Data Structures

are lists that can be

are
Tree

Array
Linked List

may implement

Graph
Hash table

Stack
Queue
Guided Activity: Introduction to Data Structures Assignment 31 and 32
Copyright 2013-2014, Infosys Limited

Confidential

28

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

Copyright 2013-2014, Infosys Limited

Confidential

29

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 Multidimensional
Declaration and initialization of one/two dimensional (1-D and 2-D) arrays:
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

Confidential

30

Assignments in Arrays

Demo : Data Structures - Arrays Assignment 33

Guided Activity: Data Structures- Arrays Assignment 34,35,36,37

Copyright 2013-2014, Infosys Limited

Confidential

31

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:

data

link
node

Copyright 2013-2014, Infosys Limited

Confidential

32

Linked List Operations

Demo: Data Structures- Linked List - Assignment 38

Copyright 2013-2014, Infosys Limited

Confidential

33

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

Data Structures - Stack - Assignment 39

Copyright 2013-2014, Infosys Limited

Confidential

34

Stack Operations

Demo: Data Structures - Stack - Assignment 40

Guided Activity: Data Structures - Stack - Assignment 41

Copyright 2013-2014, Infosys Limited

Confidential

35

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

Guided Activity: Data Structures- Queue Assignment 42

Copyright 2013-2014, Infosys Limited

Confidential

36

Queue Operations

Demo: Data Structures - Queue - Assignment 43

Copyright 2013-2014, Infosys Limited

Confidential

Non Linear
Data Structures

38

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

Copyright 2013-2014, Infosys Limited

Confidential

39

Tree organization structure

Guided Activity: Advanced Data Structures- Trees - Assignment 44

Copyright 2013-2014, Infosys Limited

Confidential

40

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

Copyright 2013-2014, Infosys Limited

Confidential

41

Binary Search Tree assignments

Demo: Advance Data Structures- Binary Search Tree - Assignment 45

Guided Activity: Advanced Data Structures- Binary Search Trees Assignment 46

Copyright 2013-2014, Infosys Limited

Confidential

42

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
Copyright 2013-2014, Infosys Limited

Confidential

Thank You

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 change
without notice. Infosys acknowledges the proprietary rights of other companies to the trademarks, product names and such other intellectual property rights mentioned in this document. Except
as 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.

You might also like