OO programming using Java

Education, Training and Assessment
We enable you to leverage knowledge anytime,
anywhere!

2

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

3

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

4

Course Information

Course Code: CCFP4.0-OOP
Course Name: OO programming using Java
Document Number: OOP-04
Version Number: V4.0

Copyright © 2013-2014, Infosys Limited

Confidential

5 Recap • Object Oriented Fundamentals – Java Architecture – Variables in detail – Reference Variables & Objects in Memory – Methods – Parameter Passing Techniques – ‘this’ reference • Data Structures – Linear – Non–linear Copyright © 2013-2014. Infosys Limited Confidential .

Training and Assessment We enable you to leverage knowledge anytime.OO Fundamentals Education. anywhere! .

Infosys Limited Confidential .7 OO Fundamentals • Object Oriented Fundamentals – Arrays – Revisit – Strings – Constructors – “static” keyword – Command Line Arguments • Algorithm Design Techniques Copyright © 2013-2014.

Training and Assessment We enable you to leverage knowledge anytime.Revisit Education.Arrays . anywhere! .

Infosys Limited • Confidential .9 Arrays (1 of 4) • An array is a collection of similar data in contiguous locations of memory having the same name • Arrays can be used to store data belonging to primitive data types and reference types • Arrays are created dynamically in Java • Types of Arrays – Single Dimensional Arrays – Multi-Dimensional Arrays Copyright © 2013-2014.

