Friday, 29 May 2015

Object-Oriented Programming v/s Procedural Programming

All the programs consist of two elements: process and data.  There can be two different approaches depending upon whether our main focus is on processing or data. The procedural programming languages (C, FORTRAN, PASCAL, COBOL etc.) focus on the processing part i.e. they give more importance to “what is happening” in the system and less importance to data.

The object-oriented languages (Java, C++) focus on the data i.e. they give more importance to “who is being affected”. The procedural approach becomes less and less suitable as the programs become large and complex. The object-oriented programming languages were developed to overcome the limitations of the procedural programming languages. The object-oriented programs are relatively far less complex as compared to the similar programs written in procedural languages.

Object-oriented programs are organized around data (i.e. objects) and a set of well-defined
interfaces (public methods) to that data. Java is based on object-oriented paradigm. Java is almost pure object-oriented programming language. We have used the term “almost” as Java also supports Primitive Data Types due to performance reasons. The C++ is not a pure object-oriented language. C++ is an extension to C so it uses an approach, which is a mix of procedure-oriented approach and object-oriented approach.

The basic differences in the two approaches are summarized below:

(i) The object-oriented programs are data centric while the programs written in procedural
languages are process centric.

(ii) The object-oriented programs are organized around data (objects) so they model the real
world objects in a better way.

(iii) The degree of reusability and extensibility of code is very high in case of object-
oriented approach as compared to procedural approach. So code size is less.

(iv) The object-oriented programs are easier to maintain, as they are relatively less complex
and smaller in size.

(v) The object-oriented programs are based on the bottom-up design methodology while
the procedural programs are based on the top-down design methodology.

Java return Statement

The return statement is used to immediately return control from a called method to the calling method.

The syntax of the return statement is as follows:

return;

or

return expression;

The first form of return statement is used to simply transfer control from the called method to the calling method. The second form returns control as well as returns a value to the calling method as specified by the expression following the return statement.

Java continue Statement

The general form of simple continue statement is:

continue;

The unlabeled continue statement can be used inside loops (for, while and do-while) only. It prematurely stops the current iteration of the loop body and proceeds with the next iteration, if possible.

In the case of while and do-while loops, the rest of the body is skipped and the execution continues with the loop condition. In the case of for loop, the rest of the body is skipped and the execution continues with the increment/decrement expression.

This is similar to continue statement in C/C++.

Example: The linear search program is rewritten to make use of the continue statement.

import java.util.*;

class LinearSearch

{ public static void main(String args[]) throws IOException

{ int n, x, a[]; boolean found = false; String number;

Buffered Reader in

= new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the number of elements in the array: ");

number = in.readLine(); n = Integer.parseInt(number);

a = new int[n];

for(int i = 0; i < n; i++)// read the array elements

{ number = in.readLine();

a[i] = Integer.parseInt(number);

}

System.out.print("Enter the element to be searched in the array: ");

number = in.readLine(); x = Integer.parseInt(number);

int i = 0;

while(i < n && !found) // search the specified element

{

if(x = =  a[i]) // simple if statement

{

found = true;

continue;

}

i++;

}

if(found) // if-else statement

System.out.println("The number " + x + " is present in the list");

else

System.out.println("The number " + x + " is not in the list");
}
}

labeled continue Statement

The syntax of the labeled continue statement is as follows:

continue  <label>;

The labeled continue statement specifies the label of the enclosing loop to continue. The label need not correspond to the closest enclosing loop.

Example: The matrix search program is rewritten here to make use of the continue statement.

import java.util.*;

class SearchMatrix1

{ public static void main(String args[]) throws IOException

{

int i, j, x, row, col , matrix[][];  String number;  boolean found = false;

Buffered Reader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the number of rows in the matrix: ");

number = in.readLine(); row = Integer.parseInt(number);

System.out.print("Enter the number of columns in the matrix: ");

number = in.readLine(); col = Integer.parseInt(number);

matrix = new int[row][col];

for(i = 0; i < row; i++) //enter one element in one line

{

System.out.println("enter the elements of row " + (i+1));

for(j = 0; j < col; j++)

{ number = in.readLine();

}

}

System.out.print("Enter the element to be searched in the matrix: ");

number = in.readLine();

x = Integer.parseInt(number);

outerloop: for(i = 0; i < row && !found; i++)

{ for(j = 0; j < col; j++)

{ if(matrix[i][j] == x)

{ found = true;

continue outerloop;

}

}

}

// break will transfer control to the following statement

if(found)

System.out.println("The number "+ x + " is present in the matrix");

else

System.out.println("The number " + x + " is not in the matrix");

}

}

