This is a Tutorial site on BlueJ for ICSE and ISC Students. Programs on encapsulation, inheritance, polymorphism, loop, function, string, array etc are posted here with technical analysis of the program. Solved ICSE and ISC guess computer application papers are posted here.
Wednesday, December 30, 2009
ICSE sample papers on BlueJ (2)
Today again I will discuss on some questions on icse computer application paper.
public void show()
{
int
a=5,b=6,c=7,d;
if(a < c && a < b)
{
d=a;
}
}
Logical && operator means a number of conditions can be combined using && operator and is all the conditions are true then the block will be executed.
In java function can be called using two techniques, one of them is pass by reference or call by reference. Here objects are passed to a function as argument and the actual argument object in the function definition refers the passed object. Any modification done on the data members of the actual argument object causes a change in the original object.
Both indexOf() and valueOf() are static functions of String class.
indexOf() function takes a string or character as argument and returns it’s first occurance location . So the return type of this function is int value. This function has several overloaded versions.
valueOf() function takes any non string value as argument and returns it’ string version.
A class Number has been defined to find the frequency of each digit present in it and the sum of the digits.
Some of the members of the class Number are given below:
Class name Number
Data member num – long integer type
Member functions:
Number( ) constructor to assign 0 to num
Number(long a) constructor to assign a to num
void digitFrequency( ) to find the frequency of each digit and to display it.
int sumDigits( ) to returns the sum of the digits of the number.
Specify the class Number giving the details of the two constructors and functions void digitFrequency( ) and
int sumDigits( ). You do not need to write the main function.
class Number
{
private long num;
public Number()
{
num=0;
}
public Number(long a)
{
num=a;
}
public void digitFrequency()
{
int one,two,three,four,five,six,seven,eight,nine,zero;
one=0;
two=0;
three=0;
four=0;
five=0;
six=0;
seven=0;
eight=0;
nine=0;
zero=0;
for(long i=num;i > 0;i=i/10)
{
if(i%10==0)
zero++;
else if(i%10==1)
one++;
else if(i%10==2)
two++;
else if(i%10==3)
three++;
else if(i%10==4)
four++;
else if(i%10==5)
five++;
else if(i%10==6)
six++;
else if(i%10==7)
seven++;
else if(i%10==8)
eight++;
else if(i%10==9)
nine++;
}
System.out.println("Zero Digit="+zero);
System.out.println("one Digit="+one);
System.out.println("Two Digit="+two);
System.out.println("Three Digit="+three);
System.out.println("Four Digit="+four);
System.out.println("Five Digit="+five);
System.out.println("Six Digit="+six);
System.out.println("Seven Digit="+seven);
System.out.println("Eight Digit="+eight);
System.out.println("Nine Digit="+nine);
}
public int sumDigits()
{
int sum=0;
for(long i=num;i > 0;i=i/10)
{
sum+=i%10;
}
return sum;
}
}
There is class Ascending that contains integers that are already arranged in ascending order. Some of the member functions of Ascending are given below:
Class name Ascending
Data member
int a[ ] an array of integers sorted in ascending order.
int size number of integers in the array
Member function
Ascending( int n) constructor to create an Ascending list of size n
void displayList( ) to display the list of integers
Ascending merge(Ascending a1) to merge the ascending list a1 with the current Ascending list object and return a third Ascending list which is also sorted in ascending order.
Important: While generating the final Ascending list both the original Ascending lists must be scanned only once. Elements common to the two list should appear only once in the third Ascending list.
Specify the class Ascending giving details of the function void displayList( ) and Ascending merge(Ascending a1) only.
import java.io.*;
class Ascending
{
private int size,a[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public Ascending(int n)throws Exception
{
size=n;
a=new int[size];
for(int i=0;i < size;i++)
{
System.out.println("Value=");
a[i]=Integer.parseInt(br.readLine());
}
}
public Ascending()
{
}
public void displayList()
{
for(int i=0;i < size;i++)
{
System.out.println(a[i]+ " ");
}
}
public Ascending merge(Ascending a1)throws Exception
{
Ascending temp=new Ascending();
temp.size=size+a1.size;
temp.a=new int[temp.size];
int i=0,j=0,x=0;
while(i < size && j < a1.size)
{
if(a[i] < a1.a[j])
{
temp.a[x++]=a[i++];
}
else if(a1.a[j] < a[i])
{
temp.a[x++]=a1.a[j++];
}
else
{
temp.a[x++]=a1.a[j];
j++;
i++;
}
}
if(i < size)
{
for(;i < size;i++)
{
temp.a[x++]=a[i];
}
}
else
{
for(;j < a1.size;j++)
{
temp.a[x++]=a1.a[j];
}
}
return temp;
}
}
Show the use of logical operator && with an example.
public void show()
{
int
a=5,b=6,c=7,d;
if(a < c && a < b)
{
d=a;
}
}
Logical && operator means a number of conditions can be combined using && operator and is all the conditions are true then the block will be executed.
Explain the term – “pass by reference”
In java function can be called using two techniques, one of them is pass by reference or call by reference. Here objects are passed to a function as argument and the actual argument object in the function definition refers the passed object. Any modification done on the data members of the actual argument object causes a change in the original object.
Differentiate between indexOf() and valueOf() functions.
Both indexOf() and valueOf() are static functions of String class.
indexOf() function takes a string or character as argument and returns it’s first occurance location . So the return type of this function is int value. This function has several overloaded versions.
valueOf() function takes any non string value as argument and returns it’ string version.
Program to find the frequency of each digit in a number and the sum of the digits
A class Number has been defined to find the frequency of each digit present in it and the sum of the digits.
Some of the members of the class Number are given below:
Class name Number
Data member num – long integer type
Member functions:
Number( ) constructor to assign 0 to num
Number(long a) constructor to assign a to num
void digitFrequency( ) to find the frequency of each digit and to display it.
int sumDigits( ) to returns the sum of the digits of the number.
Specify the class Number giving the details of the two constructors and functions void digitFrequency( ) and
int sumDigits( ). You do not need to write the main function.
class Number
{
private long num;
public Number()
{
num=0;
}
public Number(long a)
{
num=a;
}
public void digitFrequency()
{
int one,two,three,four,five,six,seven,eight,nine,zero;
one=0;
two=0;
three=0;
four=0;
five=0;
six=0;
seven=0;
eight=0;
nine=0;
zero=0;
for(long i=num;i > 0;i=i/10)
{
if(i%10==0)
zero++;
else if(i%10==1)
one++;
else if(i%10==2)
two++;
else if(i%10==3)
three++;
else if(i%10==4)
four++;
else if(i%10==5)
five++;
else if(i%10==6)
six++;
else if(i%10==7)
seven++;
else if(i%10==8)
eight++;
else if(i%10==9)
nine++;
}
System.out.println("Zero Digit="+zero);
System.out.println("one Digit="+one);
System.out.println("Two Digit="+two);
System.out.println("Three Digit="+three);
System.out.println("Four Digit="+four);
System.out.println("Five Digit="+five);
System.out.println("Six Digit="+six);
System.out.println("Seven Digit="+seven);
System.out.println("Eight Digit="+eight);
System.out.println("Nine Digit="+nine);
}
public int sumDigits()
{
int sum=0;
for(long i=num;i > 0;i=i/10)
{
sum+=i%10;
}
return sum;
}
}
Program on merging two ascending lists
There is class Ascending that contains integers that are already arranged in ascending order. Some of the member functions of Ascending are given below:
Class name Ascending
Data member
int a[ ] an array of integers sorted in ascending order.
int size number of integers in the array
Member function
Ascending( int n) constructor to create an Ascending list of size n
void displayList( ) to display the list of integers
Ascending merge(Ascending a1) to merge the ascending list a1 with the current Ascending list object and return a third Ascending list which is also sorted in ascending order.
Important: While generating the final Ascending list both the original Ascending lists must be scanned only once. Elements common to the two list should appear only once in the third Ascending list.
Specify the class Ascending giving details of the function void displayList( ) and Ascending merge(Ascending a1) only.
import java.io.*;
class Ascending
{
private int size,a[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public Ascending(int n)throws Exception
{
size=n;
a=new int[size];
for(int i=0;i < size;i++)
{
System.out.println("Value=");
a[i]=Integer.parseInt(br.readLine());
}
}
public Ascending()
{
}
public void displayList()
{
for(int i=0;i < size;i++)
{
System.out.println(a[i]+ " ");
}
}
public Ascending merge(Ascending a1)throws Exception
{
Ascending temp=new Ascending();
temp.size=size+a1.size;
temp.a=new int[temp.size];
int i=0,j=0,x=0;
while(i < size && j < a1.size)
{
if(a[i] < a1.a[j])
{
temp.a[x++]=a[i++];
}
else if(a1.a[j] < a[i])
{
temp.a[x++]=a1.a[j++];
}
else
{
temp.a[x++]=a1.a[j];
j++;
i++;
}
}
if(i < size)
{
for(;i < size;i++)
{
temp.a[x++]=a[i];
}
}
else
{
for(;j < a1.size;j++)
{
temp.a[x++]=a1.a[j];
}
}
return temp;
}
}
Sunday, December 20, 2009
ICSE sample papers on BlueJ(1)
Today I will discuss few basic questions on BlueJ.
Question 1
(A) Give the output of the following program segment.
int x=0;
do
{
if(x< 3)
{
x+=2;
System.out.println(x);
continue;
}
else
{
System.out.println(++x);
break;
}
}
while(x<10);
Output :
2
4
5
Explanation of the output
Initially value of x is 0, so within the loop body it enters the if block and the value becomes 2 (x+=2 means x=x+2). Thus the first output is 2.
After that the continue statement is encountered and again the if block is executed. So the second output is 4.
Next the control skips the if block and within else block the expression ++x with println() function means x is incremented by 1 first and then the value (5) is displayed. The break statement then causes premature termination of the loop.
The program has been written to determine a natural number n is prime or not. There are five places in the code marked by ?1?, ?2?, ?3?, ?4?, ?5? which must be replaced by expressions or statements so that the program works correctly.
public static void main(String args[])throws IOException
{
InputStrreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
int n,i,c=0;
System.out.println(”Enter No”);
n=Integer.parseInt(br.readLine());
for(i=1;?1?;i++)
{
if(?2?= =0)
{
?3?;
?4?;
}
}
if(?5?)
System.out.println(”Prime No”+n);
}
(i) What is the expression or statement at ?1? [1]i < n
(ii) What is the expression or statement at ?2? [1]
(iii) n%i+1 (because loop control variable I starts with 1)
(iv) What is the expression or statement at ?3? [1]C=1
(v) What is the expression or statement at ?4? [1]break
(vi) What is the expression or statement at ?5? [1]C==0
Question 3
Class MyArray contains an array of n intergers(n<=100) that are already arranged in ascending order. The index of the array elements very from 0 to n-1. Some of the member functions of MyArray are given below:
Class name: MyArray
Data members arr-an array of n integers
n-size of the array
Member functionsMyArray( ) constructor to initialize n=nn and the array arr
void readArray( ) reads n integers that are arranged in ascending order
void displayArray( ) display n integers
int binarySearch(int value) searches for the value in the array using the binary search technique. It returns the subscript of the array element if the value is found, otherwise it returns –999.
(a) Specify the class MyArray giving the details of the void displayArray( ) and int binarySearch(int value) only. You may assume that the other functions are written for you. You do not need to write the main function.
(b) What change would be necessary in the function binarySearch(int value). If the given array was arranged in descending order?
(c) When will binary search technique fail to work?
import java.io.*;
class MyArray
{
private int n;
private int arr[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
MyArray(int m)
{
n=m;arr=new int[n];
}
public void readArray()throws Exception{
for(int i=0;i < n;i++)
{
System.out.println("Value=");
arr[i]=Integer.parseInt(br.readLine());
}
}
public void displayArray()
{
for(int i=0;i < n;i++)
{System.out.println(arr[i]);
}
}
public int binarySearch(int value)
{
int i,j,mid;i=0;j=n-1;
while(i<=j)
{
mid=(i+j)/2;
if(arr[mid]==value)
{
break;
}
else if(arr[mid] < value)
{
i=mid+1;
}
else
{
j=mid-1;
}
}
if(i < =j)return 1;
else
return 0;
}
}
For Question (b) Simply change the binarySearch() as follows
public int binarySearch(int value)
{
int i,j,mid;
i=0;
j=n-1;
while(j > =i)
{
mid=(i+j)/2;
if(arr[mid]==value)
{
break;
}
else if(arr[mid] > value)
{
i=mid+1;
}
else
{
j=mid-1;
}
}
if(i < =j)
return 1;
else
return 0;
}
}
Question C, The basic condition for binary searching is that the list must be sorted and the middle element of the list can be directly accessed. In array this is possible, but in case of linked list , the middle element can not be accessed directly. So in linked list searching binary search can not be applied.
Question 1
(A) Give the output of the following program segment.
int x=0;
do
{
if(x< 3)
{
x+=2;
System.out.println(x);
continue;
}
else
{
System.out.println(++x);
break;
}
}
while(x<10);
Output :
2
4
5
Explanation of the output
Initially value of x is 0, so within the loop body it enters the if block and the value becomes 2 (x+=2 means x=x+2). Thus the first output is 2.
After that the continue statement is encountered and again the if block is executed. So the second output is 4.
Next the control skips the if block and within else block the expression ++x with println() function means x is incremented by 1 first and then the value (5) is displayed. The break statement then causes premature termination of the loop.
The program has been written to determine a natural number n is prime or not. There are five places in the code marked by ?1?, ?2?, ?3?, ?4?, ?5? which must be replaced by expressions or statements so that the program works correctly.
public static void main(String args[])throws IOException
{
InputStrreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
int n,i,c=0;
System.out.println(”Enter No”);
n=Integer.parseInt(br.readLine());
for(i=1;?1?;i++)
{
if(?2?= =0)
{
?3?;
?4?;
}
}
if(?5?)
System.out.println(”Prime No”+n);
}
(i) What is the expression or statement at ?1? [1]i < n
(ii) What is the expression or statement at ?2? [1]
(iii) n%i+1 (because loop control variable I starts with 1)
(iv) What is the expression or statement at ?3? [1]C=1
(v) What is the expression or statement at ?4? [1]break
(vi) What is the expression or statement at ?5? [1]C==0
Question 3
Class MyArray contains an array of n intergers(n<=100) that are already arranged in ascending order. The index of the array elements very from 0 to n-1. Some of the member functions of MyArray are given below:
Class name: MyArray
Data members arr-an array of n integers
n-size of the array
Member functionsMyArray( ) constructor to initialize n=nn and the array arr
void readArray( ) reads n integers that are arranged in ascending order
void displayArray( ) display n integers
int binarySearch(int value) searches for the value in the array using the binary search technique. It returns the subscript of the array element if the value is found, otherwise it returns –999.
(a) Specify the class MyArray giving the details of the void displayArray( ) and int binarySearch(int value) only. You may assume that the other functions are written for you. You do not need to write the main function.
(b) What change would be necessary in the function binarySearch(int value). If the given array was arranged in descending order?
(c) When will binary search technique fail to work?
import java.io.*;
class MyArray
{
private int n;
private int arr[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
MyArray(int m)
{
n=m;arr=new int[n];
}
public void readArray()throws Exception{
for(int i=0;i < n;i++)
{
System.out.println("Value=");
arr[i]=Integer.parseInt(br.readLine());
}
}
public void displayArray()
{
for(int i=0;i < n;i++)
{System.out.println(arr[i]);
}
}
public int binarySearch(int value)
{
int i,j,mid;i=0;j=n-1;
while(i<=j)
{
mid=(i+j)/2;
if(arr[mid]==value)
{
break;
}
else if(arr[mid] < value)
{
i=mid+1;
}
else
{
j=mid-1;
}
}
if(i < =j)return 1;
else
return 0;
}
}
For Question (b) Simply change the binarySearch() as follows
public int binarySearch(int value)
{
int i,j,mid;
i=0;
j=n-1;
while(j > =i)
{
mid=(i+j)/2;
if(arr[mid]==value)
{
break;
}
else if(arr[mid] > value)
{
i=mid+1;
}
else
{
j=mid-1;
}
}
if(i < =j)
return 1;
else
return 0;
}
}
Question C, The basic condition for binary searching is that the list must be sorted and the middle element of the list can be directly accessed. In array this is possible, but in case of linked list , the middle element can not be accessed directly. So in linked list searching binary search can not be applied.
Monday, December 14, 2009
Operators in BlueJ
Java provides a rich operator environment. The operators are mainly divided into the following four categories.
1. arithmetic
2. bitwise
3. relational
4. logical
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
-- Decrement
The basic arithmetic operators (+,-,*, /) works in the universal style for all numeric values.
N.B. Division operator while applied on integer values no fractional part will be available.
The Modulus operator, % returns the remainder of a division operation. In Java, Modulus operator can be applied to floating point types as well as integer types. (In C and C++, Modulus operator is applicable to integer values only.)
Example BlueJ
class Mod
{
private int x;
private double y;
public void show ()
{
x=13;
y=15.5;
System. out.println( “ X mod by 10=” + x%10);
System. out.println( “ Y
mod by 10=” + y%10);
}
}
Output:
X mod by 10=3
Y mod by 10=5.5
Java provides special operators to combine an arithmetic operator with an assignment operator. Assignment operator is used to assign a value or the result of an expression to a variable. All of us know the use of '=' operator. When we use the statement int a=10, this means that the assignment operator will assign the value at its right side on the variable at its left side. In addition to that Java offers a set of shorthand arithmetic assignment operators
. Consider the example: a=a+b;
The above statement adds 'b' to 'a' and assigns the result on 'a’. We can write the above statement as: a+=b; both a=a+b and a+=b are same. Similarly we can use - =, * =, / =, % =
The ++ and – are java’s increment and decrement operators. Let int a=10; if we want to increase ‘a’ by, we can write a=a+1; or a+=1;
The same statement can be written as a++ or ++a. In same style we can write --a or a-- to decrease the value of a by 1. These are called increment and decrement operators.
When the operator is used before the variable, it is called prefix operator and when used after the operator, it is postfix operator.
While used in a single statement, prefix and postfix operator means the same – increment or decrement by 1 but in expression they differ. If postfix operator is used in an expression, the expression will be executed first and after that the effect of the operator will take place. In case of prefix operator, the operator will act first and then the expression.
Example
class Incre
{
private int a=10;
public void show()
{
System. out.println(“ After postfix expression=” + a++);
System. out.println(“ Now a=” + a);
System. out.println(“ After prefix expression=” + ++a);
System. out.println(“ Now a=” + a);
}
}
output
After postfix expression=10
Now a=11
After prefix expression=12
Now a=12
1. arithmetic
2. bitwise
3. relational
4. logical
Arithmetic operators in Java
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
-- Decrement
The basic arithmetic operators (+,-,*, /) works in the universal style for all numeric values.
N.B. Division operator while applied on integer values no fractional part will be available.
Modulus / modulo operator
The Modulus operator, % returns the remainder of a division operation. In Java, Modulus operator can be applied to floating point types as well as integer types. (In C and C++, Modulus operator is applicable to integer values only.)
Example BlueJ
class Mod
{
private int x;
private double y;
public void show ()
{
x=13;
y=15.5;
System. out.println( “ X mod by 10=” + x%10);
System. out.println( “ Y
mod by 10=” + y%10);
}
}
Output:
X mod by 10=3
Y mod by 10=5.5
Arithmetic Assignment Operator in BlueJ
Java provides special operators to combine an arithmetic operator with an assignment operator. Assignment operator is used to assign a value or the result of an expression to a variable. All of us know the use of '=' operator. When we use the statement int a=10, this means that the assignment operator will assign the value at its right side on the variable at its left side. In addition to that Java offers a set of shorthand arithmetic assignment operators
. Consider the example: a=a+b;
The above statement adds 'b' to 'a' and assigns the result on 'a’. We can write the above statement as: a+=b; both a=a+b and a+=b are same. Similarly we can use - =, * =, / =, % =
Increment and Decrement Operators in Java
The ++ and – are java’s increment and decrement operators. Let int a=10; if we want to increase ‘a’ by, we can write a=a+1; or a+=1;
The same statement can be written as a++ or ++a. In same style we can write --a or a-- to decrease the value of a by 1. These are called increment and decrement operators.
When the operator is used before the variable, it is called prefix operator and when used after the operator, it is postfix operator.
While used in a single statement, prefix and postfix operator means the same – increment or decrement by 1 but in expression they differ. If postfix operator is used in an expression, the expression will be executed first and after that the effect of the operator will take place. In case of prefix operator, the operator will act first and then the expression.
Example
class Incre
{
private int a=10;
public void show()
{
System. out.println(“ After postfix expression=” + a++);
System. out.println(“ Now a=” + a);
System. out.println(“ After prefix expression=” + ++a);
System. out.println(“ Now a=” + a);
}
}
output
After postfix expression=10
Now a=11
After prefix expression=12
Now a=12
Friday, December 11, 2009
Few Fundamental Questions on icse java programs
Name any two library packages of java .
Two library packages of Java are lang and io package. lang package is the largest package among the all java library packages. Classes like System, String etc are inbuilt classes of lang package. BufferedReader is one of the classes in io package.
State two main features of Object Oriented Programming languages
Two main feature of Object Oriented Programming languages are Encapsulation and Inheritance.
What is meant by a user defined data type?
In java library packages numbers of classes are defined. They are all pre defined classes. Apart from those predefined classes we can define our own class using the keyword class. Within the class body we normally declare data members and define function members. These classes are called user defined data type.
Give an example of syntax error
Syntax error means the error which occurs during compilation time.
Example is System. out. println (“Error…Colon is placed instead of semi colon.”):
What is the scope of the keyword protected in accessing functions?
Excepting sub classes, no other classes can access protected functions of any class.
What is a parameterized constructor?
Parameterized constructor means the constructor which takes value(s) as argument when invoked to create object. Such constructors normally initialize the data members of the objects while the object is created.
What is the use of static in the main function?
Static keyword in function means the function is a class member, not an instance member. A class function or static function means whose existence does not depend on the creation of the object and such member can be used before creating the object. As to run a program, we have to execute the main function first, main function is a static function.
Friday, December 4, 2009
Inheritance in Bluej for icse schools
This is a mechanism by which one object acquires the properties of another object of different class. Inheritance is probably the most powerful feature of object oriented programming. Inheritance is the process of creating new classes, called derived or sub classes from existing classes. The existing classes are called base or super classes. In OOP, the concept of inheritance in object oriented programming provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one.
Inheritance in object oriented programming is accomplished by using the keyword extends,
public class Animal{
}
public class Mammal extends Animal{
}
public class Reptile extends Animal{
}
public class Dog extends Mammal{
}
Now based on the above example, In Object Oriented terms following are true:
Animal is the superclass of Mammal class.
Animal is the superclass of Reptile class.
Mammal and Reptile are sub classes of Animal class.
Dog is the subclass of both Mammal and Animal classes.
Now if we consider the relationship we can say:
Mammal is an Animal
Reptile is an Animal
Dog is an Mammal
Hence : Dog is an Animal as well
Inheritance in object oriented programming is accomplished by using the keyword extends,
public class Animal{
}
public class Mammal extends Animal{
}
public class Reptile extends Animal{
}
public class Dog extends Mammal{
}
Now based on the above example, In Object Oriented terms following are true:
Animal is the superclass of Mammal class.
Animal is the superclass of Reptile class.
Mammal and Reptile are sub classes of Animal class.
Dog is the subclass of both Mammal and Animal classes.
Now if we consider the relationship we can say:
Mammal is an Animal
Reptile is an Animal
Dog is an Mammal
Hence : Dog is an Animal as well
Tuesday, December 1, 2009
What is Encapsulation in BlueJ
Encapsulation is one of the four fundamental features in object oriented programming. The others are inheritance, polymorphism, and abstraction.
Encapsulation is the mechanism of binding together code and the data members on which it (code) works thus preventing them from unauthorized access. Encapsulation can be described as a protective wrapper that prevents the code and data from being accessed by other code defined outside the class. Access to the data and code inside the wrapper is tightly controlled by a well defined interface. To relate this to the real world, we can consider our TV set. It encapsulates number of physical components and functions inside it. We can communicate with the components using the functions defined in the set. Thus we can operate the TV set without bothering about the complexity of the TV set architecture. The same idea is applicable to programming also. We can use the encapsulated codes to get our job done without knowing details about it.
In Java the basis of encapsulation is the class. A class defines the structure and behavior – data and code that will be shared by a set of objects. Each object of a given class contains the structure and behavior defined by the class. For this reason objects are known as instance of a class.
Purpose of a class is to encapsulate complexity.
Let us look at an example
public class Test{
private String name;
private int age;
public int getAge(){
return age;
}
public String getName(){
return name;
}
public void setAge( int newAge){
age = newAge;
}
public void setName(String newName){
name = newName;
}
}
The public functions can be accessed from outside the class but the private data members can not be accessed by codes which reside outside the class. Therefore any class that wants to access the variables should access them through these functions defined in the class without knowing details of the statements written inside the function body. This is encapsulation.
Any doubt? Put Comments.
Encapsulation is the mechanism of binding together code and the data members on which it (code) works thus preventing them from unauthorized access. Encapsulation can be described as a protective wrapper that prevents the code and data from being accessed by other code defined outside the class. Access to the data and code inside the wrapper is tightly controlled by a well defined interface. To relate this to the real world, we can consider our TV set. It encapsulates number of physical components and functions inside it. We can communicate with the components using the functions defined in the set. Thus we can operate the TV set without bothering about the complexity of the TV set architecture. The same idea is applicable to programming also. We can use the encapsulated codes to get our job done without knowing details about it.
In Java the basis of encapsulation is the class. A class defines the structure and behavior – data and code that will be shared by a set of objects. Each object of a given class contains the structure and behavior defined by the class. For this reason objects are known as instance of a class.
Purpose of a class is to encapsulate complexity.
Let us look at an example
public class Test{
private String name;
private int age;
public int getAge(){
return age;
}
public String getName(){
return name;
}
public void setAge( int newAge){
age = newAge;
}
public void setName(String newName){
name = newName;
}
}
The public functions can be accessed from outside the class but the private data members can not be accessed by codes which reside outside the class. Therefore any class that wants to access the variables should access them through these functions defined in the class without knowing details of the statements written inside the function body. This is encapsulation.
Any doubt? Put Comments.
Wednesday, November 25, 2009
What is Object Oriented Programming
Object Oriented Programming can be defined as a technique by which a computer program is designed and written around objects. In this article we will introduce some of the fundamental concepts behind OOP.
Object is the basic run-time entities in an Object Oriented Programming. Here problem is analyzed in terms of objects and nature of communication between them. When a program is executed, objects interact with each other without knowing the details of their data or code.
The real life example of an object can be anything. A car, pen, you, me all are objects.
A class is the template of objects. Classes act as some kind of blueprint of the objects created out of the class. A class defines the nature and behavior of the objects to be created. Nature of an object is defined by the data members and behavior is defined by the methods defined in the class.
Real life example of class is type of animals, type of birds etc.
In java class is created by using the keyword ‘class’. Here is an example of Java code that defines a class.
class MyClass
{
int i;
void setData()
{
i=10;
}
}
Here ‘int i’ is the data member and ‘void setData()’ is the method or function member.
In Java the following conventions are followed:
Class name starts with upper case character and method names starts with lower case character.
Concept of Constructor in object oriented programming is another interesting and important topic. Constructors are used to initialize properties and methods when an object is instantiated. Constructors are special type of methods whose execution is a must. While creating an object, constructor is invoked using ‘new’ operator.
Constructor has the following important features:
1. It’s name and the class name must be same
2. Constructor has no return type because the implicit return type of a constructor is the class type itself.
3. If Constructor is not defined in a class, compiler will supply default constructor which means a constructor with no argument and empty body.
4. It is the constructor’s job to initialize the internal state of an object making the object usable.
If you have any other suggestions, feel free and leave a comment
What is object in Object Oriented Programming
Object is the basic run-time entities in an Object Oriented Programming. Here problem is analyzed in terms of objects and nature of communication between them. When a program is executed, objects interact with each other without knowing the details of their data or code.
The real life example of an object can be anything. A car, pen, you, me all are objects.
What is Class in Object Oriented Programming
A class is the template of objects. Classes act as some kind of blueprint of the objects created out of the class. A class defines the nature and behavior of the objects to be created. Nature of an object is defined by the data members and behavior is defined by the methods defined in the class.
Real life example of class is type of animals, type of birds etc.
In java class is created by using the keyword ‘class’. Here is an example of Java code that defines a class.
class MyClass
{
int i;
void setData()
{
i=10;
}
}
Here ‘int i’ is the data member and ‘void setData()’ is the method or function member.
In Java the following conventions are followed:
Class name starts with upper case character and method names starts with lower case character.
What is Constructor
Concept of Constructor in object oriented programming is another interesting and important topic. Constructors are used to initialize properties and methods when an object is instantiated. Constructors are special type of methods whose execution is a must. While creating an object, constructor is invoked using ‘new’ operator.
Constructor has the following important features:
1. It’s name and the class name must be same
2. Constructor has no return type because the implicit return type of a constructor is the class type itself.
3. If Constructor is not defined in a class, compiler will supply default constructor which means a constructor with no argument and empty body.
4. It is the constructor’s job to initialize the internal state of an object making the object usable.
If you have any other suggestions, feel free and leave a comment
No comments:
Post a Comment