0% found this document useful (0 votes)
75 views6 pages

LAB Manual: Course: CSC241-Object Oriented Programming

The document is a lab manual for an object oriented programming course that provides instructions and examples for 3 lab activities to help students understand the differences between procedural and object oriented programming approaches. The activities demonstrate how object oriented programming bundles data and behaviors into objects that can be reused, compared to procedural programming which separates data and behaviors into independent lists of instructions. Completing the activities will help students understand the advantages of using object oriented programming over procedural programming.

Uploaded by

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

LAB Manual: Course: CSC241-Object Oriented Programming

The document is a lab manual for an object oriented programming course that provides instructions and examples for 3 lab activities to help students understand the differences between procedural and object oriented programming approaches. The activities demonstrate how object oriented programming bundles data and behaviors into objects that can be reused, compared to procedural programming which separates data and behaviors into independent lists of instructions. Completing the activities will help students understand the advantages of using object oriented programming over procedural programming.

Uploaded by

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

LAB

MANUAL
Course: CSC241-Object Oriented Programming

Department of Computer Science

Learning Procedure
1) Stage J (Journey inside-out the concept)
2) Stage a1 (Apply the learned)
3) Stage v (Verify the accuracy)
4) Stage a2 (Assess your work)

CCS241 –Lab Manual 1


COMSATS Institute of Information Technology (CIIT)
Islamabad
LAB # 01

Statement Purpose:
Objective of this lab is to make students understand the difference between object oriented and
procedural approaches to programming

Activity Outcomes:
The student will understand the advantages of using OOP

The student will understand the difference between procedural and object oriented approaches

Instructor Note:
The Students should have knowledge about structured programming.

CCS241 –Lab Manual 2


1) Stage J
(Journey)
Introduction
Procedural programming uses a list of instructions to tell the computer what to do step-
by-step. Procedural programming relies on procedures, also known as routines or
subroutines. A procedure contains a series of computational steps to be carried out.
Procedural programming is intuitive in the sense that it is very similar to how you would
expect a program to work. If you want a computer to do something, you should provide
step-by-step instructions on how to do it. It is, therefore, no surprise that most of the early
programming languages are all procedural. Examples of procedural languages include
Fortran, COBOL and C, which have been around since the 1960s and 70s.

Object-oriented programming, or OOP, is an approach to problem-solving where all


computations are carried out using objects. An object is a component of a program that
knows how to perform certain actions and how to interact with other elements of the
program. Objects are the basic units of object-oriented programming. A simple example
of an object would be a person. Logically, you would expect a person to have a name.
This would be considered a property of the person. You would also expect a person to be
able to do something, such as walking. This would be considered a method of the person.
A method in object-oriented programming is like a procedure in procedural programming.
The key difference here is that the method is part of an object. In object-oriented
programming, you organize your code by creating objects, and then you can give those
objects properties and you can make them do certain things.

One of the most important characteristics of procedural programming is that it relies on


procedures that operate on data - these are two separate concepts. In object-oriented
programming, these two concepts are bundled into objects. This makes it possible to
create more complicated behavior with less code. The use of objects also makes it
possible to reuse code. Once you have created an object with more complex behavior, you
can use it anywhere in your code.

2) Stage a1 (apply)

Lab Activities:

Activity 1:
The example demonstrates the difference in approach if we want to find the circumference of
circle.
Solution:
Procedural Approach Object Oriented Approach
Public class Circle{ Public class Circle{
int radius; Private int radius;
Public void setRadius(int r) Public void setRadius(int r)
{ radius = r;} { radius = r;}
Public void showCircumference() Public void showCircumference()
{ {
double c = 2*3.14*radius; double c = 2*3.14* radius;
System.out.println(“Circumferenceis”+ c); System.out.println(“Circumference is”+
} c);
Public static void main() }
{ }
setRadius(5); Public class runner
showCircumference(); {
//output would be 31.4 Public static void main()
setRadius(10); {
showCircumference(); Circle c1= new circle();
// output would be 62.8 c1.setRadius(5);
} c1.showCircumference();
} //output would be 31.4; it belongs to c1
Circle c2= new circle();
c2.setRadius(10);
c2.showCircumference();
//output would be 62.8; it belongs to c2
}
}

Activity 2:
The example demonstrates the difference in approach if we want to model the concept of a
Book. In object Oriented approach the concept can be defined once and then reused in form
of different objects.

Procedural Approach Object Oriented Approach


Public class Book{ Public class Book{
string title; Private string title;
double price; Private double price;
int noOfPages; Private int noOfPages;

Public void setTitle(string t) Public void setTitle(string t)


{ title = t;} { title = t;}
Public void setPrice (double p) Public void setPrice (double p)
{ price = p;} { price = p;}
Public void setNoOfPages (int n) Public void setNoOfPages (int n)
{ noOfPages = n;} { noOfPages = n;}
Public void display() Public void display()
{ {
System.out.println(“BookTitle”+ title + “ BookPrice
System.out.println(“BookTitle”+
“ + price + “BookPages” + title
noOfPages);
+ “ BookPrice “ + p
} }
}
Public static void main()
{ Public class runner
setTitle (“OOP”); setPrice (200); {
setNoOfPages (500); display(); Public static void main()
} {
Book b1= new Book(); b1.setTitle (“OOP”); b1.setPrice (2
//output belongs to b1
}
Book b2= new Book(); b2.setTitle (“ICP”); b2..setPrice (15
b2.setNoOfPages (350); b2.display();

//output belongs to b2
}
}

Activity 3:
The example demonstrates the difference in approach if we want to model the concept of an
Account. Again we can see that in object Oriented approach the concept can be defined once
and then reused uniquely by different objects.

Procedural Approach Object Oriented Approach


Public class Account{ Public class Account{
double balance; double balance;
Public void setBalance(double b) Public void setBalance(double b)
{ balance = b;} { balance = b;}
Public void showBalance() Public void showBalance()
{ {
System.out.println(“Balance is”+ balance); System.out.println(“Balance is”+
} balance);
Public static void main() }
{ }
setBalance (5000); Public class runner
showBalance (); // output would be 5000 {
} Public static void main()
} {
Account a1= new Account (); a1.setBalance(2500); a1.showBalance();
//output would be2500; it belongs to a1

Account a2= new Account (); a2.setBalance(5000); a2.showBalance();


//output would be 5000; it belongs to a2

}
}

You might also like