Java Jump Statements

Jump statements are used for breaking the control flow. Java supports the following jump statements:

  • break
  • labeled break
  • continue
  • labeled continue
  • return
Beside this the constructs used for exception handling can also be put into this category but they are discussed in a separate chapter.

break Statement

The general form of simple break statement is:

break;

The unlabeled break statement can be used inside loops (for, while and do-while) and switch statements. It terminates the containing statement and transfers the control outside the closest enclosing loop or switch statement. The rest of containing statement body is skipped and the execution continues after the containing statement.

This is similar to break statement in C/C++. Refer to examples of simple-if statement and switch statement discussed above for the use of break statement.

labeled break Statement

The syntax of the labeled break statement is as follows:

break  <label>;

The labeled beak statement is used to terminate the block whose label is specified in the break statement. Unlike simple break statement, you can terminate any block. For example, it is possible to terminate the outermost loop from inside a deeply nested for loop.

The break statement can also be used to terminate a simple block (i.e. the block need not be a loop or switch statement)

Example: The following program searches for a given element inside a matrix (2-D array) and makes use of the labeled break statement to come out of the outermost for loop as soon as the element is found.

import java.util.*;

class SearchMatrix

{ public static void main(String args[]) throws IOException

{ int i, j, x, row, col , matrix[][];

String number;

boolean found = false;

Buffered Reader in

= new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the number of rows in the matrix: ");

number = in.readLine();

row = Integer.parseInt(number);

System.out.print("Enter the number of columns in the matrix: ");

number = in.readLine();

col = Integer.parseInt(number);

matrix = new int[ row][col];

for(i = 0; i < row; i++) //enter one element in one line

{ System.out.println("enter the elements of row " + (i+1));

for(j = 0; j < col; j++)

{ number = in.readLine();

}

}

System.out.print("Enter the element to be searched : ");

number = in.readLine();

x = Integer.parseInt(number);

outerloop: for(i = 0; i < row; i++)

matrix[i][j]= Integer.parseInt(number);

{

for(j = 0; j < col; j++)

{

if(matrix[i][j] == x)

{ found = true;

}

 break outerloop;
}

}
}
if(found)

System.out.println("The number "+ x + " is present");

else

System.out.println("The number " + x + " is not present");
}
}

The use of labeled break statement in the above program is a substitute of the labeled goto statement of C/C++. This is a better alternative, as the program is more readable hence the labeled break is referred to as the civilized form of goto statement.

Java Iteration Statements (Loop Control Structure)

The Java supports the while, do-while and for iteration/loop control statements. The syntax is similar to C/C++. With JDK1.5, a second form of for was added that implements a “for-each” style loop.

while Statement

The general form of a while statement is:

while(condition)

loop body

Loop body may contain one or more statements. If loop body contains more than one statement then they must be enclosed between braces.

Example: The following program accepts one natural number say n as command line argument and then calculates and displays the sum of first n natural numbers.

class SumNaturalNumbers

{

public static void main(String a[])

{ int n, sum=0;

n = Integer.parseInt(a[0]); //command line argument

int i = 1;

sum = 0;

while (i <= n)

{

sum = sum + i;

i  = i + 1;

}

System.out.println("Sum of first " + n + " natural numbers is " + sum);

}

}

do-while Statement

The general form of a do-while statement is:

do

{

} while (condition)

loop body

Example: The following program accepts one integer number say n as command line argument and then calculates and displays the sum of its digits.

class SumOfDigits

{ public static void main(String a[])

{ int n, m, sum, digit;

n = Integer.parseInt(a[0]); //command line argument

m = n;

if(m < 0)

m = -m;

sum = 0;

do

{ digit = m % 10;

sum = sum + digit;

m = m / 10;

}while (m > 0);

System.out.println("Sum of digits of number " + n + " is: " + sum);

}

}

for Statement

The general form of a for statement is:

for(initialization; condition; increment/decrement)

loop body

Loop body may contain one or more statements. If loop body contains more than one statement then they must be enclosed between braces.

Example: The following program accepts one natural number say n as command line argument

and then calculates and displays the sum of first n natural numbers.

class SumNaturalNumbers1

{ public static void main(String a[])

{ int i, n, sum;

n = Integer.parseInt(a[0]); //command line argument

for(i = 1, sum = 0; i <= n; i++)

{

sum = sum + i;

}

System.out.println("Sum of first " + n + " natural numbers is " + sum);
}
}