04-CCFP4.0_OOP Using Java

Embed Size (px)

Citation preview

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    1/42

    OO programming using Java

    Education, Training and AssessmentWe enable you to leverage knowledge anytime,

    anywhere!

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    2/42

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

    2

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    3/42

    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 fromthe 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

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    4/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    Course Information

    Course Code: CCFP4.0-OOP

    Course Name: OO programming using Java

    Document Number: OOP-04

    Version Number: V4.0

    4

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    5/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    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 

    5

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    6/42

    OO Fundamentals

    Education, Training and AssessmentWe enable you to leverage knowledge anytime,

    anywhere!

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    7/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    OO Fundamentals

    • Object Oriented Fundamentals

     –  Arrays – Revisit

     –  Strings

     –  Constructors

     –  “static” keyword

     –  Command Line Arguments

    •  Algorithm Design Techniques

    7

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    8/42

     Arrays - Revisit

    Education, Training and AssessmentWe enable you to leverage knowledge anytime,

    anywhere!

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    9/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

     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

    9

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    10/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

     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:

     –  Method 2:

    10

    long telephoneNos[] = new long[3];

    telephoneNos[0] = 48214280200L;

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

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

    Where will the memory be

    allocated for this array?

    Note that the [] can be placed

    before or after the array name

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    11/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

     Arrays (3 of 4)

    •  Array elements will be stored dynamically in the heap

    •  Array elements will be initialized to default values of respective type if not

    explicitly initialized

    11

    Stack

    4821428020

    9901911334

    Heap

    telephoneNos

    long[]

    4821710601

    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

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    12/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

     Arrays (4 of 4)

    • Length property –  Length property represents the size of an array

     –  Length of an array can be identified using the below statement

    12

    Quiz: OO Fundamentals –Assignment 47

    arrayname.length

    Demo: OO Fundamentals - Assignment 48

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    13/42

    Strings

    Education, Training and AssessmentWe enable you to leverage knowledge anytime,

    anywhere!

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    14/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    Strings (1 of 3)

    •  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 

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

     –  Method 3: Combining both the statements in Method 2

    14

    Guided Activity: OO Fundamentals - Assignment 49

    String customerName = new String(“Jack”);

    String customerName;

    customerName = “Jack”;

    String customerName = “Jack”;

    Where will the 

    memory be 

    allocated?

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    15/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    Strings (2 of 3)

    • customerName is the reference variable referring to a String object

    15

    Stack

    Jack

    Heap

    customerName

    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

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    16/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    Strings (3 of 3)

    • String class contains many methods which facilitate operations on the Strings

     –  length()

     –  concat()

     –  equals()

     –  equalsIgnoreCase() etc

    16

    Demo: OO Fundamentals - Assignment 51

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    17/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    Strings - Revisit

    17

    Quiz: OO Fundamentals - Assignment 52

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    18/42

    OO Constructs - I

    Education, Training and AssessmentWe enable you to leverage knowledge anytime,

    anywhere!

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    19/42

    Constructors

    Education, Training and AssessmentWe enable you to leverage knowledge anytime,

    anywhere!

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    20/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    Constructor 

    • 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 

     –  Parameterized constructor 

    20

    Guided Activity: OO Fundamentals - Assignment 53

    Demo: OO Fundamentals - Assignment 54

    Demo: OO Fundamentals - Assignment 55

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    21/42

    ‘static’ keyword

    Education, Training and AssessmentWe enable you to leverage knowledge anytime,

    anywhere!

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    22/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    static keyword (1 of 2)

    • 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

    22

    Demo: OO Fundamentals - Assignment 56

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    23/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    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

    23

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    24/42

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    25/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    Command Line Arguments

    • 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

    • To convert command line arguments passed as String to integer use

    Integer.parseInt() method

    25

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

    Demo: OO Fundamentals - Assignment 57

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    26/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    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 throughcommand line arguments

    26

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    27/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    Retail Application – we have learnt

    27

    1. Arrays

    2. Strings

    3. Constructors

    4. ‘static’ keyword

    5. Command Line Arguments

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    28/42

     Algorithm Design Techniques

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    29/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

     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

    29

    Guided Activity: Algorithm Design Techniques – Assignment 58

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    30/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

     Algorithm design techniques

    30

     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

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    31/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

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

    31

    32

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    32/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    Brute force approach: Example – Linear Search and

    Bubble Sort

    32

    Demo: Algorithm Design Techniques – Assignment 59

    33

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    33/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

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

    33

    34

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    34/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

    Divide and conquer approach: Example - Binary Search

    34

    Demo: Algorithm Design Techniques – Assignment 60-A

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    35/42

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    36/42

    3737

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    37/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

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

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    38/42

    © 2013Infosys Limited, Bangalore, India. All Rights Reserved. Infosys believes theinformation in thisdocument is accurate as of its publication date; such information is subject to changewithout notice. Infosys acknowledgesthe proprietaryrights of other companiesto thetrademarks,product names andsuch other intellectual property rightsmentioned in this document.Exceptasexpressly permitted,neither thisdocumentation nor anypart of it maybe reproduced,stored in a retrievalsystem,or transmittedin anyform or by anymeans, electronic, mechanical,printing,photocopying,recordingor otherwise,withoutthe prior permissionof Infosys Limitedand/or anynamedintellectual property rightsholdersunderthis document.

    Thank You

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    39/42

     Appendix

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    40/42

    41

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    41/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

     Arrays (2 of 2) – Self Study

    Multi-dimensional Array

     – No. of rows can be obtained as follows:

     – No. of columns in a row can be obtained as follows:

     – Consider the array given below:

    arrayname[rownumber].length

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

    arrayname.length

    arrNum.length, i.e 2

    arrNum[1].length, i.e 2

    What is the size of the array-

    arrNum?

    How many columns are there

    in second row (index number

    one) of the array- arrNum?

    42

  • 8/20/2019 04-CCFP4.0_OOP Using Java

    42/42

    Copyright © 2013-2014, Infosys Limited ConfidentialConfidential

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