4821710601L}. 9901911334L.10 Arrays (2 of 4) Single Dimensional Arrays : • An array with one dimension (one row) is called a single dimensional array • Different ways of creating and initializing an array are given below – Method 1: Where will the memory be allocated for this array? long telephoneNos[] = new long[3]. – Method 2: long [] telephoneNos= {48214280200L. telephoneNos[0] = 48214280200L. Note that the [] can be placed before or after the array name Copyright © 2013-2014. telephoneNos[1] = 9901911334L. Infosys Limited Confidential . telephoneNos[2] = 4821710601L.

the reference would be stored in heap Copyright © 2013-2014.11 Arrays (3 of 4) long[] 4821428020 telephoneNos 9901911334 4821710601 Stack Heap • Array elements will be stored dynamically in the heap • Array elements will be initialized to default values of respective type if not explicitly initialized Note: Here it is assumed that the declaration of telephoneNos is done inside the main () method. If the same is an instance variable. Infosys Limited Confidential .

length Demo: OO Fundamentals . Infosys Limited Confidential .12 Arrays (4 of 4) Quiz: OO Fundamentals –Assignment 47 • Length property – Length property represents the size of an array – Length of an array can be identified using the below statement arrayname.Assignment 48 Copyright © 2013-2014.

anywhere! . Training and Assessment We enable you to leverage knowledge anytime.Strings Education.

– Method 3: Combining both the statements in Method 2 String customerName = “Jack”.Assignment 49 • A set of related characters can be represented using a string • In Java. Copyright © 2013-2014. customerName = “Jack”. – Method 2: Assigning a group of characters into a String object String customerName.14 Strings (1 of 3) Guided Activity: OO Fundamentals . this is done with the help of a built in class called String • Creation and Initialization of a String object – Method 1: Creating an object of String using new operator String customerName = new String(“Jack”). Infosys Limited Confidential Where will the  memory be  allocated? .

then it would be stored in heap Quiz: OO Fundamentals . If the customerName is an instance variable. Infosys Limited Confidential .15 Strings (2 of 3) Jack customerName Stack Heap • customerName is the reference variable referring to a String object Note: Here it is assumed that the declaration of customerName is done inside the main () method.Assignment 50 Copyright © 2013-2014.

16 Strings (3 of 3) Demo: OO Fundamentals . Infosys Limited Confidential .Assignment 51 • String class contains many methods which facilitate operations on the Strings – length() – concat() – equals() – equalsIgnoreCase() etc Copyright © 2013-2014.

Assignment 52 Copyright © 2013-2014.17 Strings .Revisit Quiz: OO Fundamentals . Infosys Limited Confidential .

OO Constructs . anywhere! . Training and Assessment We enable you to leverage knowledge anytime.I Education.

Training and Assessment We enable you to leverage knowledge anytime.Constructors Education. anywhere! .

Infosys Limited Confidential .Assignment 54 – Parameterized constructor Demo: OO Fundamentals .Assignment 53 • Constructor is a special method that has the same name as that of a class • The constructor is used to initialize the instance variables of the class • Types of constructors: – Default constructor Demo: OO Fundamentals .20 Constructor Guided Activity: OO Fundamentals .Assignment 55 Copyright © 2013-2014.

Training and Assessment We enable you to leverage knowledge anytime. anywhere! .‘static’ keyword Education.

Infosys Limited Confidential . static keyword can be used in 3 scenarios: – static variables: • Also called class variables and are initialized at class load time – static methods • Used for accessing static variables. • Can be invoked using class name or object reference – static blocks • Block of statements in a java class which gets executed when a class is first loaded in memory • Can have any number of static blocks in which case it gets executed in the order in which it is written Copyright © 2013-2014.22 static keyword (1 of 2) Demo: OO Fundamentals .Assignment 56 • The concept of static is used whenever data which is common to the objects of the class is required • In Java.

Infosys Limited Confidential .23 static keyword (2 of 2) • Static methods and static blocks can access only other static data members and methods directly • Non static variable cannot be accessed directly inside a static method or static block Copyright © 2013-2014.

anywhere! . Training and Assessment We enable you to leverage knowledge anytime.Command Line Arguments Education.

parseInt() method Copyright © 2013-2014.25 Command Line Arguments Demo: OO Fundamentals .when the program is invoked public static void main(String [] args){ } • To convert command line arguments passed as String to integer use Integer.Assignment 57 • To pass an argument to the main method • The String[] (array of String objects) contains the command line arguments that are passed . Infosys Limited Confidential .

26 Revisiting main() method • main() is declared as public so it can be accessed from anywhere • static allows main( ) to be called without having to instantiate a particular instance of the class • This is required because the main( ) is called by the Java interpreter before any objects are created • void informs the compiler that main( ) does not return any value • The parameter passed to the main() i. String args[] facilitates input through command line arguments Copyright © 2013-2014. Infosys Limited Confidential .e.

3. 5. 4. 2. Infosys Limited Confidential .27 Retail Application – we have learnt 1. Arrays Strings Constructors ‘static’ keyword Command Line Arguments Copyright © 2013-2014.

Algorithm Design Techniques .

29 Algorithm design techniques . Infosys Limited Confidential .Introduction • A problem can be solved by using different logical approaches / algorithms • The analysis of various approaches and the choice of a particular approach is a key step in the solution of a given problem • The choice of a particular approach may depend on factors like the efficiency in terms of time. • Algorithm design techniques are used to implement an approach based on factors that are important for a solution Guided Activity: Algorithm Design Techniques – Assignment 58 Copyright © 2013-2014. memory requirements or how optimal solution is.

30 Algorithm design techniques Algorithm Design Techniques are Brute Force Approach Exhaustively try every possibility Divide and Conquer Approach Split into smaller problems and solve Greedy Approach First solve the problem that requires least effort Dynamic Programming Approach Identify overlapping sub problems and reuse their solutions Copyright © 2013-2014. Infosys Limited Confidential .

Infosys Limited Confidential . Copyright © 2013-2014.31 Brute force approach • Intuitive / straight forward problem solving approach • Directly based on the problem statement and the concept ..”Just Do It” • Covers a wide range of problems under its gamut • Simple but may be inefficient in certain cases • Examples include bubble sort. linear search etc.

32 Brute force approach: Example – Linear Search and Bubble Sort Demo: Algorithm Design Techniques – Assignment 59 Copyright © 2013-2014. Infosys Limited Confidential .

33 Divide and conquer approach • Divide the problem into two or more independent subproblems • Solve the subproblems and combine solutions of the subproblems to get overall solution • Examples include binary search. Copyright © 2013-2014. Infosys Limited Confidential . hash table etc..

Infosys Limited Confidential .34 Divide and conquer approach: Example .Binary Search Demo: Algorithm Design Techniques – Assignment 60-A Copyright © 2013-2014.

35 Dynamic programming approach • Divide the problems into sub problems and identify sub problems that overlap • The solutions of overlapping sub problems are reused in arriving at the overall solution • The difference between dynamic programming and divide and conquer is that the sub problems in divide and conquer are considered to be disjoint and distinct whereas in dynamic programming they are overlapping • Examples include Fibonacci series generation Copyright © 2013-2014. Infosys Limited Confidential .

. Infosys Limited Confidential . (Increases space requirement) • Compute Fibonacci numbers iteratively starting from the smallest (0) to the biggest one (n) Pseudo code : FIBO (n) F[0] = 0 F[1] = 1 for i = 2 to n F[i] = F[i-1] + F[i-2] end for return F[n] Analysis: This just has a linear loop with array accesses in the loop.n] to store all Fibonacci numbers from 0 to n.36 Dynamic programming: Example – Fibonacci series generation • Create an array F[0. so this has a complexity of O(n) Guided Activity: Algorithm Design Techniques – Assignment 60-B Copyright © 2013-2014.

3737 Greedy approach • Primarily used in Optimization problems • Constructs solution through a sequence of steps where each step is considered to be a partial solution. Copyright © 2013-2014. Infosys Limited Confidential . travelling salesman problem etc. This is extended to get the complete solution • Choice of each step must be – Locally optimal – Feasible – Irrevocable • Examples include currency change making..

recording or otherwise. product names and such other intellectual property rights mentioned in this document. printing. All Rights Reserved. Infosys believes the information in this document is accurate as of its publication date. Bangalore. mechanical. Except as expressly permitted. Infosys acknowledges the proprietary rights of other companies to the trademarks. . without the prior permission of Infosys Limited and/ or any named intellectual property rights holders under this document. photocopying. electronic. stored in a retrieval system. or transmitted in any form or by any means. such information is subject to change without notice. neither this documentation nor any part of it may be reproduced.Thank You © 2013 Infosys Limited. India.

Appendix .

40 Arrays (1 of 2) – Self Study Multi-Dimensional Arrays • An array with more than one dimension • Different ways of creating and initializing a multi-dimensional array are given below: – Method 1 int array[][]= new int[2][3]. array[0][1]=2. Copyright © 2013-2014. etc – Method 2 int array[][]= {{1. Infosys Limited Confidential .{3. array[0][0]=1.4}}. or int[][] array= new int [2][3].2}.

length – Consider the array given below: char[][] arrNum={{‘a’.length.length. i. Infosys Limited Confidential .’2’}.41 Arrays (2 of 2) – Self Study Multi-dimensional Array – No.’I’}} How many columns are there in second row (index number one) of the array. of columns in a row can be obtained as follows: arrNum? arrayname[rownumber].e 2 arrNum[1].arrNum? arrNum. of rows can be obtained as follows: arrayname.length What is the size of the array– No.e 2 Copyright © 2013-2014. i.{‘###BOT_TEXT###’.

This is extended to get the complete solution • Choice of each step must be – Locally optimal – Feasible – Irrevocable • Examples include currency change making.. Infosys Limited Confidential . travelling salesman problem etc.42 Greedy approach • Primarily used in Optimization problems • Constructs solution through a sequence of steps where each step is considered to be a partial solution. Copyright © 2013-2014.