Lab 02
Lab 02
10
1) Useful Concepts
Data Abstraction
Abstraction is the process of recognizing and focusing on important characteristics of a situation
or object and leaving/filtering out the un-wanted characteristics of that situation or object. For
example a person will be viewed differently by a doctor and an employer.
A doctor sees the person as patient. Thus he is interested in name, height, weight, age, blood
group, previous or existing diseases etc of a person.
An employer sees a person as an employee. Therefore, employer is interested in name, age,
health, degree of study, work experience etc of a person.
Class and Object:
The fundamental idea behind object-oriented languages is to combine into a single unit both data
and the functions that operate on that data. Such a unit is called an object. A class serves as a
plan, or blueprint. It specifies what data and what functions will be included in objects of that
class. An object is often called an ―instance of a class.
Instance Variables and Methods:
Instance variables represent the characteristics of the object and methods represent the behavior
of the object. For example length & width are the instance variables of class Rectangle and
Calculatearea() is a method.
Instance variables and methods belong to some class, and are defined inside the class to which
they belong.
Syntax:
public class Class_Name
{
Instance_Variable_Declaration_1
Instance_Variable_Declaration_2
. . .
Instance_Variable_Declaration_Last
Method_Definition_1 Method_Definition_2
. . .
Method_Definition_Last
}
Constructors:
It is a special function that is automatically executed when an object of that class is created. It has
no return type and has the same name as that of the class. It is normally defined in classes to
11
initialize data members. A constructor with no parameters is called a no-argument constructor. A
constructor may contain arguments which can be used for initiation of data members.
Syntax:
class_name( )
{
Public class_name()
{
//body
}
Activity 1:
The following example shows the declaration of class Rectangle. It has two data
members that represent the length and width of rectangle. The method calculateArea
will return the area of rectangle. The runner class will create an object of Rectangle
class and area function will be called.
Solution:
12
class Rectangle {
Activity 2:
The following example demonstrates the use of constructors
Solution:
class Rectangle {
public Rectangle() {
length = 5;
width = 2;
}
13
length = l;
width = w;
}
Activity 3:
The following example shows the declaration of class Point. It has two data members that represent
the x and y coordinate. Create two constructors and a function to move the point. The runner class
will create an object of Point class and move function will be called.
Solution:
class Point {
private int x;
private int y;
public Point() {
x = 1;
14
y = 2;
}
15
p1.display();
Lab Task 1
Create a class circle class with radius as data member. Create two constructors (no argument, and two
arguments) and a method to calculate Circumference.
Lab Task 2
Create a class Account class with balance as data member. Create two constructors (no argument, and
two arguments) and methods to withdraw and deposit balance.
Lab Task 3
Create a class ―Distance‖ with two constructors (no argument, and two argument), two data members (feet
and inches). Also create display function which displays all data members.
Lab Task 4
Write a class Marks with three data members to store three marks. Create two constructors and a method
to calculate and return the sum.
Lab Task 5
Write a class Time with three data members to store hr, min and seconds. Create two constructors and
apply checks to set valid time. Also create display function which displays all data members.
16