OOP vs POP: Key Differences Explained
OOP vs POP: Key Differences Explained
All computer programs consist of two elements: code and data. Furthermore, a
program can be conceptually organized around its code or around its data. That is,
some programs are written around ―what is happening and others are written
around ―who is being affected. These are the two paradigms that govern how a
program is constructed.
Object-oriented programming organizes a program around its data (that is, objects)
and a set of well-defined interfaces to that data. An object-oriented program can be
characterized as data controlling access to code. As you will see, by switching the
controlling entity to data, you can achieve several organizational benefits.
There are many high level languages like COBOL, FORTRAN, PASCAL, C used
for conventional programming commonly known as POP.
POP basically consists of writing a list of instructions for the computer to follow, and
organizing these instructions into groups known as functions.
In a multi-function program, many important data items are placed as global so that
they may be accessed by all the functions. Each function may have its own local data.
Global data are more vulnerable to an in advent change by a function. In a large
program it is very difficult to identify what data is used by which function. In case we
need to revise an external data structure, we should also revise all the functions that
access the data. This provides an opportunity for bugs to creep in.
Drawback: It does not model real world problems very well, because functions are
actionoriented and do not really corresponding to the elements of the problem.
That is, an object a considered to be a partitioned area of computer memory that stores
data and set of operations that can access that data. Since the memory partitions are
independent, the objects can be used in a variety of different programs without
modifications.
Emphasis on data.
Programs are divided into what are known as objects.
Data structures are designed such that they characterize the objects.
Methods that operate on the data of an object are tied together.
Data is hidden.
Objects can communicate with each other through methods.
Reusability.
Follows bottom-up approach in program design.
Object
Class
Encapsulation
Abstraction
Inheritance
Polymorphism
Object:
Class:
Encapsulation:
Wrapping up of data and methods into a single unit is called as Data Encapsulation.
Abstraction:
Data Abstraction refers to the act of representing the essential features without
including the background details (or) explanations.
(or)
Inheritance:
Inheritance is the process by which objects of one class acquires the properties of
objects of another class.
In inheritance, the derived class (child class) obtains the characteristics of the parent
class.
Example:
Polymorphism:
Polymorphism is a greek term, which means an ability to take more than one form.
Example:
Consider the addition operator ‘+’. If we pass integers as operands, then the operator
will give the sum of 2 integers.
Here the operator + performs different operations depending on the type of data. This is
called as operator overloading.
Applications of OOP:
History of JAVA:
James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project
in June 1991. The small team of sun engineers called Green Team.
Initially it was designed for small, embedded systems in electronic appliances like set-
top boxes.
Firstly, it was called "Green talk" by James Gosling, and the file extension was .gt.
In 1993, it was named as “OAK”.
In 1995, Sun Micro Systems conducted a public conference and released JAVA’s first
version.
Many java versions have been released till now. The current stable release of Java is Java
SE 10.
1. Simple
2. Object Oriented
3. Robust
4. Architecture Neutral
5. Multithreaded
6. Interpreted and High Performance
7. Secure
8. Portable
9. Distributed
10. Dynamic
1. Simple:
JAVA language is designed to be very simple for programmers to learn
and use effectively.
Many of the confusing concepts in C/C++ like pointers, operator
overloading etc. are eliminated from JAVA language.
2. Object Oriented:
JAVA language supports all the object oriented programming principles.
JAVA is a pure Object Oriented Programming language.
In JAVA language, everything is represented as an object. Even primary
data types like integers, floating point numbers are also represented as
objects.
3. Robust:
Robust means strong.
JAVA is said to be robust language because of two reasons. They are
a) Memory Management
b) Exception Handling
a) Memory Management:
JAVA language provides implicit memory management. It avoids
writing much code of memory management by the programmer.
When JAVA software starts, it runs a garbage collector thread, it takes
care of garbage process.
Objects which are not required in further process of a program are
treated as garbage objects. Garbage collector removes garbage objects
while the execution is going on.
b) Exception Handling:
Exception is an abnormal condition in a program.
JAVA language provides an efficient mechanism for handling
exceptions without trashing the program.
4. Architecture Neutral:
JAVA compiler generates byte code that must be again converted into
machine code at execution time.
This machine code conversion can be done by JVM.
Byte code is architecture neutral. That means, any operating system can
execute the byte code without bothering its architecture.
5. Multithreaded:
JAVA language supports multithreaded programming which allows to
write programs that do many things simultaneously.
6. Interpreted and High Performance:
Generally languages like C and C++ are compiled languages but JAVA is
both compiled and interpreted language.
At compilation stage, the JAVA source code is converted into byte code.
At run time, the JVM converts byte code into machine understandable
code.
By the use of interpreter in JVM, JAVA language is slower in its execution.
To enhance the performance of the program, Just In Time(JIT) compiler is
used in JVM.
7. Secure:
By downloading C/C++ programs from internet, we obtain .exe files.
.exe file is the main source for getting virus into the system. So by
downloading C/C++ programs, there is a chance to get virus into our
systems.
By downloading JAVA programs from internet, we obtain .class files.
.class file is the solution for virus. So by downloading JAVA programs,
there is no way of getting virus into the system.
8. Portable:
Portability of JAVA comes from its byte code.
A .class file obtained from the compilation on one platform can be easily
ported or carried for execution on different platform.
9. Distributed:
JAVA language supports distributed programming so that we can easily
write the networking programs using JAVA language.
JAVA language internally supports 2 important protocols for network
programming i.e., TCP/IP.
TCP-Transmission Control Protocol
IP-Internet Protocol
10. Dynamic:
JVM carries lot of run time information of the program and also the
objects of the program.
JVM dynamically loads .class files.
Simple Program:
class Simple
{
public static void main (String args[])
{
[Link] (“Hello VIT”);
}
}
Here class is a keyword which is used for defining a class.
Simple is a name of the class.
To make main method available to outside of the class, we use public keyword.
static keyword specifies that the main method is called by the JVM without
instantiating the class.
void represents the main method does not return any value.
The parameters which are passed to main method are called as command line
arguments. In JAVA language, main method takes an array of string objects as
parameters.
System is the one of the standard (or) predefined class which is used to perform
some I/O operations.
[Link] represents the standard output device i.e., monitor.
[Link] represents the standard input device i.e., keyboard.
println is the method which is used to display the output on the output screen
line by line.
JVM Architecture:
Classloader Sub System:
Loading:
Bootstrap Class Loader is responsible for loading JAVA API classes which are
available in [Link] file.
Extension Class Loader is responsible for loading the classes from JRE’s
extension directories (jre/lib/ext)
Application Class Loader is responsible for loading the classes found on path
which maps to the class path environment variable.
If all the class loaders are failed to load the required class file then JVM will
generate “Class Not Found” exception.
Linking:
It is used to invoke the java code that initializes static variables to their proper
starting values.
Run time Data Areas:
Different areas into which the byte code is loaded are called as Run Time Data
Areas.
JVM creates 5 run time data areas to load the byte code. They are
a) Method area
b) Heap area
c) Java Stack area
d) PC register
e) Native Method stack
Method area:
All classes’ byte code is loaded and stored in this run time area. All static
variables are created and stored in this area.
Heap area:
This area can be used for storing all the objects that are created. It is the main
memory of JVM.
This can be expanded by its own depending on the object creation.
Method area and Heap area are sharable memory areas.
Java Stack area:
This area can be used for storing the information of the methods.
Java Stack can be considered as the combination of stack frames where every
frame contains the state of a single method.
In this run time area, JVM by default creates one thread i.e, main thread.
main thread is responsible to execute main method.
main thread is also responsible for creating objects in Heap area.
PC register:
This area will contain the address of next instruction that have to be executed.
Native Method Stack:
a) Interpreter
b) Just In Time (JIT) compiler
c) Garbage Collector
Interpreter is used to read the byte code and then execute the instructions.
JIT compiler is used to improve the performance.
Garbage Collector is responsible to destroy all the unused objects from Heap
area.
Comments in JAVA:
Comments are used to make the program more readable by adding the details of
the code.
There are three types of comments in Java.
The single-line comment is used to comment only one line of the code. It
is the widely used and easiest way of commenting the statements.
Single line comments starts with two forward slashes (//).
Syntax:
Example:
Syntax:
/*
This
is
multi line
comment
*/
Example:
3. Documentation Comment:
Syntax:
1. /**
2. *
3. *We can use various tags to depict the parameter
4. *or heading or author name
5. *We can also use HTML tags
6. *
7. */
In JAVA, there is a total of eight escape sequences that are described in the following
table.
Escape Description
Characters
Data types are used to specify what type of values are stored in variables.
They are
In JAVA language, primitive data types are the building blocks of data
manipulation.
In JAVA language, primitive data types are categorized into 4 groups. They are
1) Integers
2) Floating point numbers
3) Characters
4) Boolean
Integers:
Example:
byte b;
short – It is probably the least used data type. It is a signed 16-bit type that has a range
from -215 to +215-1. Its default value is 0.
Example:
short s;
int – It is widely used data type for storing integer values. It is a signed 32-bit type that
has a range from -231 to +231-1. Its default value is 0.
Example:
int a;
long – It is used for occasions where an int type is not large enough to hold the desired
value. It is a signed 64-bit type that has a range from -263 to +263-1. Its default value is 0.
Example:
long l;
Example:
float f;
double – It specifies double precision values that uses 64 bits of storage. Range of
double type is 4.9e-324 to 1.8e+308. By using double type, it is possible to store upto 15
digits after decimal point.
Example:
double d;
Characters:
In JAVA language, the data type used to store characters is char.
JAVA language uses UNICODE to represent characters.
UNICODE defines a fully international character set that can represent all of the
characters found in all human languages.
In JAVA language, character is a 16 bit type. The range of char is 0 to 216 -1.
Example:
char ch;
Boolean:
JAVA language has a primitive type called Boolean for storing logical/ boolean
values.
It can have only one of two possible values true or false.
It represents one bit of information.
The default value is false.
Example:
boolean b;
Keywords in JAVA:
volatile while
Identifiers:
An identifier can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9,
and two special characters such as underscore and dollar Sign.
The first character must be a letter.
Blank spaces cannot be used in identifiers.
Java keywords cannot be used as identifiers.
Identifiers are case-sensitive.
Variables:
Variable in Java is a data container that saves the data values during Java
program execution.
Every variable is assigned a data type that designates the type and quantity of
value it can hold.
A variable is a name given to a memory location. It is the basic unit of storage in
a program.
The value stored in a variable can be changed during program execution.
A variable is only a name given to a memory location.
All the operations done on the variable affect that memory location.
In Java, all variables must be declared before use.
Declaration of variables:
Syntax:
Example:
int a, b, c;
Initialization of variables:
Syntax:
var_name=value;
Example:
a=10;
datatype var_name=value;
Example:
int a=10;
Java Scanner class provides the following methods to read different primitives types:
Method Description
int nextInt() It is used to scan the next token of the input as an integer.
float nextFloat() It is used to scan the next token of the input as a float.
double nextDouble() It is used to scan the next token of the input as a double.
byte nextByte() It is used to scan the next token of the input as a byte.
boolean nextBoolean() It is used to scan the next token of the input into a boolean
value.
long nextLong() It is used to scan the next token of the input as a long.
short nextShort() It is used to scan the next token of the input as a Short.
Write a Program to perform addition of two numbers.
import [Link].*;
class Add
int a, b, c;
[Link](“enter a, b values”);
a=[Link]();
b=[Link]();
c=a+b;
[Link](“Result=”+c);
import [Link].*;
class Area
float b,h,area;
[Link](“enter b, h values”);
b=[Link]();
h=[Link]();
area=0.5*b*h;
[Link](“Area=”+area);
Literals in JAVA:
Any constant value which can be assigned to the variable is called literal/constant.
1. Integer Literals
2. Floating point Literals
3. Character Literals
4. Boolean Literals
5. String Literals
Integer Literals:
Integer literals are sequences of digits. There are three types of integer literals:
o Decimal Integer: These are the set of numbers that consist of digits from 0 to 9.
It may have a positive (+) or negative (-) Note that between numbers commas
and non-digit characters are not permitted. For example, 5678, +657, -89, etc.
The vales that contain decimal are floating literals. In Java, float and double primitive
types fall into floating-point literals. Keep in mind while dealing with floating-point
literals.
o Floating-point literals for float type end with F or f. For example, 6f, 8.354F, etc.
It is a 32-bit float literal.
o Floating-point literals for double type end with D or d. It is optional to write D or
d. For example, 6d, 8.354D, etc. It is a 64-bit double literal.
o It can also be represented in the form of the exponent.
Character Literals:
Boolean Literals:
Boolean literals are the value that is either true or false. It may also have values 0 and 1.
For example, true, 0, etc.
String literal is a sequence of characters that is enclosed between double quotes ("")
marks. It may be alphabet, numbers, special characters, blank space, etc.
Operators:
1) Arithmetic Operators
2) Relational Operators
3) Logical Operators
4) Assignment Operators
5) Increment and Decrement Operators (Unary Operators)
6) Bit-wise operators
7) Conditional Operator
Arithmetic Operators:
These operators are used to perform arithmetic operations like addition, subtraction,
multiplication and division.
Symbol Meaning
+ Addition
- Subtraction
* Multiplication
/ Division (gives quotient of the division)
% Modulus (gives remainder of the division)
Example:
a+b=15
a-b=5
a*b=50
a/b=2
a%b=0
Relational Operators:
Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Is equal to
!= Not equal to
Here a.e-1 and a.e-2 are used to represent variables or values or expressions.
Example:
Logical Operators:
They are
a)Logical AND (&&): This operator is used to combine two conditions. If both the
conditions are true then the result will be true otherwise false.
Example:
b) Logical OR(||):If one of the conditions is true then this operator will give true
otherwise give false.
Example:
Example:
Assignment Operators:
In JAVA Language, Assignment operator (=) is used to assign a value to the variable.
Example:
int a=10;
a=b=c=20;
var=var op exp
Example:
short hand assignment operator examples are +=, -=, *=, /=, %= etc.
These operators are called as unary operators because these operators are applied to
only one operand.
Increment operator:
a) Pre incrementation
b) Post incrementation
Syntax:
++operand
In pre incrementation, incrementation is done first and any other operation is done
next.
Example:
int a=10, b;
b=++a;
[Link](“a=”+a+” “+”b=”+b);
Syntax:
operand++;
In post incrementation, any other operation is done first and incrementation is done
next.
Example:
int a=10, b;
b=a++;
[Link](“a=”+a+” “+”b=”+b);
Decrement operator:
a) Pre decrementation
b) Post decrementation
a) Pre decrementation: In this, decrement operator is placed before the operand.
Syntax:
--operand
In pre decrementation, decrementation is done first and any other operation is done
next.
Example:
int a=10, b;
b=--a;
[Link](“a=”+a+” “+”b=”+b);
Syntax:
operand--;
In post decrementation, any other operation is done first and decrementation is done
next.
Example:
int a=10, b;
b=a--;
[Link](“a=”+a+” “+”b=”+b);
Bit-wise operators:
These operators are applied to only bits (i.e., 0’s and 1’s).
If we want apply bit-wise operators to any numbers, first that number is converted into
binary number and then apply bit-wise operator on binary number.
Truth table for the Bit-wise AND, Bit-wise OR, Bit-wise Ex-OR and Bit-wise complement.
Bit-wise Left Shift Operator(<<): This operator shits the bits towards left side by
specified number of times.
Syntax:
var<<num;
Here var represents a variable name and num represents an integer which specifies
how many times the bits to be shifted towards left side.
Example:
int a=10;
a<<2;
a=00001010
a<<2=00101000
Bit-wise Right Shift Operator(>>): This operator shifts the bits towards right side by
specified number of times.
Syntax:
var>> num;
Example:
int a=10;
a>>2;
a-00001010
a>>2-00000010
This operator also shifts the bits towards right side by specified number of times but
this operator always fills the sign bit with zero.
Syntax:
var>>> num;
Example:
int a=-8;
-8>>>2
a-11111000
a>>>2-00111110
Conditional Operator:
Syntax:
var=exp1?exp2:exp3;
Here exp1 represents a condition. If the condition evaluates to true then exp2 is
evaluated and assigned to var.
Example:
int a=5,b=3,max;
max=(a>b)?a:b;
Output: max=5
Expressions:
Evaluation of expressions:
Example1:
a-b/3+c*2-1
9-12/3+3*2-1
9-4+3*2-1
9-4+6-1
5+6-1
11-1
10
Example2:
Evaluate r=a/b+c*d-e&&f-!g/h where a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8
1/2+3*4-5&&6-!7/8
1/2+3*4-5&&6-0/8
0+3*4-5&&6-0/8
0+12-5&&6-0/8
0+12-5&&6-0
12-5&&6-0
7&&6-0
7&&6
Type conversion is nothing but converting from one data type to another data type.
Those are
When one type of data is assigned to another type of variable, JAVA’s automatic type
conversion takes place if the following conditions are met.
When two types are incompatible, we can use explicit type conversion.
(Cast-type) expression;
Here cast-type represents any one of the predefined data type and expression
represents a value or a variable or an expression.
Example:
int x=(int)23.5;
float ratio=(float)male/female;
class Conversion
int i=257;
byte b;
double d=323.142;
b=(byte)i;
[Link](b+” “+i);
b=(byte)d;
[Link](b+” “+d);
i) Selection Statements
ii) Iteration Statements (or) looping statements
iii) Jump Statements
Selection Statements:
a) if statement
b) switch statement
if statement: Generally, if statement is available in 4 formats.
1. Simple if statement
2. if-else statement
3. Nested if-else statement
4. else-if ladder.
Simple if statement:
Syntax:
if(condition)
{
Statement block;
}
Statement-x;
Here the condition is evaluated first. If the condition evaluates to true, then statement
block flowed by statement-x will be executed.
If the condition evaluates to false then the control is transferred to statement-x and
statement-x is executed.
Flow-chart for Simple if statement:
Example:
if (10<20)
[Link] (“Hello”)
[Link] (“Bye”);
Output:
Hello
Bye
import [Link].*;
class Result
int a, b, c, d;
float result;
a=[Link]();
b=[Link]();
c=[Link]();
d=[Link]();
if((c-d)!=0)
Result=(float)(a-b)/(c-d);
[Link](“result is ”+result);
Write a program to find the smallest of two numbers using simple if statement.
import [Link].*;
class Smallest
int a,b;
a=[Link]();
b=[Link]();
if(a<b)
if(b<a)
[Link](b+“is the smallest number”);
if-else statement:
Syntax:
if(condition)
{
statement block-1;
}
else
{
statement block-2;
}
Statement-x;
Here the condition is evaluated first. If the condition evaluates to true then statement
block-1 is executed.
Example:
int a=10,b=20;
if(a==b)
else
Write a program to find the largest of two numbers using if-else statement.
import [Link].*;
class Largest
int a,b;
a=[Link]();
b=[Link]();
if(a>b)
else
import [Link].*;
class EvenOdd
{
int n;
[Link](“enter a number”);
n=[Link]();
if(n%2==0)
else
import [Link].*;
class Number
int n;
[Link](“Enter n value”);
n=[Link]();
if(n>=0)
else
Nested if-else statement means writing one if-else with in another if-else statement.
Syntax:
if(condition1)
{
if(condition2)
{
Statement block-1;
}
else
{
Statement block-2;
}
}
else
{
if(condition3)
{
Statement block-3;
}
else
{
Statement block-4;
}
}
Here condition1 is evaluated first. If the condition1 evaluates to true then the
condition2 is evaluated. If the condition2 is also true then statement blolck-1 is
executed otherwise statement block-2 is executed.
import [Link].*;
class Large
int a,b,c;
a=[Link]();
b=[Link]();
c=[Link]();
if(a>b)
if(a>c)
else
[Link](b+“is largest number”);
else
if(b>c)
else
else-if ladder:
Syntax:
if(condition1)
statement-1;
else if(condition2)
statement-2;
else if(condition3)
statement-3;
.
.
.
else if(condition-n)
statement-n;
else
statement-x;
import [Link].*;
class Roots
double a,b,c,d,r1,r2;
a=[Link]();
b=[Link]();
c=[Link]();
d=b*b-4*a*c;
if(d==0)
{
r1=r2=-b/(2*a);
else if(d>0)
r1=((-b+[Link](d))/(2*a));
r2-((-[Link](d))/(2*a));
else
Write a program that accepts a number from 1 to 7 as input and display the week
days depending on the input number.
import [Link].*;
class WeekDay
int n;
[Link](“enter n value”);
n=[Link]();
if(n==1)
[Link](“Monday”);
else if(n==2)
[Link](“Tuesday”);
else if(n==3)
[Link](“Wednesday”);
else if(n==4)
[Link](“Thursday”);
else if(n==5)
[Link](“Friday”);
else if(n==6)
[Link](“Saturday”);
else if(n==7)
[Link](“Sunday”);
else
[Link](“Invalid number”);
Switch Statement:
Syntax:
switch(expression)
{
case value-1: block-1;
breakl;
case value-2: block-2;
break;
case value-3: block-3;
break;
.
.
.
.
case value-n: block-n;
break;
default: default-block;
}
Statement-x;
break statement at the end of each case statement signals that the end of the case
statement and causes an exit from switch statement, transferring control to statement-x;
When the switch statement is executed, the value of the expression is compared against
the values value-1, value-2, ……value-n.
If a case is found whose value matches with the value of the expression, then the block
of statements that follows the case are executed.
Default statement is an optional case. If none of the values matches with the expression
value then the default block is executed.
int a=1;
switch(a)
case 0: [Link](“zero”);
break;
case 1: [Link](“one”);
break;
case 2: [Link](“two”);
break;
Output: one
Example2:
int a=4;
switch(a)
case 0: [Link](“zero”);
break;
case 1: [Link](“one”);
break;
case 2: [Link](“two”);
break;
Write a program that accepts a number from 1 to 7 as input and display the week
days depending on the input number.
import [Link].*;
class WeekDay
int n;
n=[Link]();
switch(n)
case 1: [Link](“Monday”);
break;
case 2: [Link](“Tuesday”);
break;
case 3: [Link](“Wednesday”);
break;
case 4: [Link](“Thursday”);
break;
case 5: [Link](“Friday”);
break;
case 6: [Link](“Saturday”);
break;
case 7: [Link](“Sunday”);
break;
Write a program that accepts two integers and perform arithmetic operations by
taking user choice using switch statement.
import [Link].*;
class Arithmetic
{
public static void main(String args[])
int a,b,c,ch;
a=[Link]();
b=[Link]();
[Link](“[Link]”);
[Link](“2. Subtraction”);
[Link](“5. Exit”);
ch=[Link]();
switch(ch)
case 1: c=a+b;
[Link](“Addition=”+c);
break;
case 2: c=a-b;
[Link](“Subtraction=”+c);
break;
case 3: c=a*b;
[Link](“Multiplication=”+c);
break;
case 4: c=a/b;
[Link](“Division=”+c);
break;
case 5: [Link](0);
break;
Iterative statements are used to execute a set of statements repeatedly for a specific
number of times or until the condition is satisfied.
Initialization:
Example:
i=0,n=10,a=5;
Condition:
Example:
Updation:
In this, the value of the control variable is updated by using either incfrement operator
or decrement operator.
Example:
a) While loop
b) do-while loop
c) for loop
while loop:
Syntax:
initialization;
while(condition)
{
statement-1;
statement-2;
.
.
.
statement-n;
updation;
}
Here the condition is evaluated first. If condition evaluates to true then the statements
with in the while block are executed.
After execution of all the statements in while block, control transferred to condition.
If condition is again true then the statements in the while block are executed again.
Once the condition becomes false, the control is transferred to the statement after the
while loop.
class Display
int i;
i=1;
while(i<=10)
[Link](i);
i++;
}
import [Link].*;
class Sum
int n, i, sum=0;
[Link](“enter n value”);
n=[Link]();
i=1;
while(i<=n)
sum=sum+i;
i++;
[Link](“sum=”+sum);
import [Link].*;
class Sum
{
int n, i, sum=0;
[Link](“enter n value”);
n=[Link]();
i=1;
while(i<=n)
sum=sum+i*i;
i++;
[Link](“sum=”+sum);
import [Link].*;
class SumOfDigits
[Link](“enter n value”);
n=[Link]();
while(n!=0)
digit=n%10;
sum=sum+digit;
n=n/10;
[Link](“sum=”+sum);
import [Link].*;
class Reverse
[Link](“enter n value”);
n=[Link]();
while(n!=0)
digit=n%10;
rev=rev*10+digit;
n=n/10;
import [Link].*;
class Palindrome
[Link](“enter n value”);
n=[Link]();
temp=n;
while(n!=0)
digit=n%10;
rev=rev*10+digit;
n=n/10;
if(rev==temp)
[Link](temp+“is palilndrome”);
else
}
Write a program to check whether the given number is Armstrong number or not.
import [Link].*;
class Armstrong
[Link](“enter n value”);
n=[Link]();
temp=n;
while(n!=0)
digit=n%10;
sum=sum+(digit*digit*digit);
n=n/10;
if(sum==temp)
else
import [Link].*;
class Factorial
int n, i, fact=1;
[Link](“enter n value”);
n=[Link]();
i=1;
while(i<=n)
fact=fact*I;
Write a program to print the Fibonacci series upto the given number.
import [Link].*;
class Fibonacci
[Link](“enter n value”);
n=[Link]();
[Link](“Fibonacci series is”);
[Link](a+” “+b);
c=a+b;
while(c<=n)
[Link](c);
a=b;
b=c;
c=a+b;
do-while loop:
Syntax:
initialization;
do
{
statement-1;
statement-2;
statement-3;
.
.
statement-n;
updation;
} while(condition);
Here the set of statements available in do-while block are executed once without
checking any condition.
After execution of set of statements one time, control is transferred to condition. If the
condition evaluates to true then the set of statements in do-while block are executed
again.
class Display
int i;
i=1;
do
{
[Link](i);
i++;
} while (i<=10);
Write a program to display the odd numbers from 1 to 50 using do-while loop.
class DisplayOdd
int i;
i=1;
do
[Link](i);
i+=2;
} while(i<=50);
import [Link].*;
class MultiplicationTable
int n,i;
[Link](“enter n value”);
n=[Link]();
i=1;
do
[Link](i+”*”+n+”=”+(n*i));
i++;
} while(i<=20);
for loop:
Syntax:
for(initialization;condition;updation)
{
//body of the loop
}
Here initialization is done first and initialization part is executed only one time during
the execution of for loop.
After initialization, control is transferred to condition. If the condition is true then the
body of the loop is executed. After execution of body of the loop, control is transferred
to updation.
Then control is transferred to condition. If the condition is again true then body of the
loop is executed again.
class Display
int i;
for(i=1;i<=10;i++)
[Link](i);
}
Write a program to check whether the given number is prime number or not.
import [Link].*;
class Prime
int n, i, count=0;
[Link](“enter n value”);
n=[Link]();
for(i=1;i<=n;i++)
if(n%i==0)
count++;
if(count==2)
else
}
Write a program to check whether the given number is perfect number or not.
import [Link].*;
class Perfect
int n, i, sum=0;
[Link](“enter n value”);
n=[Link]();
for(i=1;i<n;i++)
if(n%i==0)
sum=sum+i;
if(sum==n)
else
The Java for-each loop or enhanced for loop is introduced since J2SE 5.0.
for(type iter_var:collection)
statement_block;
Here type specifies the type of the elements available in collection, iter_var represents a
variable that receives elements from collection one at a time, from beginning to end and
collection represents a collection of elements.
Example:
int a[]={1,2,3,4,5};
for(int x:a)
[Link](x);
Writing one for loop with in another for loop is called as nested for loop.
Syntax:
for(initialization;condition1;updation1)
{
for(initialization2;condition2;updation2)
{
//body of the inner loop
}
}
Here initialization in outer for loop is executed first then control is transferred to
condition1. If condition1 is true then the control is transferred to inner for loop.
In inner loop, initialization2 is executed first and then the control is transferred to
condition2. If condition2 is true then the body of the inner for loop is executed. After
execution of body of the inner for loop, control is transferred to updation2.
When the condition2 is false, the control is transferred to updation1. After updation,
control is transferred to condition1 and it is evaluated.
import [Link].*;
class Prime
int n, i, j, count;
[Link](“enter n value”);
n=[Link]();
for(i=2;i<=n;i++)
count=0;
for(j=1;j<=i;j++)
if(i%j==0)
count++;
if(count==2)
[Link](i);
}
Write a program to display the following number pattern.
12
123
1234
12345
import [Link].*;
class Pattern1
int n,i, j;
n=[Link]();
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
[Link](j);
[Link]();
}
Write a program to display the following number pattern.
22
333
4444
55555
import [Link].*;
class Pattern2
int n, i, j;
n=[Link]();
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
[Link](i);
[Link]();
}
Write a program to display the following number pattern.
54321
4321
321
21
import [Link].*;
class Pattern3
int n, i, j;
n=[Link]();
for(i=n;i>=1;i--)
for(j=i;j>=1;j--)
[Link](j);
[Link]();
}
Jump Statements:
These statements are used to transfer the control from one point to another point in a
program.
1. break statement
2. continue statement
break statement:
break;
Example:
int i=0;
while(i<=10)
i++;
if(i==6)
break;
[Link](i);
In nested loops, if the break statement is used in inner loop, then that break statement
causes an early exit only from inner loop.
continue statement:
continue statement is used to perform the next iteration of the loop by skipping some
statements in loop.
Syntax:
continue;
Example1:
int i=0;
while(i<10)
i++;
if(i==6)
continue;
[Link](i);
Example2:
int i=0;
while(i<10)
i++;
if(i<=5)
continue;
[Link](i);
Arrays:
An array is a collection of homogeneous elements that are stored under a single name.
In JAVA language, arrays are categorized into two types. They are
In first step, we have to declare the name of the array with data type.
Syntax:
datatype array_name[];
Or
datatype[] array_name;
In Second step, memory is allocated for the array using new operator.
Syntax:
After allocating memory, all the array elements are initialized to zero by default.
We can combine the above two steps into a single statement as follows.
Example:
After creating an array, array elements are accessed using index value.
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
a[5]=6;
a[6]=7;
a[7]=8;
a[8]=9;
a[9]=10;
Example:
int a[]={1,2,3,4,5};
Example:
for(int i=0;i<5;i++)
a[i]=[Link]();
}
Write a program to read and display array elements.
import [Link].*;
class A
int n,i;
n=[Link]();
for(i=0;i<n;i++)
a[i]=[Link]();
for(i=0;i<n;i++)
[Link](a[i]);
import [Link].*;
class SumAvg
int n,i,sum=0;
float avg;
n=[Link]();
for(i=0;i<n;i++)
a[i]=[Link]();
for(i=0;i<n;i++)
sum=sum+a[i];
avg=sum/n;
[Link](sum+” “+avg);
import [Link].*;
class Sorting
{
int n,i,temp;
n=[Link]();
for(i=0;i<n;i++)
a[i]=[Link]();
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
temp=a[i];
a[i]=a[j];
a[j]=temp;
for(i=0;i<n;i++)
{
[Link](a[i]);
Multi-dimensional Arrays:
An array with more than one dimension is called as multi dimensional array.
Two-dimensional array:
When we want to store the data in tabular form or matrix form, two-dimensional array
is used.
Syntax:
Example:
It is also possible to create two-dimensional array without specifying the column size.
Example:
import [Link].*;
class Matrix
{
int r1,c1,r2,c2i,j;
r1=[Link]();
c1=[Link]();
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
a[i][j]=[Link]();
r2=[Link]();
c2=[Link]();
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
b[i][j]=[Link]();
if((r1==r2)||(c1==c2))
{
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
[Link](c[i][j]+” “);
[Link]();
else