ProblemSolving Introduction to OOP Objectsv1.0

Embed Size (px)

Citation preview

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    1/16

    INTERNAL

    March 23, 2009

    Problem Solving

    Introduction to Objects

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    2/16

    Problem Solving

    INTERNAL- 1 -

    Topic Objective

    Understand Objects

    Existence

    Identity Responsibilities

    Understand Classes

    Message Passing

    Abstraction

    Encapsulation

    Class Relationships

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    3/16

    Problem Solving

    INTERNAL

    Elements of Programming

    Data

    Programs are set of instructions that act upon data that is received (input) and transforms

    it into required information.

    Expressions

    Instructions that carry out required operations on the given data.

    Functions/Methods

    A block of instructions that accept some data, carry out some operations and returns an

    output.

    Calculate Gross

    Salary

    Basic Pay, DA Rate,

    HRA RateGross Salary

    Data

    Function

    Inside Calculate Gross Salary

    DA = Basic Pay * DA Rate/100

    HRA = Basic Pay * HRA

    Rate/100Gross Salary = Basic Pay +

    DA+ HRA

    Return Gross Salary

    Expressions

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    4/16

    Problem Solving

    INTERNAL

    Structured approach to programming

    Structured approach to programming, implements a system as a collection of functions that

    accepts data, processes it and returns an output.

    A function some times calls another function, when it wants to make use of the functionality

    defined in the other function.A program to calculate salary of an employee might look some what like this. A function to

    calculate salary, calling another two functions that calculates HRA and DA. The values

    returned by these modules is added up in the module that calculates salary,

    Calculate Salary

    Calculate HRA Calculate DA

    Basic pay,

    HRA Rate, DA

    Rate salary

    Basic pay,

    HRA RateHRA

    Basic pay,DA Rate

    DA

    CalculateSalary(Basic Pay, HRA Rate, DA Rate)

    {

    DA = call CalculateDA(Basic Pay, DA Rate)

    HRA = call CalulateHRA(Basic Pay, HRA Rate)

    Salary = DA+HRA

    Return Salary

    }

    Calculate DA(Basic Pay, DA Rate)

    {

    Return DA Rate*Basic Pay /100

    }

    Calculate HRA(Basic Pay, HRA Rate)

    {

    Return HRA Rate * Basic Pay/100

    }

    Note: This is a pseudo code

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    5/16

    Problem Solving

    INTERNAL

    Writing programs that simulate real world domains

    Structured approach implements a system as a collection of functions that act upon data

    that is passed into it.

    We write programs that make up a software to solve a problem in real world. In real world

    problem domains we do not find any functions that work on data.

    We find objects that are described by some state (data) and having some behaviours

    (functions).

    Object Oriented Approach to programming is about implementing a system as collection of

    objects that have state and behaviours, interacting with each other to achieve expected

    functionalities.

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    6/16

    Problem Solving

    INTERNAL- 5 -

    Objects Make the World

    An object is a real world entity that has a well defined structure and behaviour.

    Characteristics of an object

    State Behaviour

    Responsibility

    Communication

    Identity

    Examples of objects

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    7/16

    Problem Solving

    INTERNAL

    StateWhat an object knows

    State of an object is a set of properties that describes the object, which can take different

    values during the life time of the object.

    Bob is an employee of tech smart, let us havea look into Bobs properties as an employee: Bob

    Name: Bob

    Emp.No. 2312

    Department: Marketing

    BasicPay = 10,000

    Designation: Manager

    Linda

    Name: Linda

    Emp.No. 3361

    Department: Marketing

    BasicPay = 9,500

    Designation: Executive

    Linda is another employee

    Knowing Responsibility: It is the responsibility of an object to know its state

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    8/16

    Problem Solving

    INTERNAL

    Behaviours

    Behaviour is how objects react to its state changes or any other operations performed on it

    Bank account

    State:

    Account number:121XXXXXXX

    Account Balance:Rs. XXXXX/-

    Opening Date:12/01/2005

    Branch:XXXXX

    Bank accountBehaviours

    Withdraw

    Deposit

    Generate Transaction Statement

    A bank account can perform these responsibilities, by making use of state values or

    other data provided to it.

    Doing responsibility: It is the responsibility of object to behave according

    the operations performed on it

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    9/16

    Problem Solving

    INTERNAL- 8 -

    Doing responsibilities

    Doing something itself, like creating an object/doing a calculation

    Initiating action in other objects Controlling and coordinating activities in other objects

    Knowing responsibilities

    Knowing about state

    Knowing about related objects

    Knowing about things it can derive or calculate

    Responsibilities

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    10/16

    Problem Solving

    INTERNAL

    Communication

    Objects can communicate with each other, provided they know each other.

    Objects can respond to the message send by another object, by performing an action, or

    returning some data.

    Account

    121XXXX

    Please

    deposit Rs.

    1000/- to

    account

    121XXXX

    Customer

    Deposit an

    amount of

    Rs. 1000/-

    Balance

    updated

    accordingly, new

    balance is Rs.

    XXXXX/-

    Money credited

    to your account,

    balance is Rs.

    XXXXX

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    11/16

    Problem Solving

    INTERNAL

    Identifying objects and behaviours

    The nouns in the requirements specification would suggest the Objects in the problem

    domain.

    The verbs from the requirements specification would suggest object behaviors.

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    12/16

    Problem Solving

    INTERNAL- 11 -

    Problem Context - Prepaid Connection manager

    A local grocery shop in our society is an authorized distributor for prepaid and post paid

    GSM mobile connections for Talk the Talk" service provider. The shop sells new

    connections, provide easy recharge and topups. The shop also maintains an inventory of

    connections sold, and re-charges and top-ups done from there, for audits and reports.

    Identify objects and their responsibilities

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    13/16

    Problem Solving

    INTERNAL- 12 -

    Objects in the scenario

    Try to find out the objects involved.

    ? What could be the possible objects?? Connection, TopUp, anything else?

    ? The Recharge list itself?

    ? Each entry in the recharge list?

    ? Anything missed out?

    ? What are the attributes of each objects?

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    14/16

    Problem Solving

    INTERNAL- 13 -

    Object Way..

    Once you have identified the objects, we need to identify the behaviours ofobjects.

    ? Which object will take the responsibility of recording a top up made againsta connection?

    ? Which object will take care of knowing talk-time in a re-charge coupon?

    ? Which object will know about the details of transaction on connections?

    ? What are the responsibilities of Connection object?

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    15/16

    Problem Solving

    INTERNAL

    Summary

    An object is a real world entity that has a well defined structure and

    behaviour.

    Characteristics of an object

    State Behaviour

    Responsibility

    Communication

    Identity

  • 8/12/2019 ProblemSolving Introduction to OOP Objectsv1.0

    16/16

    Happy Learning!