Sie sind auf Seite 1von 42

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-04
Version Number: V4.0

Copyright 2013-2014, Infosys Limited

Confidential

Recap
Object Oriented Fundamentals
Java Architecture
Variables in detail
Reference Variables & Objects in Memory
Methods Parameter Passing Techniques
this reference

Data Structures
Linear
Nonlinear

Copyright 2013-2014, Infosys Limited

Confidential

OO Fundamentals

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

OO Fundamentals
Object Oriented Fundamentals
Arrays Revisit
Strings
Constructors
static keyword
Command Line Arguments

Algorithm Design Techniques

Copyright 2013-2014, Infosys Limited

Confidential

Arrays - Revisit

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

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, Infosys Limited

Confidential

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];


telephoneNos[0] = 48214280200L;
telephoneNos[1] = 9901911334L;
telephoneNos[2] = 4821710601L;

Method 2:
long [] telephoneNos= {48214280200L, 9901911334L, 4821710601L};

Note that the [] can be placed


before or after the array name

Copyright 2013-2014, Infosys Limited

Confidential

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, the reference would be stored in heap

Copyright 2013-2014, 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.length

Demo: OO Fundamentals - Assignment 48

Copyright 2013-2014, Infosys Limited

Confidential

Strings

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

14

Strings (1 of 3)
Guided Activity: OO Fundamentals - Assignment 49

A set of related characters can be represented using a string


In Java, 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
StringcustomerName=newString(Jack);

Method 2: Assigning a group of characters into a String object


StringcustomerName;
customerName=Jack;

Method 3: Combining both the statements in Method 2


StringcustomerName=Jack;

Copyright 2013-2014, Infosys Limited

Confidential

Wherewillthe
memorybe
allocated?

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.
If the customerName is an instance variable, then it would be stored in heap

Quiz: OO Fundamentals - Assignment 50

Copyright 2013-2014, Infosys Limited

Confidential

16

Strings (3 of 3)

Demo: OO Fundamentals - Assignment 51

String class contains many methods which facilitate operations on the Strings
length()
concat()
equals()
equalsIgnoreCase() etc

Copyright 2013-2014, Infosys Limited

Confidential

17

Strings - Revisit

Quiz: OO Fundamentals - Assignment 52

Copyright 2013-2014, Infosys Limited

Confidential

OO Constructs - I

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

Constructors

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

20

Constructor
Guided Activity: 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 - Assignment 54

Parameterized constructor
Demo: OO Fundamentals - Assignment 55

Copyright 2013-2014, Infosys Limited

Confidential

static keyword

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

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, 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, 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, Infosys Limited

Confidential

Command Line Arguments

Education, Training and Assessment


We enable you to leverage knowledge anytime,
anywhere!

25

Command Line Arguments


Demo: OO Fundamentals - Assignment 57

To pass an argument to the main method


The String[] (array of String objects) contains the command line arguments that
are passed ,when the program is invoked

public static void main(String [] args){ }

To convert command line arguments passed as String to integer use


Integer.parseInt() method

Copyright 2013-2014, 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.e. String args[] facilitates input through
command line arguments

Copyright 2013-2014, Infosys Limited

Confidential

27

Retail Application we have learnt


1.
2.
3.
4.
5.

Arrays
Strings
Constructors
static keyword
Command Line Arguments

Copyright 2013-2014, Infosys Limited

Confidential

Algorithm Design Techniques

29

Algorithm design techniques - 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, memory requirements or how optimal solution is.
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, Infosys Limited

Confidential

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

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.,

Copyright 2013-2014, Infosys Limited

Confidential

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, hash table etc.,

Copyright 2013-2014, Infosys Limited

Confidential

34

Divide and conquer approach: Example - Binary Search

Demo: Algorithm Design Techniques Assignment 60-A

Copyright 2013-2014, Infosys Limited

Confidential

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

36

Dynamic programming: Example Fibonacci series


generation
Create an array F[0..n] to store all Fibonacci numbers from 0 to n.
(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, so this has a
complexity of O(n)

Guided Activity: Algorithm Design Techniques Assignment 60-B


Copyright 2013-2014, Infosys Limited

Confidential

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. This is extended to get the complete solution
Choice of each step must be
Locally optimal
Feasible
Irrevocable

Examples include currency change making, travelling salesman problem etc.,

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.

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]; or int[][] array= new int [2][3];
array[0][0]=1;
array[0][1]=2; etc

Method 2

int array[][]= {{1,2},{3,4}};

Copyright 2013-2014, Infosys Limited

Confidential

41

Arrays (2 of 2) Self Study


Multi-dimensional Array
No. of rows can be obtained as follows:
arrayname.length
What is the size of the array No. of columns in a row can be obtained as follows:
arrNum?

arrayname[rownumber].length

Consider the array given below:


char[][] arrNum={{a,2},{\0,I}}

How many columns are there


in second row (index number
one) of the array- arrNum?

arrNum.length, i.e 2

arrNum[1].length, i.e 2

Copyright 2013-2014, Infosys Limited

Confidential

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. This is extended to get the complete solution
Choice of each step must be
Locally optimal
Feasible
Irrevocable

Examples include currency change making, travelling salesman problem etc.,

Copyright 2013-2014, Infosys Limited

Confidential

Das könnte Ihnen auch gefallen