0% found this document useful (0 votes)
11 views28 pages

Lec 20

Uploaded by

vamsi.d124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views28 pages

Lec 20

Uploaded by

vamsi.d124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

ESc101 : Fundamental of Computing

I Semester 2008-09

Lecture 20

Object Oriented programming



Classes and Objects

1
What kind of problems did we solve till now

1. Computing Factorial of a number

2. Computing highest permutation

3. Checking if a number is palindrome

4. Computing prime numbers less than an integer n.

mathematical problems involving primitive (especially numeric) types only

2
Which methodology we used

Structured programming : A program is a group of methods which compute


solution of the problem when executed on a given input data.

The methods are designed in total isolation from ... the property of data to
be processed

3
Which methodology we used

Structured programming : A program is a group of methods which compute


solution of the problem when executed on a given input data.

The methods are designed in total isolation from the property of data to be
processed

4
A fact of real life

Most of the computational problems we face are NOT on merely numbers.

5
Examples

1. Problem involving maintenance of student records


Roll numbers, name, CPI,...

2. Problem involving maintenance Bank accounts


account number, balance, interest,...

3. Problem involving computing some geometric structure of a set of points


Examples : smallest enclosing circle, intersection of line segments.
x-coordinate, y-coordinate

6
Aim : first to model the problem and then solve it ?

7
Modeling the problem

1. For student records will you need the family history of the student ?

2. For Bank accounts will you need to know the height of the account holder ?

3. For points, will you need to know the source from where they were generated
?
NO
We just focus on those features which are relevant to the problem

8
Abstraction : Ist step in modeling the problem

Abstraction of the problem :


Focus only on those features which are relevant to the problem

You have been using it for long (without perhaps noticing it consciously)

9
Let us review the Examples again

1. Problem involving maintenance of student records

2. Problem involving maintenance of Bank accounts

3. Problem involving computing some geometric structure of a set of points

which words catch your eyes ?

10
Let us review the Examples again

1. Problem involving maintenance of student records

2. Problem involving maintenance of Bank accounts

3. Problem involving computing some geometric structure of a set of points

which words catch your eyes ?

11
Maintaining student records

Each student record will have the following


pieces of information.

• Name, Roll number, SPI, CPI ...


and methods for each record

• Retrieve Roll number


• Retrieve Name
• Update CPI
• ...

12
Maintaining student records

Each student record will have the following


pieces of information.

• Name, Roll number, SPI, CPI ...


and methods for each record

• Retrieve Roll number,


• Retrieve Name
• Update CPI
• ...

13
Maintaining student records

Each student record will have the following


attributes

• Name, Roll number, SPI, CPI ...


and methods for each record :

• Retrieve Roll number,


• Retrieve Name
• Update CPI
• ...

14
Maintaining Bank accounts

Each account will have the following


attribute

• Account number, Name, Type of account, Balance,...


and methods for each account :

• What is the balance ?


• What was the last transaction ?
• Deposit a given amount ?
• Withdraw a given amount ?
• ...

15
Maintaining Bank accounts

Each account will have the following


attribute

• Account number, Name, Type of account, Balance,...


and methods for each account :

• What is the balance ?


• What was the last transaction ?
• Deposit a given amount ?
• Withdraw a given amount ?
• ...

16
Computing some geomteric structure for a set of points

Each point will have the following


attributes

• x coordinate, y coordinate
and methods for points

• get the x coordinate


• get the y coordinate
• get distance from origin
• translate the point by ∆x , ∆y
• ...

17
Computing some geomteric structure for a set of points

Each point will have the following


attributes

• x coordinate, y coordinate
and methods for points

• get the x coordinate


• get the y coordinate
• get distance from origin
• translate the point by ∆x , ∆y
• ...

18
Crucial Observation

For a given real life problem :

whenever we think of the (attributes of the) data, ... the thoughts of the methods
on them comes automatically in our mind.
So it is useful to think of (the attributes of) data in conjunction of the methods
which work on them.

19
Crucial Observation

For a given real life problem :

whenever we think of the (attributes of the) data, the thoughts of the methods on
them comes automatically in our mind.
So it is useful to think of (the attributes of) data in conjunction of the methods
which work on them.

20
Crucial Observation

For a given real life problem :

whenever we think of the (attributes of the) data, the thoughts of the methods on
them comes automatically in our mind.
So it is useful to think of (the attributes of) data in conjunction of the
methods which work on them.

21
Objects

Objects are self contained entity which has its own collection of

• attributes
• methods to access them, manipulate them and compute some functions on
them.

22
Encapsulation : one of the fundamental principle of OOP

The ability of an object to be a container (or capsule) for its attributes (i.e. data
variables) and its related methods (i.e. functions).

Object-oriented programming may be seen as a collection of cooperating


objects.

(it will become clear as we proceed)

23
What is a class ?

Definition : A class specifies the attributes (data) and methods (actions) that
objects can work with.

It is just a template or prototype for each of many objects belonging to the class.

24
How to describe Class for Point

What will be the attributes or properties of point ?


25
Class for Point

attributes

• x-coordinate
• y-coordinate
The methods dealing with Point :
set x-coordinate or y-coordinate of a point
get x-coordinate and y-coordinate of a point
Square of distance from origin
translation of a point

26
Class for Point

public class Point


{ // what are the attributes ?
double x;
double y;
// what are the actions or methods ?
public void setX(double x_value) {x = x_value;}
public void setY(double y_value) { y = y_value;}
public double getX() {return x;}
public double getY() {return y;}
public double Square_distance_from_origin()
{return x*x+y*y}
}

27
How to create an object

Point P; //P is NOT a Point object.


//P is reference to an object of class Point

P = new Point(); //an object of class Point is created by this command


//P stores the reference to it.

28

You might also like