SlideShare a Scribd company logo
1 of 159
Java Manual
Manual of JAVA 2
This manual contains more than 60 programs of JAVA 2
2014
Farwa Ansari
NFC, FSD
8/10/2014
2
Table of Contents
Program 1:...................................................................................................................................................12
Solution:......................................................................................................................................................12
Output: ........................................................................................................................................................13
Discussion:..................................................................................................................................................13
Program 2:...................................................................................................................................................13
Solution:......................................................................................................................................................13
Output: ........................................................................................................................................................14
Discussion:..................................................................................................................................................15
Program 3:...................................................................................................................................................15
Solution:......................................................................................................................................................15
Output: ........................................................................................................................................................17
Discussion:..................................................................................................................................................17
Program 4:...................................................................................................................................................17
Solution:......................................................................................................................................................17
Output: ........................................................................................................................................................19
Discussion:..................................................................................................................................................19
Program 5:...................................................................................................................................................19
Solution:......................................................................................................................................................19
Output: ........................................................................................................................................................21
Discussion:..................................................................................................................................................21
Program 6:...................................................................................................................................................22
Solution:......................................................................................................................................................22
Output: ........................................................................................................................................................23
Discussion:..................................................................................................................................................23
Program 7:...................................................................................................................................................24
Solution:......................................................................................................................................................24
Output: ........................................................................................................................................................25
Discussion:..................................................................................................................................................25
Program 8:...................................................................................................................................................25
Solution:......................................................................................................................................................25
Output: ........................................................................................................................................................26
3
Discussion:..................................................................................................................................................27
Program 9:...................................................................................................................................................27
Solution:......................................................................................................................................................27
Output: ........................................................................................................................................................29
Discussion:..................................................................................................................................................29
Program 10:.................................................................................................................................................30
Solution:......................................................................................................................................................30
Output: ........................................................................................................................................................32
Discussion:..................................................................................................................................................32
Program 11:.................................................................................................................................................32
Solution:......................................................................................................................................................32
Output: ........................................................................................................................................................33
Discussion:..................................................................................................................................................34
Program 12:.................................................................................................................................................34
Solution:......................................................................................................................................................34
Output: ........................................................................................................................................................35
Discussion:..................................................................................................................................................36
Program 13:.................................................................................................................................................36
Solution:......................................................................................................................................................36
Output: ........................................................................................................................................................37
Discussion:..................................................................................................................................................38
Program14:..................................................................................................................................................38
Solution:......................................................................................................................................................38
Output: ........................................................................................................................................................41
Discussion:..................................................................................................................................................41
Program 15:.................................................................................................................................................42
Solution:......................................................................................................................................................42
Output: ........................................................................................................................................................43
Discussion:..................................................................................................................................................43
Program 16:.................................................................................................................................................43
Solution:......................................................................................................................................................43
Output: ........................................................................................................................................................46
4
Discussion:..................................................................................................................................................46
Program 17:.................................................................................................................................................46
Solution:......................................................................................................................................................46
Output: ........................................................................................................................................................48
Discussion:..................................................................................................................................................48
Program 18:.................................................................................................................................................48
Solution:......................................................................................................................................................48
Output: ........................................................................................................................................................50
Discussion:..................................................................................................................................................50
Program 19:.................................................................................................................................................50
Solution:......................................................................................................................................................50
Output: ........................................................................................................................................................52
Discussion:..................................................................................................................................................52
Program 20:.................................................................................................................................................52
Solution:......................................................................................................................................................52
Output: ........................................................................................................................................................55
Discussion:..................................................................................................................................................55
Program 21:.................................................................................................................................................55
Solution:......................................................................................................................................................55
Output: ........................................................................................................................................................57
Discussion:..................................................................................................................................................58
Program 22:.................................................................................................................................................58
Solution:......................................................................................................................................................58
Output: ........................................................................................................................................................60
Discussion:..................................................................................................................................................60
Program 23:.................................................................................................................................................60
Solution:......................................................................................................................................................61
Output: ........................................................................................................................................................63
Discussion:..................................................................................................................................................63
Program 24:.................................................................................................................................................63
Solution:......................................................................................................................................................63
Output: ........................................................................................................................................................66
5
Discussion:..................................................................................................................................................66
Program 25:.................................................................................................................................................66
Solution:......................................................................................................................................................66
Output: ........................................................................................................................................................69
Discussion:..................................................................................................................................................69
Program 26:.................................................................................................................................................69
Solution:......................................................................................................................................................69
Output: ........................................................................................................................................................71
Discussion:..................................................................................................................................................71
Program 27:.................................................................................................................................................71
Solution:......................................................................................................................................................71
Output: ........................................................................................................................................................73
Discussion:..................................................................................................................................................73
Program 28:.................................................................................................................................................73
Solution:......................................................................................................................................................73
Output: ........................................................................................................................................................76
Discussion:..................................................................................................................................................76
Program 29:.................................................................................................................................................76
Solution:......................................................................................................................................................76
Output: ........................................................................................................................................................78
Discussion: ..................................................................................................................................................78
Program 30:.................................................................................................................................................78
Solution:......................................................................................................................................................78
Output: ........................................................................................................................................................79
Discussion:..................................................................................................................................................79
Program 31:.................................................................................................................................................80
Solution:......................................................................................................................................................80
Output: ........................................................................................................................................................81
Discussion:..................................................................................................................................................81
Program 32:.................................................................................................................................................81
Solution:......................................................................................................................................................81
Output: ........................................................................................................................................................83
6
Discussion:..................................................................................................................................................83
Program 33:.................................................................................................................................................83
Solution:......................................................................................................................................................83
Output: ........................................................................................................................................................84
Discussion:..................................................................................................................................................84
Program 34:.................................................................................................................................................85
Solution:......................................................................................................................................................85
Output: ........................................................................................................................................................88
Discussion:..................................................................................................................................................88
Program 35:.................................................................................................................................................88
Solution:......................................................................................................................................................88
Output: ........................................................................................................................................................90
Discussion:..................................................................................................................................................90
Program 36:.................................................................................................................................................91
Solution:......................................................................................................................................................91
Output: ........................................................................................................................................................94
Discussion:..................................................................................................................................................94
Program 37:.................................................................................................................................................94
Solution:......................................................................................................................................................94
Output: ........................................................................................................................................................96
Discussion:..................................................................................................................................................96
Program 38:.................................................................................................................................................96
Solution:......................................................................................................................................................96
Output: ........................................................................................................................................................98
Discussion:..................................................................................................................................................98
Program 39:.................................................................................................................................................99
Solution:......................................................................................................................................................99
Output: ......................................................................................................................................................101
Discussion:................................................................................................................................................101
Program 40:...............................................................................................................................................101
Solution:....................................................................................................................................................101
Output: ......................................................................................................................................................103
7
Discussion:................................................................................................................................................103
Program 41:...............................................................................................................................................103
Solution:....................................................................................................................................................103
Output: ......................................................................................................................................................105
Discussion:................................................................................................................................................105
Program 42:...............................................................................................................................................105
Solution:....................................................................................................................................................105
Output: ......................................................................................................................................................107
Discussion:................................................................................................................................................107
Program 43:...............................................................................................................................................107
Solution:....................................................................................................................................................107
Output: ......................................................................................................................................................110
Discussion:................................................................................................................................................110
Program 44:...............................................................................................................................................110
Solution:....................................................................................................................................................110
Output: ......................................................................................................................................................111
Discussion:................................................................................................................................................111
Program 45:...............................................................................................................................................112
Solution:....................................................................................................................................................112
Output: ......................................................................................................................................................113
Discussion:................................................................................................................................................113
Program 46:...............................................................................................................................................113
Solution:....................................................................................................................................................113
Output: ......................................................................................................................................................114
Discussion:................................................................................................................................................114
Program 47:...............................................................................................................................................115
Solution:....................................................................................................................................................115
Output: ......................................................................................................................................................116
Discussion:................................................................................................................................................116
Program 48:...............................................................................................................................................116
Solution:....................................................................................................................................................116
Output: ......................................................................................................................................................117
8
Discussion:................................................................................................................................................117
Program 49:...............................................................................................................................................118
Solution:....................................................................................................................................................118
Output: ......................................................................................................................................................119
Discussion:................................................................................................................................................119
Program 50:...............................................................................................................................................119
Solution:....................................................................................................................................................119
Output: ......................................................................................................................................................120
Discussion:................................................................................................................................................120
Program 51:...............................................................................................................................................120
Solution:....................................................................................................................................................120
Output: ......................................................................................................................................................122
Discussion:................................................................................................................................................122
Program 52:...............................................................................................................................................122
Solution:....................................................................................................................................................122
Output: ......................................................................................................................................................123
Discussion:................................................................................................................................................123
Program 53:...............................................................................................................................................124
Solution:....................................................................................................................................................124
Output: ......................................................................................................................................................124
Discussion:................................................................................................................................................125
Program 54:...............................................................................................................................................125
Solution:....................................................................................................................................................125
Output: ......................................................................................................................................................126
Discussion:................................................................................................................................................126
Program 55:...............................................................................................................................................127
Solution:....................................................................................................................................................127
Output: ......................................................................................................................................................127
Discussion:................................................................................................................................................128
Program 56:...............................................................................................................................................128
Solution:....................................................................................................................................................128
Output: ......................................................................................................................................................129
9
Discussion:................................................................................................................................................130
Program 57:...............................................................................................................................................130
Solution:....................................................................................................................................................130
Output: ......................................................................................................................................................131
Discussion:................................................................................................................................................131
Program 58:...............................................................................................................................................131
Solution:....................................................................................................................................................131
Output: ......................................................................................................................................................132
Discussion:................................................................................................................................................132
Program 59:...............................................................................................................................................132
Solution:....................................................................................................................................................133
Output: ......................................................................................................................................................133
Discussion:................................................................................................................................................134
Program 60:...............................................................................................................................................134
Solution:....................................................................................................................................................134
Output: ......................................................................................................................................................135
Discussion:................................................................................................................................................135
Program 61:...............................................................................................................................................135
Solution:....................................................................................................................................................136
Output: ......................................................................................................................................................136
Discussion:................................................................................................................................................136
Program 62:...............................................................................................................................................137
Write a program of interface.....................................................................................................................137
Solution:....................................................................................................................................................137
Output:......................................................................................................................................................139
Description:...............................................................................................................................................140
Program 63:...............................................................................................................................................140
Write a program in which one interface can extend another. ...................................................................140
Solution:....................................................................................................................................................140
Output: ......................................................................................................................................................141
Description:...............................................................................................................................................142
Program 64:...............................................................................................................................................142
10
Solution:....................................................................................................................................................142
Output:......................................................................................................................................................143
Discussion: ................................................................................................................................................143
Program 65:...............................................................................................................................................143
Solution:....................................................................................................................................................143
Output:......................................................................................................................................................144
Discussion: ................................................................................................................................................144
Program 66:...............................................................................................................................................144
Solution:....................................................................................................................................................144
Output:......................................................................................................................................................146
Discussion: ................................................................................................................................................146
Program 67:...............................................................................................................................................146
Solution:....................................................................................................................................................146
Output:......................................................................................................................................................147
Discussion: ................................................................................................................................................147
Program 68:...............................................................................................................................................148
Solution:....................................................................................................................................................148
Output:......................................................................................................................................................149
Discussion: ................................................................................................................................................149
Program 69:...............................................................................................................................................149
Solution:....................................................................................................................................................149
Output:......................................................................................................................................................151
Discussion: ................................................................................................................................................151
Program 70:...............................................................................................................................................151
Solution:....................................................................................................................................................151
Output:......................................................................................................................................................153
Discussion: ................................................................................................................................................153
Program 71:...............................................................................................................................................153
Solution:....................................................................................................................................................153
Output:......................................................................................................................................................154
Discussion: ................................................................................................................................................154
Program 72:...............................................................................................................................................155
11
Solution:....................................................................................................................................................155
Output:......................................................................................................................................................155
Discussion: ................................................................................................................................................156
Program 73:...............................................................................................................................................156
Solution:....................................................................................................................................................156
Output:......................................................................................................................................................158
Discussion: ................................................................................................................................................158
Program 74:...............................................................................................................................................158
Solution:....................................................................................................................................................158
Output:......................................................................................................................................................159
Discussion: ................................................................................................................................................159
12
Program 1:
Write a simple program that explains the syntax of java code or
demonstrate how to write it.
Solution:
class Example {
/* this line contains a keyword class that shows that a class is going to start and Example is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
// Your program begin with a call to main.
public static void main(String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
System.out.println("This is a simple java program.");
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display the string passed to it & ln is used to goto the next line.
Semicolon is used to terminate the statement/line */
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
13
Output:
Discussion:
In this program a class of name Example is declared then its definition starts. Its
definition is enclosed between the opening and closing of curly braces {}. Then main
method of no return type is declared. This method is static and public. Its definition is
enclosed between the opening and closing curly braces {}. This program simply displays
the string that is “This is a simple java program”.
Program 2:
Write a program that initializes a variable and display it times two.
Solution:
class Example2 {
/* this line contains a keyword class that shows that a class is going to start and Example2 is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main (String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
14
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
int num; // a variable of type integer is declared
num = 100; // a variable num is initialized to 100
System.out.println("num: " + num);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display the string passed to it & ln is used to goto the next line. This
statement of code prints the value of num variable of type int i.e 100*/
num = num * 2;
/*increases the value of num variable by 2. Semicolon is used to terminate the statement/line*/
System.out.println("num * 2: " + num);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display the string passed to it & ln is used to goto the next line. This
statement of code prints the value of num times 2 i.e 200. Semicolon is used to terminate the
statement/line */
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
Output:
15
Discussion:
In this program a class of name Example2 is declared then its definition starts. Its
definition is enclosed between the opening and closing of curly braces {}. Then main
method of no return type is declared. This method is static and public. Its definition is
enclosed between the opening and closing curly braces {}. This program declares a
variable called num then assigns the value to num that is 100 and then it displays the
value of num by multiplying it by 2
Program 3:
Write a program that demonstrate the use of “ if ” statement in java.
Solution:
class ifsample {
/* this line contains a keyword class that shows that a class is going to start and ifsample is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main (String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
int x , y;
/*two variables of type int are declared. Semicolon is used to terminate the statement/line*/
x = 10;
y = 20;
// variables x & y are initialized. Semicolon is used to terminate the statement/line
if(x < y)
/*in this line of code an if statement with a condition is declared, that if x is less than y then the
next statement will be interpret. The next statement will be interpret because this condition is
true*/
16
System.out.println (" 'X' is less than 'Y' ");
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display the string passed to it & ln is used to goto the next line. This
statement of code will print out that 'X' is less than 'Y' only if the above statement is true which
is true. Semicolon is used to terminate the statement/line */
x = x * 2;
/* increase the value of x from 10 to 20 and stores it in x. Semicolon is used to terminate the
statement/line*/
if(x == y)
/* an if statement with a condition is declared, that if x == y then the next statement will be
interpret or displayed or execute. So the above condition is true because now x is equal to y i.e
20*/
System.out.println (" 'X' is equal to 'Y' ");
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display the string passed to it & ln is used to goto the next line. This
statement of code will print out that 'X' is less than 'Y' only if the above statement is true which
is true already. Semicolon is used to terminate the statement/line */
x = x * 2;
/* increase the value of x from 20 to 40 and stores it in x. Semicolon is used to terminate the
statement/line*/
if(x == y)
/*an if statement with a condition is declared, that if x == y then the next statement will be
interpret but here it is not true because x is not equal to y, it is greater than y*/
System.out.println (" You won't see anything ");
/*System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display the string passed to it & ln is used to goto the next line. This
statement of code will not print anything because the above if statement is not true so it will exit.
Semicolon is used to terminate the statement/line */
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
17
Output:
Discussion:
In this program a class of name ifsample is declared then its definition starts. Its
definition is enclosed between the opening and closing of curly braces {}. Then main method of
no return type is declared. This method is static and public. Its definition is enclosed between the
opening and closing curly braces {}. This program declares & defines two variables then it
checks if x is less than y then it displays “x is less than y”. x is incremented x times 2 it will
become equal to y so it displays that “x is now equal to y”. it again incremented x and checks if x
is greater than y and displays it.
Program 4:
Write a program that demonstrate the use of “ for ” loop in java.
Solution:
/* Demonstrate the for loop. Call this file "fortest.java*/
class fortest {
/*this line contains a keyword class that shows that a class is going to start and fortest is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main( String args[]) {
18
/* this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
int x;
// a variable x of type int is declared. Semicolon is used to terminate the statement/line
for(x=0; x<10; x=x+1)
/*a for loop is declared here it contains three parts initialization i.e initializes x to 0, condition
i.e x is less than 10, iteration i.e increment of x. Firstly loop initialize x to zero and then it will
check the condition that either it is true or not, if it is true then the next statement will be
compiled, after that control/system will pass to the iteration process then it will check the
condition if it is fulfilled then the statement given below will be executed again i.e it will go on
increasing the value of x by 1 until the condition is not satisfied*/
System.out.println("This is x: " + x);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display the string passed to it & ln is used to goto the next line. This
statement of code will print the string written in brackets and also print out the value of x until it
is less than 10 i.e it will print it out only if the condition of for statement is true. Semicolon is
used to terminate the statement/line */
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
19
Output:
Discussion:
In this program a class of name fortest is declared then its definition starts. Its definition
is enclosed between the opening and closing of curly braces {}. Then main method of no return
type is declared. This method is static and public. Its definition is enclosed between the opening
and closing curly braces {}. This class displays the value of x 10 times using for loop
Program 5:
Write a program that uses a block of code as the target of a “for” loop.
Solution:
class blocktest {
/* this line contains a keyword class that shows that a class is going to start and blocktest is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main(String args[]) {
20
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
int x, y;
// two variables x and y of type int are declared. Semicolon is used to terminate the
statement/line
y = 20;
// variable y is initializes to 20. Semicolon is used to terminate the statement/line
for ( x=0; x<10; x++) {
/*a for loop is declared here it contains three parts initialization i.e initializes x to 0, condition
i.e x is less than 10, iteration i.e increment of x. Firstly loop initialize x to zero and then it will
check the condition that either it is true or not, if it is true then the next statement will be
compiled, after that control/system will pass to the iteration process then it will check the
condition if it is fulfilled then the statement given below will be executed again i.e it will go on
increasing the value of x by 1 until the condition is not satisfied i.e x<10. Opening curly brace {
will separate the block of code of for loop i.e it means begin of block*/
System.out.println ("This is X: " + x);
System.out.println ("This is y: " + y);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display the string passed to it & ln is used to goto the next line. These
statements of code will print the string written in brackets and also print out the value of x and y.
Semicolon is used to terminate the statement/line */
y = y - 2;
/* here Y will be decreased by 2 and then the control passes to iteration portion in for loop and
then the portion below that loop will be displayed until the condition will be less than 10 i.e it
will print it out only if the condition of for statement is true i.e x<10. It after displaying them
once check the decrement of Y by 2 and goes on further until x<10. Semicolon is used to
terminate the statement/line */
21
} /* closing curly brace } is used to close the definition of for loop.
Closing curly brace { will separate the block of code of for loop i.e it means end
of block*/
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
Output:
Discussion:
In this program a class of name blocktest is declared then its definition starts. Its
definition is enclosed between the opening and closing of curly braces {}. In this code the target
of for loop is the block of code not just the statement of code.
22
Program 6:
Write a program that calculates the distance traveled by light in
thousand days using long variables.
Solution:
class Light {
/* this line contains a keyword class that shows that a class is going to start and Light is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main(String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
int lightspeed = 186000;
/*approximate light speed in miles per second. Lightspeed is a variable of type int. Semicolon is
used to terminate the statement/line*/
long seconds, days, distance;
/*three variables of type long are declared. Semicolon is used to terminate the statement/line*/
days = 1000;
/*specify number of days here i.e initializes days to 1000. Semicolon is used to terminate the
statement/line of code*/
seconds = days * 24 * 60 * 60;
// convert to seconds. Semicolon is used to terminate the statement/line
distance = lightspeed * seconds ;
/* calculating distance by multiplying lightspeed & seconds. Semicolon is used to terminate the
statement/line*/
System.out.print("In " + days);
23
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display the string passed to it & ln is used to goto the next line. This
statement of code will print the string written in brackets and also print out the value of days.
Semicolon is used to terminate the statement/line */
System.out.print(" days light will trevel about ");
System.out.println(distance + " miles");
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. These statements of code
will print the string written in brackets and also print out the value of miles written in 2nd
one.
Semicolon is used to terminate the statement/line */
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
Output:
Discussion:
In this program a class of name Light is declared then its definition starts. Its
definition is enclosed between the opening and closing of curly braces {}. Then main method of
no return type is declared. This method is static and public. Its definition is enclosed between the
opening and closing curly braces {}. It computes distance light travels using 3 long variables.
Then stores approximate speed of light in miles per second and specify number of days then
convert to seconds and compute distance and displays it.
24
Program 7:
Write a code to calculate area using double variables
Solution:
class Area {
/* this line contains a keyword class that shows that a class is going to start and Area is the name
of this class. Opening curly brace { shows that form here a class’ definition is going to begin */
public static void main(String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
double pi, r , a;
/* three variables of type double are declared . Semicolon is used to terminate the
statement/line*/
r = 10.8;
// radius of circle . Semicolon is used to terminate the statement/line
pi = 3.1416;
/*approx. value of pi. Pi is declared . Semicolon is used to terminate the statement/line*/
a = pi * r * r;
//area’s formula to calculate area . Semicolon is used to terminate the statement/line
System.out.println("Area of circle is " + a);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display the string passed to it & ln is used to goto the next line. This
statement of code will print the string written in brackets and also print out the value of a .
Semicolon is used to terminate the statement/line */
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
25
Output:
Discussion:
In this program a class of name Area is declared then its definition starts. Its
definition is enclosed between the opening and closing of curly braces {}. Then main method of
no return type is declared. This method is static and public. Its definition is enclosed between the
opening and closing curly braces {}. It compute area of circle
Program 8:
Write a program that demonstrate the use of char data type
Solution:
class CharDemo {
/* this line contains a keyword class that shows that a class is going to start and CharDemo is
the name of this class. Opening curly brace { shows that form here a class’ definition is going
to begin */
public static void main(String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
26
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
char ch1, ch2;
// two variables of type char(character). Semicolon is used to terminate the statement/line
ch1 = 88;
/* code for X i.e ch1 variable is defined . Semicolon is used to terminate the statement/line*/
ch2 = 'Y';
//2nd
character is defined to y. Semicolon is used to terminate the statement/line
System.out.println("ch1 and ch2: " + ch1 + " and " + ch2);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of ch1 and ch2 . Semicolon is
used to terminate the statement/line*/
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
Output:
27
Discussion:
In this program a class of name CharDemo is declared then its definition starts. Its
definition is enclosed between the opening and closing of curly braces {}. Then main method of
no return type is declared. This method is static and public. Its definition is enclosed between the
opening and closing curly braces {}. It demonstrate char data type ch1 is assigned the value 88
which is ASCII letter that correspond to letter X
Program 9:
Write a program that demonstrates how char variables behave like
integers.
Solution:
class CharDemo2 {
/* this line contains a keyword class that shows that a class is going to start and CharDemo2 is
the name of this class. Opening curly brace { shows that form here a class’ definition is going
to begin */
public static void main(String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
char ch1 = 'X';
//a variable X of char type is declared. Semicolon is used to terminate the statement/line
char ch2 = 'A';
//a variable A of char type is declared. Semicolon is used to terminate the statement/line
System .out.println("ch1 contains " + ch1);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of ch1. Semicolon is used to
terminate the statement/line*/
28
ch1++;
/*an increment operator is used here to increase the value of ch1. Semicolon is used to terminate
the statement/line*/
System.out.println("Now ch1 contains " +ch1);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of ch1. Semicolon is used to
terminate the statement/line*/
System.out.println("ch2 contains " + ch2);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of ch2. Semicolon is used to
terminate the statement/line */
System.out.println(" ~~~ LOOP ~~~ ");
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets. Semicolon is used to terminate the statement/line */
for (int i = 0; i<25 ; i++) {
/*a for loop is declared here it contains three parts initialization i.e initializes i to 0, condition
i.e i is less than 25, iteration i.e increment of i. Firstly loop initialize i to zero and then it will
check the condition that either it is true or not, if it is true then the next statement will be
compiled, after that control/system will pass to the iteration process then it will check the
condition if it is fulfilled then the statement given below will be executed again i.e it will go on
increasing the value of i by 1 until the condition is not satisfied i.e x<25. Opening curly brace {
will separate the block of code of for loop i.e it means begin of block*/
ch2 ++;
// ch2 is incremented by 1
System.out.println("Now value of ch2 is " +ch2);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of ch2 */
}// closing curly brace } is used to close the definition of for loop
29
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
Output:
Discussion:
In this program a class of name CharDemo2 is declared then its definition starts.
Its definition is enclosed between the opening and closing of curly braces {}. Then main method
of no return type is declared. This method is static and public. Its definition is enclosed between
the opening and closing curly braces {}. In this code ch1 is first given the value X. then it is
incremented. Then it displays the incremented value of ch1 i.e the character in ASCII sequence.
30
Program 10:
Write a program that demonstrate the use of Boolean values.
Solution:
class BoolTest {
/* this line contains a keyword class that shows that a class is going to start and BoolTest is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main(String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
boolean b;
//booleab b is declared . Semicolon is used to terminate the statement/line
b = false;
/* bolean of varable b is initialized to false. Semicolon is used to terminate the statement/line*/
System.out.println("b is: " + b);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of b */
b = true;
//now Boolean is initialized to true. Semicolon is used to terminate the statement/line
System.out.println("b is: " + b);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of b*/
if(b)
31
// a boolean value can control the if statement
System.out.println("This statement is executed ");
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets */
b = false;
//now Boolean is initialized to false. Semicolon is used to terminate the statement/line
if(b)
// a boolean value can control the if statement.
System.out.println("This statement is not executed ");
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets which will not be displayed because the above if statement is
not fulfilled. Semicolon is used to terminate the statement/line */
System.out.println("10 > 9 is " + (10 > 9));
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of 10 > 9 and outcome of this
relational operator is a boolean value. Semicolon is used to terminate the statement/line */
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
32
Output:
Discussion:
In this program a class of name BoolTest is declared then its definition starts. Its
definition is enclosed between the opening and closing of curly braces {}. Then main method of
no return type is declared. This method is static and public. Its definition is enclosed between the
opening and closing curly braces {}. It demonstrate the Boolean values it can control the if
statement. Then it displays the outcome of a relational operator i.e a Boolean value
Program 11:
Write a program that demonstrate the use of dynamically initialized
values.
Solution:
class DynInit {
/* this line contains a keyword class that shows that a class is going to start and DynInit is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main(String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
33
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
double a = 3.0, b = 4.0;
/*initialized two variables of type double. Semicolon is used to terminate the statement/line*/
double c = Math.sqrt(a * a + b * b);
/* c is dynamically initialized. Math.sqrt is used to take the square root of value which we get
after multiplying a to a and b to b then adding them we get square root. Semicolon is used to
terminate the statement/line*/
System.out.println("Hypotenuse is: " + c);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of c*/
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
Output:
34
Discussion:
In this program three local variables a, b and c are declared. C is dynamically initialized
that is by calling the function of square root i.e Math.Sqrt. at the end it displays the value of
hypotenuse that is stored in c.
Program 12:
Write a program that demonstrate the scope of block
Solution:
class Scope {
/* this line contains a keyword class that shows that a class is going to start and Scope is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main(String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
int x = 10;
/* a variable x of type int is initialized to 10. Semicolon is used to terminate the statement/line */
if(x == 10) {
/* start new scope followed by the if condition if it is true then next code will be executed.
Semicolon is used to terminate the statement/line*/
int y = 20;
/*initializes a variable of type int known only to this block. Semicolon is used to terminate the
statement/line*/
System.out.println("X and Y: " + x + " and " + y);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
35
print the string written in brackets and also print out the value of x and y. x and y both known here
*/
x = y * 2;
// x will store the value twice of y. Semicolon is used to terminate the statement/line
} // end of block scope
y = 100;
/* Error y not known here because y is initialized after the block scope as it is declared inside the
block scope. Semicolon is used to terminate the statement/line*/
System.out.println("x is " + x);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of x. x is still known here because it
is declared outside the block scope*/
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
Output:
36
Discussion:
In this program the scope of a variable is described. Firstly a variable x of type int is
declared that is known to all code within main. Then it is initialized. Then another scope of if
statement is started. Then a variable y is declared that is known only to the block of if. Then their
values are displayed. Then the block is closed. After that y is again initialized but it will display
error that y is not known here but x is still known here because it is declared within the main not
in the block of if.
Program 13:
Write a program that demonstrate the life time of variable.
Solution:
class LifeTime {
/* this line contains a keyword class that shows that a class is going to start and LifeTime is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main(String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
int x;
//a variable x of type int is declared. Semicolon is used to terminate the statement/line
for(x = 0; x<3; x++) {
/*a for loop is declared here it contains three parts initialization i.e initializes x to 0, condition
i.e x is less than 3, iteration i.e increment of x. Firstly loop initialize x to zero and then it will
check the condition that either it is true or not, if it is true then the next statement will be
compiled, after that control/system will pass to the iteration process then it will check the
condition if it is fulfilled then the statement given below will be executed again i.e it will go on
increasing the value of x by 1 until the condition is not satisfied i.e x<10. Opening curly brace {
will separate the block of code of for loop i.e it means begin of block*/
int y = -1; // y is initialized each time block is entered;
37
System.out.println("Y is : " + y);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of y */
y = 10; // changes the value of y by 10 i.e its life time is
changed
System.out.println("Y is now: " + y);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of y */
} // closing curly brace } is used to close the definition of for loop
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
Output:
38
Discussion:
In this program a class of name LifeTime is declared then its definition starts. Its
definition is enclosed between the opening and closing of curly braces {}. Then main method of
no return type is declared. This method is static and public. Its definition is enclosed between the
opening and closing curly braces {}. It demonstrate the lifetime of a variable. Variable x is
declared in the main but the 2nd
variable y is initialized each time block is entered. In the block of
for loop value of y can be changed and displayed not outside it.
Program14:
Write a code that demonstrate the basic arithmetic operators.
Solution:
class BasicMath {
/* this line contains a keyword class that shows that a class is going to start and BasicMath is
the name of this class. Opening curly brace { shows that form here a class’ definition is going
to begin */
public static void main(String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
int a = 1 + 1;
/* addition arithmetic operation using integers. Variable a of type int is declared and initialized.
Semicolon is used to terminate the statement/line*/
int b = a * 3;
/* multiplication arithmetic operation using integers. Variable b of type int is declared and
initialized. Semicolon is used to terminate the statement/line*/
int c = b / 4;
/* division arithmetic operation using integers. Variable c of type int is declared and initialized.
Semicolon is used to terminate the statement/line*/
39
int d = c - a;
/* subtraction arithmetic operation using integers. Variable d of type int is declared and
initialized. Semicolon is used to terminate the statement/line*/
int e = -d;
/*variable e of type int is initialized and stores the negative value of d. Semicolon is used to
terminate the statement/line*/
System.out.println("a = " + a);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of a */
System.out.println("b = " + b);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of b */
System.out.println("c = " + c);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of c */
System.out.println("d = " + d);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of d */
System.out.println("e = " + e);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of e */
double da = 1 + 1;
/* addition arithmetic operation using double. Variable da of type double is declared and
initialized. Semicolon is used to terminate the statement/line*/
double db = da * 3;
40
/* multiplication arithmetic operation using double. Variable db of type double is declared and
initialized. Semicolon is used to terminate the statement/line*/
double dc = db / 4;
/* division arithmetic operation using double. Variable dc of type double is declared and
initialized. Semicolon is used to terminate the statement/line*/
double dd = dc - a;
/* subtraction arithmetic operation using double. Variable dd of type double is declared and
initialized. Semicolon is used to terminate the statement/line*/
double de = -dd;
/*variable de of type int is initialized and stores the negative value of dd. Semicolon is used to
terminate the statement/line*/
System.out.println("da = " + da);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of da */
System.out.println("db = " + db);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of db */
System.out.println("dc = " + dc);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of dc */
System.out.println("dd = " + dd);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of dd */
System.out.println("de = " + de);
41
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of de */
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
Output:
Discussion:
This program performs the basic arithmetic operations of multiplication, division,
subtraction and addition using variables of type double and int and displays them.
42
Program 15:
Write a program that demonstrates the use of modulus operator.
Solution:
class modulus {
/* this line contains a keyword class that shows that a class is going to start and modulus is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main ( String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
int x = 42;
double y = 42.25;
/* variables x and y of type int & double are initialized and declared respectively. Semicolon is
used to terminate the statement/line*/
System.out.println("x mod 10 = " + x % 10);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of x % 10 */
System.out.println("y mod 10 = " + y % 10);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of y % 10 */
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
43
Output:
Discussion:
In this program a class of name modulus is declared then its definition starts. Its
definition is enclosed between the opening and closing of curly braces {}. Then main method of
no return type is declared. This method is static and public. Its definition is enclosed between the
opening and closing curly braces {}. It displays the value of x and y’s modulus 10.
Program 16:
Write a program that demonstrates the use of assignment operator.
Solution:
class OpEquals {
/* this line contains a keyword class that shows that a class is going to start and OpEquals is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main(String args[]) {
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or access, means this class is accessible out of the class in which it is declared i.e
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating an instance of that class. Void means this main() method has no return
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that form here the method’s definition is going to begin */
44
int a = 1;
/* a variable a of type int in initialized and declared. Semicolon is used to terminate the
statement/line*/
int b = 2;
/* a variable b of type int in initialized and declared. Semicolon is used to terminate the
statement/line*/
int c = 3;
/* a variable c of type int in initialized and declared. Semicolon is used to terminate the
statement/line*/
int d = 4;
/* a variable d of type int in initialized and declared. Semicolon is used to terminate the
statement/line*/
int e = 5;
/* a variable e of type int in initialized and declared. Semicolon is used to terminate the
statement/line*/
int f = 5;
/* a variable f of type int in initialized and declared. Semicolon is used to terminate the
statement/line*/
a += 5;
// increment in a by 5. Semicolon is used to terminate the statement/line
b *= 4;
// multiply b by 4. Semicolon is used to terminate the statement/line
c /= 6;
// divide c by 6. Semicolon is used to terminate the statement/line
d %= 8;
// modulus of d by 8. Semicolon is used to terminate the statement/line
f -= 10;
45
// decrement in f by 5. Semicolon is used to terminate the statement/line
e += d * c;
// increment in e by d * c. Semicolon is used to terminate the statement/line
System.out.println(" a = " + a);
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to print or display & ln is used to goto the next line. This statement of code will
print the string written in brackets and also print out the value of a. Semicolon is used to
terminate the statement/line */
System.out.println(" b = " + b);
/* This statement of code will print the string written in brackets and also print out the value of b.
Semicolon is used to terminate the statement/line */
System.out.println(" c = " + c);
/* This statement of code will print the string written in brackets and also print out the value of c.
Semicolon is used to terminate the statement/line */
System.out.println(" d = " + d);
/* This statement of code will print the string written in brackets and also print out the value of d.
Semicolon is used to terminate the statement/line */
System.out.println(" e = " + e);
/* This statement of code will print the string written in brackets and also print out the value of e.
Semicolon is used to terminate the statement/line */
System.out.println(" f = " + f);
/* This statement of code will print the string written in brackets and also print out the value of f.
Semicolon is used to terminate the statement/line */
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close the definition of class
46
Output:
Discussion:
In this program a class of name OpEquals is declared then its definition starts.
Its definition is enclosed between the opening and closing of curly braces {}. Then main method
of no return type is declared. This method is static and public. Its definition is enclosed between
the opening and closing curly braces {}. It demonstrate several assignment operators i.e += , *=,
%= and displays their values
Program 17:
Write a program that demonstrates the use of increment operator.
Solution:
class IncDec {
/* this line contains a keyword class that shows that a class is going to start and IncDec is the
name of this class. Opening curly brace { shows that form here a class’ definition is going to
begin */
public static void main (String args[]) {
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs
Java Manual with 60+ Programs

More Related Content

What's hot

Deployment guide series ibm tivoli composite application manager for web reso...
Deployment guide series ibm tivoli composite application manager for web reso...Deployment guide series ibm tivoli composite application manager for web reso...
Deployment guide series ibm tivoli composite application manager for web reso...Banking at Ho Chi Minh city
 
Java how to_program__7th_edition
Java how to_program__7th_editionJava how to_program__7th_edition
Java how to_program__7th_editionABDUmomo
 
CAST_CBOK_Ver_6-2 2010.09.17
CAST_CBOK_Ver_6-2 2010.09.17CAST_CBOK_Ver_6-2 2010.09.17
CAST_CBOK_Ver_6-2 2010.09.17Tasha Howle
 
Qtp Tutorial
Qtp TutorialQtp Tutorial
Qtp Tutorialseshuu
 
FCC Interop Board Final Report 05 22 12
FCC Interop Board Final Report 05 22 12FCC Interop Board Final Report 05 22 12
FCC Interop Board Final Report 05 22 12Claudio Lucente
 
Z commerce-for-the-cloud-blueprint
Z commerce-for-the-cloud-blueprintZ commerce-for-the-cloud-blueprint
Z commerce-for-the-cloud-blueprintArief Wicaksono
 
100PercentPureJavaCookbook-4_1_1
100PercentPureJavaCookbook-4_1_1100PercentPureJavaCookbook-4_1_1
100PercentPureJavaCookbook-4_1_1AbrarMoiz
 
Deploying GFI EventsManager™
Deploying GFI EventsManager™Deploying GFI EventsManager™
Deploying GFI EventsManager™GFI Software
 
Let us c++ yeshwant kanetkar
Let us c++ yeshwant kanetkarLet us c++ yeshwant kanetkar
Let us c++ yeshwant kanetkarVinayak Mishra
 
PostgreSQL 12 New Features with Examples (English) GA
PostgreSQL 12 New Features with Examples (English) GAPostgreSQL 12 New Features with Examples (English) GA
PostgreSQL 12 New Features with Examples (English) GANoriyoshi Shinoda
 

What's hot (17)

Final Report - v1.0
Final Report - v1.0Final Report - v1.0
Final Report - v1.0
 
Supplemental pack ddk
Supplemental pack ddkSupplemental pack ddk
Supplemental pack ddk
 
Deployment guide series ibm tivoli composite application manager for web reso...
Deployment guide series ibm tivoli composite application manager for web reso...Deployment guide series ibm tivoli composite application manager for web reso...
Deployment guide series ibm tivoli composite application manager for web reso...
 
Recommend_Cases
Recommend_CasesRecommend_Cases
Recommend_Cases
 
Java how to_program__7th_edition
Java how to_program__7th_editionJava how to_program__7th_edition
Java how to_program__7th_edition
 
CSTE_CBOK_V6-2
CSTE_CBOK_V6-2CSTE_CBOK_V6-2
CSTE_CBOK_V6-2
 
CAST_CBOK_Ver_6-2 2010.09.17
CAST_CBOK_Ver_6-2 2010.09.17CAST_CBOK_Ver_6-2 2010.09.17
CAST_CBOK_Ver_6-2 2010.09.17
 
Qtp Tutorial
Qtp TutorialQtp Tutorial
Qtp Tutorial
 
Helicopter Safety Study 3 (HSS-3)
Helicopter Safety Study 3 (HSS-3)Helicopter Safety Study 3 (HSS-3)
Helicopter Safety Study 3 (HSS-3)
 
Systems se
Systems seSystems se
Systems se
 
FCC Interop Board Final Report 05 22 12
FCC Interop Board Final Report 05 22 12FCC Interop Board Final Report 05 22 12
FCC Interop Board Final Report 05 22 12
 
Z commerce-for-the-cloud-blueprint
Z commerce-for-the-cloud-blueprintZ commerce-for-the-cloud-blueprint
Z commerce-for-the-cloud-blueprint
 
100PercentPureJavaCookbook-4_1_1
100PercentPureJavaCookbook-4_1_1100PercentPureJavaCookbook-4_1_1
100PercentPureJavaCookbook-4_1_1
 
Bullet Physic Engine SDK
Bullet Physic Engine SDKBullet Physic Engine SDK
Bullet Physic Engine SDK
 
Deploying GFI EventsManager™
Deploying GFI EventsManager™Deploying GFI EventsManager™
Deploying GFI EventsManager™
 
Let us c++ yeshwant kanetkar
Let us c++ yeshwant kanetkarLet us c++ yeshwant kanetkar
Let us c++ yeshwant kanetkar
 
PostgreSQL 12 New Features with Examples (English) GA
PostgreSQL 12 New Features with Examples (English) GAPostgreSQL 12 New Features with Examples (English) GA
PostgreSQL 12 New Features with Examples (English) GA
 

Viewers also liked

Digital logic and design's Lab 3
Digital logic and design's Lab 3Digital logic and design's Lab 3
Digital logic and design's Lab 3Farwa Ansari
 
Digital logic and design's Lab 4 nand
Digital logic and design's Lab 4 nandDigital logic and design's Lab 4 nand
Digital logic and design's Lab 4 nandFarwa Ansari
 
Summary of Simultaneous Multithreading: Maximizing On-Chip Parallelism
Summary of Simultaneous Multithreading: Maximizing On-Chip ParallelismSummary of Simultaneous Multithreading: Maximizing On-Chip Parallelism
Summary of Simultaneous Multithreading: Maximizing On-Chip ParallelismFarwa Ansari
 
Javadocx j option pane
Javadocx j option paneJavadocx j option pane
Javadocx j option paneFarwa Ansari
 
Linear combination of vector
Linear combination of vectorLinear combination of vector
Linear combination of vectorFarwa Ansari
 
Raster images (assignment)
Raster images (assignment)Raster images (assignment)
Raster images (assignment)Farwa Ansari
 
Applications of Image Processing
Applications of Image ProcessingApplications of Image Processing
Applications of Image ProcessingFarwa Ansari
 
Cohen sutherland algorithm
Cohen sutherland algorithmCohen sutherland algorithm
Cohen sutherland algorithmFarwa Ansari
 
Prefix and suffix of open gl
Prefix and suffix of open glPrefix and suffix of open gl
Prefix and suffix of open glFarwa Ansari
 
Tomasulo Algorithm
Tomasulo AlgorithmTomasulo Algorithm
Tomasulo AlgorithmFarwa Ansari
 
Chapter 4: Lexical & Syntax Analysis (Programming Exercises)
Chapter 4: Lexical & Syntax Analysis (Programming Exercises)Chapter 4: Lexical & Syntax Analysis (Programming Exercises)
Chapter 4: Lexical & Syntax Analysis (Programming Exercises)Farwa Ansari
 
Mission statement and Vision statement of 3 Different Companies
Mission statement and Vision statement of 3 Different CompaniesMission statement and Vision statement of 3 Different Companies
Mission statement and Vision statement of 3 Different CompaniesFarwa Ansari
 
IPv6 Implementation challenges
IPv6 Implementation challengesIPv6 Implementation challenges
IPv6 Implementation challengesFarwa Ansari
 
Implementation & Challenges of IPv6
Implementation & Challenges of IPv6Implementation & Challenges of IPv6
Implementation & Challenges of IPv6 Farwa Ansari
 
DLDLab 8 half adder
DLDLab 8 half adderDLDLab 8 half adder
DLDLab 8 half adderFarwa Ansari
 
Graphic display devices
Graphic display devicesGraphic display devices
Graphic display devicesFarwa Ansari
 
Memory Hierarchy Design, Basics, Cache Optimization, Address Translation
Memory Hierarchy Design, Basics, Cache Optimization, Address TranslationMemory Hierarchy Design, Basics, Cache Optimization, Address Translation
Memory Hierarchy Design, Basics, Cache Optimization, Address TranslationFarwa Ansari
 
Hacking and Hackers
Hacking and HackersHacking and Hackers
Hacking and HackersFarwa Ansari
 

Viewers also liked (20)

Digital logic and design's Lab 3
Digital logic and design's Lab 3Digital logic and design's Lab 3
Digital logic and design's Lab 3
 
Digital logic and design's Lab 4 nand
Digital logic and design's Lab 4 nandDigital logic and design's Lab 4 nand
Digital logic and design's Lab 4 nand
 
Summary of Simultaneous Multithreading: Maximizing On-Chip Parallelism
Summary of Simultaneous Multithreading: Maximizing On-Chip ParallelismSummary of Simultaneous Multithreading: Maximizing On-Chip Parallelism
Summary of Simultaneous Multithreading: Maximizing On-Chip Parallelism
 
Javadocx j option pane
Javadocx j option paneJavadocx j option pane
Javadocx j option pane
 
Linear combination of vector
Linear combination of vectorLinear combination of vector
Linear combination of vector
 
Raster images (assignment)
Raster images (assignment)Raster images (assignment)
Raster images (assignment)
 
Applications of Image Processing
Applications of Image ProcessingApplications of Image Processing
Applications of Image Processing
 
Cohen sutherland algorithm
Cohen sutherland algorithmCohen sutherland algorithm
Cohen sutherland algorithm
 
Scaling
ScalingScaling
Scaling
 
Prefix and suffix of open gl
Prefix and suffix of open glPrefix and suffix of open gl
Prefix and suffix of open gl
 
Templates
TemplatesTemplates
Templates
 
Tomasulo Algorithm
Tomasulo AlgorithmTomasulo Algorithm
Tomasulo Algorithm
 
Chapter 4: Lexical & Syntax Analysis (Programming Exercises)
Chapter 4: Lexical & Syntax Analysis (Programming Exercises)Chapter 4: Lexical & Syntax Analysis (Programming Exercises)
Chapter 4: Lexical & Syntax Analysis (Programming Exercises)
 
Mission statement and Vision statement of 3 Different Companies
Mission statement and Vision statement of 3 Different CompaniesMission statement and Vision statement of 3 Different Companies
Mission statement and Vision statement of 3 Different Companies
 
IPv6 Implementation challenges
IPv6 Implementation challengesIPv6 Implementation challenges
IPv6 Implementation challenges
 
Implementation & Challenges of IPv6
Implementation & Challenges of IPv6Implementation & Challenges of IPv6
Implementation & Challenges of IPv6
 
DLDLab 8 half adder
DLDLab 8 half adderDLDLab 8 half adder
DLDLab 8 half adder
 
Graphic display devices
Graphic display devicesGraphic display devices
Graphic display devices
 
Memory Hierarchy Design, Basics, Cache Optimization, Address Translation
Memory Hierarchy Design, Basics, Cache Optimization, Address TranslationMemory Hierarchy Design, Basics, Cache Optimization, Address Translation
Memory Hierarchy Design, Basics, Cache Optimization, Address Translation
 
Hacking and Hackers
Hacking and HackersHacking and Hackers
Hacking and Hackers
 

Similar to Java Manual with 60+ Programs

2014-07-30 defense in depth scap workbook
2014-07-30 defense in depth scap workbook2014-07-30 defense in depth scap workbook
2014-07-30 defense in depth scap workbookShawn Wells
 
Program Directory For CBPDO Installation and ServerPac Reference z/OS
Program Directory For CBPDO Installation and ServerPac Reference z/OSProgram Directory For CBPDO Installation and ServerPac Reference z/OS
Program Directory For CBPDO Installation and ServerPac Reference z/OSIBM India Smarter Computing
 
Book.opensuse.startup en
Book.opensuse.startup enBook.opensuse.startup en
Book.opensuse.startup enNaldo Santos
 
UITECH_OP1909_latest.pdf
UITECH_OP1909_latest.pdfUITECH_OP1909_latest.pdf
UITECH_OP1909_latest.pdfpraveenRapeti3
 
Rapid programmering start
Rapid programmering startRapid programmering start
Rapid programmering startZiaul Haque
 
Online Hotel Room Booking System
Online Hotel Room Booking SystemOnline Hotel Room Booking System
Online Hotel Room Booking SystemAbhishek Kumar
 
ConSteel_14_User_Manual-1-50.pdf
ConSteel_14_User_Manual-1-50.pdfConSteel_14_User_Manual-1-50.pdf
ConSteel_14_User_Manual-1-50.pdfJuanUnafVargas
 
programación en prolog
programación en prologprogramación en prolog
programación en prologAlex Pin
 
REMOTE RADIO HEAD TESTING: 5G case study
REMOTE RADIO HEAD TESTING: 5G  case studyREMOTE RADIO HEAD TESTING: 5G  case study
REMOTE RADIO HEAD TESTING: 5G case studyJOSE T Y
 
Intelligent Routine Generator
Intelligent Routine Generator Intelligent Routine Generator
Intelligent Routine Generator Mohammad Apu
 

Similar to Java Manual with 60+ Programs (20)

Sap hana master guide
Sap hana master guideSap hana master guide
Sap hana master guide
 
2014-07-30 defense in depth scap workbook
2014-07-30 defense in depth scap workbook2014-07-30 defense in depth scap workbook
2014-07-30 defense in depth scap workbook
 
Program Directory For CBPDO Installation and ServerPac Reference z/OS
Program Directory For CBPDO Installation and ServerPac Reference z/OSProgram Directory For CBPDO Installation and ServerPac Reference z/OS
Program Directory For CBPDO Installation and ServerPac Reference z/OS
 
Book.opensuse.startup en
Book.opensuse.startup enBook.opensuse.startup en
Book.opensuse.startup en
 
Solution master guide.pdf
Solution master guide.pdfSolution master guide.pdf
Solution master guide.pdf
 
UITECH_OP1909_latest.pdf
UITECH_OP1909_latest.pdfUITECH_OP1909_latest.pdf
UITECH_OP1909_latest.pdf
 
ISVForce Guide NEW
ISVForce Guide NEWISVForce Guide NEW
ISVForce Guide NEW
 
J raven user guide
J raven user guideJ raven user guide
J raven user guide
 
Rapid programmering start
Rapid programmering startRapid programmering start
Rapid programmering start
 
Program Directory for IBM Ported Tools for z/OS
Program Directory for IBM Ported Tools for z/OSProgram Directory for IBM Ported Tools for z/OS
Program Directory for IBM Ported Tools for z/OS
 
Online Hotel Room Booking System
Online Hotel Room Booking SystemOnline Hotel Room Booking System
Online Hotel Room Booking System
 
tr-4308
tr-4308tr-4308
tr-4308
 
Sap setup guide
Sap setup guideSap setup guide
Sap setup guide
 
Open acc.1.0
Open acc.1.0Open acc.1.0
Open acc.1.0
 
ConSteel_14_User_Manual-1-50.pdf
ConSteel_14_User_Manual-1-50.pdfConSteel_14_User_Manual-1-50.pdf
ConSteel_14_User_Manual-1-50.pdf
 
Java completed assignment
Java completed assignmentJava completed assignment
Java completed assignment
 
programación en prolog
programación en prologprogramación en prolog
programación en prolog
 
REMOTE RADIO HEAD TESTING: 5G case study
REMOTE RADIO HEAD TESTING: 5G  case studyREMOTE RADIO HEAD TESTING: 5G  case study
REMOTE RADIO HEAD TESTING: 5G case study
 
Intelligent Routine Generator
Intelligent Routine Generator Intelligent Routine Generator
Intelligent Routine Generator
 
Swi prolog-6.2.6
Swi prolog-6.2.6Swi prolog-6.2.6
Swi prolog-6.2.6
 

More from Farwa Ansari

Energy Harvesting Techniques in Wireless Sensor Networks – A Survey
Energy Harvesting Techniques in Wireless Sensor Networks – A SurveyEnergy Harvesting Techniques in Wireless Sensor Networks – A Survey
Energy Harvesting Techniques in Wireless Sensor Networks – A SurveyFarwa Ansari
 
Micro-services architecture
Micro-services architectureMicro-services architecture
Micro-services architectureFarwa Ansari
 
Software Design Patterns - An Overview
Software Design Patterns - An OverviewSoftware Design Patterns - An Overview
Software Design Patterns - An OverviewFarwa Ansari
 
Optimizing the memory management of a virtual machine monitor on a NUMA syste...
Optimizing the memory management of a virtual machine monitor on a NUMA syste...Optimizing the memory management of a virtual machine monitor on a NUMA syste...
Optimizing the memory management of a virtual machine monitor on a NUMA syste...Farwa Ansari
 
Fault Tolerance Typed Assembly Language - A graphical overview
Fault Tolerance Typed Assembly Language - A graphical overviewFault Tolerance Typed Assembly Language - A graphical overview
Fault Tolerance Typed Assembly Language - A graphical overviewFarwa Ansari
 
Comparative Analysis of Face Recognition Methodologies and Techniques
Comparative Analysis of Face Recognition Methodologies and TechniquesComparative Analysis of Face Recognition Methodologies and Techniques
Comparative Analysis of Face Recognition Methodologies and TechniquesFarwa Ansari
 
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)Farwa Ansari
 
Business plan of a software house
Business plan of a software houseBusiness plan of a software house
Business plan of a software houseFarwa Ansari
 

More from Farwa Ansari (9)

Energy Harvesting Techniques in Wireless Sensor Networks – A Survey
Energy Harvesting Techniques in Wireless Sensor Networks – A SurveyEnergy Harvesting Techniques in Wireless Sensor Networks – A Survey
Energy Harvesting Techniques in Wireless Sensor Networks – A Survey
 
Micro-services architecture
Micro-services architectureMicro-services architecture
Micro-services architecture
 
Software Design Patterns - An Overview
Software Design Patterns - An OverviewSoftware Design Patterns - An Overview
Software Design Patterns - An Overview
 
Optimizing the memory management of a virtual machine monitor on a NUMA syste...
Optimizing the memory management of a virtual machine monitor on a NUMA syste...Optimizing the memory management of a virtual machine monitor on a NUMA syste...
Optimizing the memory management of a virtual machine monitor on a NUMA syste...
 
Fault Tolerance Typed Assembly Language - A graphical overview
Fault Tolerance Typed Assembly Language - A graphical overviewFault Tolerance Typed Assembly Language - A graphical overview
Fault Tolerance Typed Assembly Language - A graphical overview
 
Comparative Analysis of Face Recognition Methodologies and Techniques
Comparative Analysis of Face Recognition Methodologies and TechniquesComparative Analysis of Face Recognition Methodologies and Techniques
Comparative Analysis of Face Recognition Methodologies and Techniques
 
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
 
Business plan of a software house
Business plan of a software houseBusiness plan of a software house
Business plan of a software house
 
Dld (lab 1 & 2)
Dld (lab 1 & 2)Dld (lab 1 & 2)
Dld (lab 1 & 2)
 

Recently uploaded

HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 

Recently uploaded (20)

HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 

Java Manual with 60+ Programs

  • 1. Java Manual Manual of JAVA 2 This manual contains more than 60 programs of JAVA 2 2014 Farwa Ansari NFC, FSD 8/10/2014
  • 2. 2 Table of Contents Program 1:...................................................................................................................................................12 Solution:......................................................................................................................................................12 Output: ........................................................................................................................................................13 Discussion:..................................................................................................................................................13 Program 2:...................................................................................................................................................13 Solution:......................................................................................................................................................13 Output: ........................................................................................................................................................14 Discussion:..................................................................................................................................................15 Program 3:...................................................................................................................................................15 Solution:......................................................................................................................................................15 Output: ........................................................................................................................................................17 Discussion:..................................................................................................................................................17 Program 4:...................................................................................................................................................17 Solution:......................................................................................................................................................17 Output: ........................................................................................................................................................19 Discussion:..................................................................................................................................................19 Program 5:...................................................................................................................................................19 Solution:......................................................................................................................................................19 Output: ........................................................................................................................................................21 Discussion:..................................................................................................................................................21 Program 6:...................................................................................................................................................22 Solution:......................................................................................................................................................22 Output: ........................................................................................................................................................23 Discussion:..................................................................................................................................................23 Program 7:...................................................................................................................................................24 Solution:......................................................................................................................................................24 Output: ........................................................................................................................................................25 Discussion:..................................................................................................................................................25 Program 8:...................................................................................................................................................25 Solution:......................................................................................................................................................25 Output: ........................................................................................................................................................26
  • 3. 3 Discussion:..................................................................................................................................................27 Program 9:...................................................................................................................................................27 Solution:......................................................................................................................................................27 Output: ........................................................................................................................................................29 Discussion:..................................................................................................................................................29 Program 10:.................................................................................................................................................30 Solution:......................................................................................................................................................30 Output: ........................................................................................................................................................32 Discussion:..................................................................................................................................................32 Program 11:.................................................................................................................................................32 Solution:......................................................................................................................................................32 Output: ........................................................................................................................................................33 Discussion:..................................................................................................................................................34 Program 12:.................................................................................................................................................34 Solution:......................................................................................................................................................34 Output: ........................................................................................................................................................35 Discussion:..................................................................................................................................................36 Program 13:.................................................................................................................................................36 Solution:......................................................................................................................................................36 Output: ........................................................................................................................................................37 Discussion:..................................................................................................................................................38 Program14:..................................................................................................................................................38 Solution:......................................................................................................................................................38 Output: ........................................................................................................................................................41 Discussion:..................................................................................................................................................41 Program 15:.................................................................................................................................................42 Solution:......................................................................................................................................................42 Output: ........................................................................................................................................................43 Discussion:..................................................................................................................................................43 Program 16:.................................................................................................................................................43 Solution:......................................................................................................................................................43 Output: ........................................................................................................................................................46
  • 4. 4 Discussion:..................................................................................................................................................46 Program 17:.................................................................................................................................................46 Solution:......................................................................................................................................................46 Output: ........................................................................................................................................................48 Discussion:..................................................................................................................................................48 Program 18:.................................................................................................................................................48 Solution:......................................................................................................................................................48 Output: ........................................................................................................................................................50 Discussion:..................................................................................................................................................50 Program 19:.................................................................................................................................................50 Solution:......................................................................................................................................................50 Output: ........................................................................................................................................................52 Discussion:..................................................................................................................................................52 Program 20:.................................................................................................................................................52 Solution:......................................................................................................................................................52 Output: ........................................................................................................................................................55 Discussion:..................................................................................................................................................55 Program 21:.................................................................................................................................................55 Solution:......................................................................................................................................................55 Output: ........................................................................................................................................................57 Discussion:..................................................................................................................................................58 Program 22:.................................................................................................................................................58 Solution:......................................................................................................................................................58 Output: ........................................................................................................................................................60 Discussion:..................................................................................................................................................60 Program 23:.................................................................................................................................................60 Solution:......................................................................................................................................................61 Output: ........................................................................................................................................................63 Discussion:..................................................................................................................................................63 Program 24:.................................................................................................................................................63 Solution:......................................................................................................................................................63 Output: ........................................................................................................................................................66
  • 5. 5 Discussion:..................................................................................................................................................66 Program 25:.................................................................................................................................................66 Solution:......................................................................................................................................................66 Output: ........................................................................................................................................................69 Discussion:..................................................................................................................................................69 Program 26:.................................................................................................................................................69 Solution:......................................................................................................................................................69 Output: ........................................................................................................................................................71 Discussion:..................................................................................................................................................71 Program 27:.................................................................................................................................................71 Solution:......................................................................................................................................................71 Output: ........................................................................................................................................................73 Discussion:..................................................................................................................................................73 Program 28:.................................................................................................................................................73 Solution:......................................................................................................................................................73 Output: ........................................................................................................................................................76 Discussion:..................................................................................................................................................76 Program 29:.................................................................................................................................................76 Solution:......................................................................................................................................................76 Output: ........................................................................................................................................................78 Discussion: ..................................................................................................................................................78 Program 30:.................................................................................................................................................78 Solution:......................................................................................................................................................78 Output: ........................................................................................................................................................79 Discussion:..................................................................................................................................................79 Program 31:.................................................................................................................................................80 Solution:......................................................................................................................................................80 Output: ........................................................................................................................................................81 Discussion:..................................................................................................................................................81 Program 32:.................................................................................................................................................81 Solution:......................................................................................................................................................81 Output: ........................................................................................................................................................83
  • 6. 6 Discussion:..................................................................................................................................................83 Program 33:.................................................................................................................................................83 Solution:......................................................................................................................................................83 Output: ........................................................................................................................................................84 Discussion:..................................................................................................................................................84 Program 34:.................................................................................................................................................85 Solution:......................................................................................................................................................85 Output: ........................................................................................................................................................88 Discussion:..................................................................................................................................................88 Program 35:.................................................................................................................................................88 Solution:......................................................................................................................................................88 Output: ........................................................................................................................................................90 Discussion:..................................................................................................................................................90 Program 36:.................................................................................................................................................91 Solution:......................................................................................................................................................91 Output: ........................................................................................................................................................94 Discussion:..................................................................................................................................................94 Program 37:.................................................................................................................................................94 Solution:......................................................................................................................................................94 Output: ........................................................................................................................................................96 Discussion:..................................................................................................................................................96 Program 38:.................................................................................................................................................96 Solution:......................................................................................................................................................96 Output: ........................................................................................................................................................98 Discussion:..................................................................................................................................................98 Program 39:.................................................................................................................................................99 Solution:......................................................................................................................................................99 Output: ......................................................................................................................................................101 Discussion:................................................................................................................................................101 Program 40:...............................................................................................................................................101 Solution:....................................................................................................................................................101 Output: ......................................................................................................................................................103
  • 7. 7 Discussion:................................................................................................................................................103 Program 41:...............................................................................................................................................103 Solution:....................................................................................................................................................103 Output: ......................................................................................................................................................105 Discussion:................................................................................................................................................105 Program 42:...............................................................................................................................................105 Solution:....................................................................................................................................................105 Output: ......................................................................................................................................................107 Discussion:................................................................................................................................................107 Program 43:...............................................................................................................................................107 Solution:....................................................................................................................................................107 Output: ......................................................................................................................................................110 Discussion:................................................................................................................................................110 Program 44:...............................................................................................................................................110 Solution:....................................................................................................................................................110 Output: ......................................................................................................................................................111 Discussion:................................................................................................................................................111 Program 45:...............................................................................................................................................112 Solution:....................................................................................................................................................112 Output: ......................................................................................................................................................113 Discussion:................................................................................................................................................113 Program 46:...............................................................................................................................................113 Solution:....................................................................................................................................................113 Output: ......................................................................................................................................................114 Discussion:................................................................................................................................................114 Program 47:...............................................................................................................................................115 Solution:....................................................................................................................................................115 Output: ......................................................................................................................................................116 Discussion:................................................................................................................................................116 Program 48:...............................................................................................................................................116 Solution:....................................................................................................................................................116 Output: ......................................................................................................................................................117
  • 8. 8 Discussion:................................................................................................................................................117 Program 49:...............................................................................................................................................118 Solution:....................................................................................................................................................118 Output: ......................................................................................................................................................119 Discussion:................................................................................................................................................119 Program 50:...............................................................................................................................................119 Solution:....................................................................................................................................................119 Output: ......................................................................................................................................................120 Discussion:................................................................................................................................................120 Program 51:...............................................................................................................................................120 Solution:....................................................................................................................................................120 Output: ......................................................................................................................................................122 Discussion:................................................................................................................................................122 Program 52:...............................................................................................................................................122 Solution:....................................................................................................................................................122 Output: ......................................................................................................................................................123 Discussion:................................................................................................................................................123 Program 53:...............................................................................................................................................124 Solution:....................................................................................................................................................124 Output: ......................................................................................................................................................124 Discussion:................................................................................................................................................125 Program 54:...............................................................................................................................................125 Solution:....................................................................................................................................................125 Output: ......................................................................................................................................................126 Discussion:................................................................................................................................................126 Program 55:...............................................................................................................................................127 Solution:....................................................................................................................................................127 Output: ......................................................................................................................................................127 Discussion:................................................................................................................................................128 Program 56:...............................................................................................................................................128 Solution:....................................................................................................................................................128 Output: ......................................................................................................................................................129
  • 9. 9 Discussion:................................................................................................................................................130 Program 57:...............................................................................................................................................130 Solution:....................................................................................................................................................130 Output: ......................................................................................................................................................131 Discussion:................................................................................................................................................131 Program 58:...............................................................................................................................................131 Solution:....................................................................................................................................................131 Output: ......................................................................................................................................................132 Discussion:................................................................................................................................................132 Program 59:...............................................................................................................................................132 Solution:....................................................................................................................................................133 Output: ......................................................................................................................................................133 Discussion:................................................................................................................................................134 Program 60:...............................................................................................................................................134 Solution:....................................................................................................................................................134 Output: ......................................................................................................................................................135 Discussion:................................................................................................................................................135 Program 61:...............................................................................................................................................135 Solution:....................................................................................................................................................136 Output: ......................................................................................................................................................136 Discussion:................................................................................................................................................136 Program 62:...............................................................................................................................................137 Write a program of interface.....................................................................................................................137 Solution:....................................................................................................................................................137 Output:......................................................................................................................................................139 Description:...............................................................................................................................................140 Program 63:...............................................................................................................................................140 Write a program in which one interface can extend another. ...................................................................140 Solution:....................................................................................................................................................140 Output: ......................................................................................................................................................141 Description:...............................................................................................................................................142 Program 64:...............................................................................................................................................142
  • 10. 10 Solution:....................................................................................................................................................142 Output:......................................................................................................................................................143 Discussion: ................................................................................................................................................143 Program 65:...............................................................................................................................................143 Solution:....................................................................................................................................................143 Output:......................................................................................................................................................144 Discussion: ................................................................................................................................................144 Program 66:...............................................................................................................................................144 Solution:....................................................................................................................................................144 Output:......................................................................................................................................................146 Discussion: ................................................................................................................................................146 Program 67:...............................................................................................................................................146 Solution:....................................................................................................................................................146 Output:......................................................................................................................................................147 Discussion: ................................................................................................................................................147 Program 68:...............................................................................................................................................148 Solution:....................................................................................................................................................148 Output:......................................................................................................................................................149 Discussion: ................................................................................................................................................149 Program 69:...............................................................................................................................................149 Solution:....................................................................................................................................................149 Output:......................................................................................................................................................151 Discussion: ................................................................................................................................................151 Program 70:...............................................................................................................................................151 Solution:....................................................................................................................................................151 Output:......................................................................................................................................................153 Discussion: ................................................................................................................................................153 Program 71:...............................................................................................................................................153 Solution:....................................................................................................................................................153 Output:......................................................................................................................................................154 Discussion: ................................................................................................................................................154 Program 72:...............................................................................................................................................155
  • 11. 11 Solution:....................................................................................................................................................155 Output:......................................................................................................................................................155 Discussion: ................................................................................................................................................156 Program 73:...............................................................................................................................................156 Solution:....................................................................................................................................................156 Output:......................................................................................................................................................158 Discussion: ................................................................................................................................................158 Program 74:...............................................................................................................................................158 Solution:....................................................................................................................................................158 Output:......................................................................................................................................................159 Discussion: ................................................................................................................................................159
  • 12. 12 Program 1: Write a simple program that explains the syntax of java code or demonstrate how to write it. Solution: class Example { /* this line contains a keyword class that shows that a class is going to start and Example is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ // Your program begin with a call to main. public static void main(String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ System.out.println("This is a simple java program."); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display the string passed to it & ln is used to goto the next line. Semicolon is used to terminate the statement/line */ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class
  • 13. 13 Output: Discussion: In this program a class of name Example is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. This program simply displays the string that is “This is a simple java program”. Program 2: Write a program that initializes a variable and display it times two. Solution: class Example2 { /* this line contains a keyword class that shows that a class is going to start and Example2 is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main (String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return
  • 14. 14 type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ int num; // a variable of type integer is declared num = 100; // a variable num is initialized to 100 System.out.println("num: " + num); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display the string passed to it & ln is used to goto the next line. This statement of code prints the value of num variable of type int i.e 100*/ num = num * 2; /*increases the value of num variable by 2. Semicolon is used to terminate the statement/line*/ System.out.println("num * 2: " + num); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display the string passed to it & ln is used to goto the next line. This statement of code prints the value of num times 2 i.e 200. Semicolon is used to terminate the statement/line */ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class Output:
  • 15. 15 Discussion: In this program a class of name Example2 is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. This program declares a variable called num then assigns the value to num that is 100 and then it displays the value of num by multiplying it by 2 Program 3: Write a program that demonstrate the use of “ if ” statement in java. Solution: class ifsample { /* this line contains a keyword class that shows that a class is going to start and ifsample is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main (String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ int x , y; /*two variables of type int are declared. Semicolon is used to terminate the statement/line*/ x = 10; y = 20; // variables x & y are initialized. Semicolon is used to terminate the statement/line if(x < y) /*in this line of code an if statement with a condition is declared, that if x is less than y then the next statement will be interpret. The next statement will be interpret because this condition is true*/
  • 16. 16 System.out.println (" 'X' is less than 'Y' "); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display the string passed to it & ln is used to goto the next line. This statement of code will print out that 'X' is less than 'Y' only if the above statement is true which is true. Semicolon is used to terminate the statement/line */ x = x * 2; /* increase the value of x from 10 to 20 and stores it in x. Semicolon is used to terminate the statement/line*/ if(x == y) /* an if statement with a condition is declared, that if x == y then the next statement will be interpret or displayed or execute. So the above condition is true because now x is equal to y i.e 20*/ System.out.println (" 'X' is equal to 'Y' "); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display the string passed to it & ln is used to goto the next line. This statement of code will print out that 'X' is less than 'Y' only if the above statement is true which is true already. Semicolon is used to terminate the statement/line */ x = x * 2; /* increase the value of x from 20 to 40 and stores it in x. Semicolon is used to terminate the statement/line*/ if(x == y) /*an if statement with a condition is declared, that if x == y then the next statement will be interpret but here it is not true because x is not equal to y, it is greater than y*/ System.out.println (" You won't see anything "); /*System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display the string passed to it & ln is used to goto the next line. This statement of code will not print anything because the above if statement is not true so it will exit. Semicolon is used to terminate the statement/line */ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class
  • 17. 17 Output: Discussion: In this program a class of name ifsample is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. This program declares & defines two variables then it checks if x is less than y then it displays “x is less than y”. x is incremented x times 2 it will become equal to y so it displays that “x is now equal to y”. it again incremented x and checks if x is greater than y and displays it. Program 4: Write a program that demonstrate the use of “ for ” loop in java. Solution: /* Demonstrate the for loop. Call this file "fortest.java*/ class fortest { /*this line contains a keyword class that shows that a class is going to start and fortest is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main( String args[]) {
  • 18. 18 /* this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ int x; // a variable x of type int is declared. Semicolon is used to terminate the statement/line for(x=0; x<10; x=x+1) /*a for loop is declared here it contains three parts initialization i.e initializes x to 0, condition i.e x is less than 10, iteration i.e increment of x. Firstly loop initialize x to zero and then it will check the condition that either it is true or not, if it is true then the next statement will be compiled, after that control/system will pass to the iteration process then it will check the condition if it is fulfilled then the statement given below will be executed again i.e it will go on increasing the value of x by 1 until the condition is not satisfied*/ System.out.println("This is x: " + x); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display the string passed to it & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of x until it is less than 10 i.e it will print it out only if the condition of for statement is true. Semicolon is used to terminate the statement/line */ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class
  • 19. 19 Output: Discussion: In this program a class of name fortest is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. This class displays the value of x 10 times using for loop Program 5: Write a program that uses a block of code as the target of a “for” loop. Solution: class blocktest { /* this line contains a keyword class that shows that a class is going to start and blocktest is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main(String args[]) {
  • 20. 20 /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ int x, y; // two variables x and y of type int are declared. Semicolon is used to terminate the statement/line y = 20; // variable y is initializes to 20. Semicolon is used to terminate the statement/line for ( x=0; x<10; x++) { /*a for loop is declared here it contains three parts initialization i.e initializes x to 0, condition i.e x is less than 10, iteration i.e increment of x. Firstly loop initialize x to zero and then it will check the condition that either it is true or not, if it is true then the next statement will be compiled, after that control/system will pass to the iteration process then it will check the condition if it is fulfilled then the statement given below will be executed again i.e it will go on increasing the value of x by 1 until the condition is not satisfied i.e x<10. Opening curly brace { will separate the block of code of for loop i.e it means begin of block*/ System.out.println ("This is X: " + x); System.out.println ("This is y: " + y); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display the string passed to it & ln is used to goto the next line. These statements of code will print the string written in brackets and also print out the value of x and y. Semicolon is used to terminate the statement/line */ y = y - 2; /* here Y will be decreased by 2 and then the control passes to iteration portion in for loop and then the portion below that loop will be displayed until the condition will be less than 10 i.e it will print it out only if the condition of for statement is true i.e x<10. It after displaying them once check the decrement of Y by 2 and goes on further until x<10. Semicolon is used to terminate the statement/line */
  • 21. 21 } /* closing curly brace } is used to close the definition of for loop. Closing curly brace { will separate the block of code of for loop i.e it means end of block*/ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class Output: Discussion: In this program a class of name blocktest is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. In this code the target of for loop is the block of code not just the statement of code.
  • 22. 22 Program 6: Write a program that calculates the distance traveled by light in thousand days using long variables. Solution: class Light { /* this line contains a keyword class that shows that a class is going to start and Light is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main(String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ int lightspeed = 186000; /*approximate light speed in miles per second. Lightspeed is a variable of type int. Semicolon is used to terminate the statement/line*/ long seconds, days, distance; /*three variables of type long are declared. Semicolon is used to terminate the statement/line*/ days = 1000; /*specify number of days here i.e initializes days to 1000. Semicolon is used to terminate the statement/line of code*/ seconds = days * 24 * 60 * 60; // convert to seconds. Semicolon is used to terminate the statement/line distance = lightspeed * seconds ; /* calculating distance by multiplying lightspeed & seconds. Semicolon is used to terminate the statement/line*/ System.out.print("In " + days);
  • 23. 23 /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display the string passed to it & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of days. Semicolon is used to terminate the statement/line */ System.out.print(" days light will trevel about "); System.out.println(distance + " miles"); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. These statements of code will print the string written in brackets and also print out the value of miles written in 2nd one. Semicolon is used to terminate the statement/line */ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class Output: Discussion: In this program a class of name Light is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. It computes distance light travels using 3 long variables. Then stores approximate speed of light in miles per second and specify number of days then convert to seconds and compute distance and displays it.
  • 24. 24 Program 7: Write a code to calculate area using double variables Solution: class Area { /* this line contains a keyword class that shows that a class is going to start and Area is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main(String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ double pi, r , a; /* three variables of type double are declared . Semicolon is used to terminate the statement/line*/ r = 10.8; // radius of circle . Semicolon is used to terminate the statement/line pi = 3.1416; /*approx. value of pi. Pi is declared . Semicolon is used to terminate the statement/line*/ a = pi * r * r; //area’s formula to calculate area . Semicolon is used to terminate the statement/line System.out.println("Area of circle is " + a); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display the string passed to it & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of a . Semicolon is used to terminate the statement/line */ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class
  • 25. 25 Output: Discussion: In this program a class of name Area is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. It compute area of circle Program 8: Write a program that demonstrate the use of char data type Solution: class CharDemo { /* this line contains a keyword class that shows that a class is going to start and CharDemo is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main(String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return
  • 26. 26 type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ char ch1, ch2; // two variables of type char(character). Semicolon is used to terminate the statement/line ch1 = 88; /* code for X i.e ch1 variable is defined . Semicolon is used to terminate the statement/line*/ ch2 = 'Y'; //2nd character is defined to y. Semicolon is used to terminate the statement/line System.out.println("ch1 and ch2: " + ch1 + " and " + ch2); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of ch1 and ch2 . Semicolon is used to terminate the statement/line*/ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class Output:
  • 27. 27 Discussion: In this program a class of name CharDemo is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. It demonstrate char data type ch1 is assigned the value 88 which is ASCII letter that correspond to letter X Program 9: Write a program that demonstrates how char variables behave like integers. Solution: class CharDemo2 { /* this line contains a keyword class that shows that a class is going to start and CharDemo2 is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main(String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ char ch1 = 'X'; //a variable X of char type is declared. Semicolon is used to terminate the statement/line char ch2 = 'A'; //a variable A of char type is declared. Semicolon is used to terminate the statement/line System .out.println("ch1 contains " + ch1); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of ch1. Semicolon is used to terminate the statement/line*/
  • 28. 28 ch1++; /*an increment operator is used here to increase the value of ch1. Semicolon is used to terminate the statement/line*/ System.out.println("Now ch1 contains " +ch1); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of ch1. Semicolon is used to terminate the statement/line*/ System.out.println("ch2 contains " + ch2); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of ch2. Semicolon is used to terminate the statement/line */ System.out.println(" ~~~ LOOP ~~~ "); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets. Semicolon is used to terminate the statement/line */ for (int i = 0; i<25 ; i++) { /*a for loop is declared here it contains three parts initialization i.e initializes i to 0, condition i.e i is less than 25, iteration i.e increment of i. Firstly loop initialize i to zero and then it will check the condition that either it is true or not, if it is true then the next statement will be compiled, after that control/system will pass to the iteration process then it will check the condition if it is fulfilled then the statement given below will be executed again i.e it will go on increasing the value of i by 1 until the condition is not satisfied i.e x<25. Opening curly brace { will separate the block of code of for loop i.e it means begin of block*/ ch2 ++; // ch2 is incremented by 1 System.out.println("Now value of ch2 is " +ch2); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of ch2 */ }// closing curly brace } is used to close the definition of for loop
  • 29. 29 } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class Output: Discussion: In this program a class of name CharDemo2 is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. In this code ch1 is first given the value X. then it is incremented. Then it displays the incremented value of ch1 i.e the character in ASCII sequence.
  • 30. 30 Program 10: Write a program that demonstrate the use of Boolean values. Solution: class BoolTest { /* this line contains a keyword class that shows that a class is going to start and BoolTest is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main(String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ boolean b; //booleab b is declared . Semicolon is used to terminate the statement/line b = false; /* bolean of varable b is initialized to false. Semicolon is used to terminate the statement/line*/ System.out.println("b is: " + b); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of b */ b = true; //now Boolean is initialized to true. Semicolon is used to terminate the statement/line System.out.println("b is: " + b); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of b*/ if(b)
  • 31. 31 // a boolean value can control the if statement System.out.println("This statement is executed "); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets */ b = false; //now Boolean is initialized to false. Semicolon is used to terminate the statement/line if(b) // a boolean value can control the if statement. System.out.println("This statement is not executed "); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets which will not be displayed because the above if statement is not fulfilled. Semicolon is used to terminate the statement/line */ System.out.println("10 > 9 is " + (10 > 9)); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of 10 > 9 and outcome of this relational operator is a boolean value. Semicolon is used to terminate the statement/line */ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class
  • 32. 32 Output: Discussion: In this program a class of name BoolTest is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. It demonstrate the Boolean values it can control the if statement. Then it displays the outcome of a relational operator i.e a Boolean value Program 11: Write a program that demonstrate the use of dynamically initialized values. Solution: class DynInit { /* this line contains a keyword class that shows that a class is going to start and DynInit is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main(String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e
  • 33. 33 anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ double a = 3.0, b = 4.0; /*initialized two variables of type double. Semicolon is used to terminate the statement/line*/ double c = Math.sqrt(a * a + b * b); /* c is dynamically initialized. Math.sqrt is used to take the square root of value which we get after multiplying a to a and b to b then adding them we get square root. Semicolon is used to terminate the statement/line*/ System.out.println("Hypotenuse is: " + c); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of c*/ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class Output:
  • 34. 34 Discussion: In this program three local variables a, b and c are declared. C is dynamically initialized that is by calling the function of square root i.e Math.Sqrt. at the end it displays the value of hypotenuse that is stored in c. Program 12: Write a program that demonstrate the scope of block Solution: class Scope { /* this line contains a keyword class that shows that a class is going to start and Scope is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main(String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ int x = 10; /* a variable x of type int is initialized to 10. Semicolon is used to terminate the statement/line */ if(x == 10) { /* start new scope followed by the if condition if it is true then next code will be executed. Semicolon is used to terminate the statement/line*/ int y = 20; /*initializes a variable of type int known only to this block. Semicolon is used to terminate the statement/line*/ System.out.println("X and Y: " + x + " and " + y); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will
  • 35. 35 print the string written in brackets and also print out the value of x and y. x and y both known here */ x = y * 2; // x will store the value twice of y. Semicolon is used to terminate the statement/line } // end of block scope y = 100; /* Error y not known here because y is initialized after the block scope as it is declared inside the block scope. Semicolon is used to terminate the statement/line*/ System.out.println("x is " + x); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of x. x is still known here because it is declared outside the block scope*/ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class Output:
  • 36. 36 Discussion: In this program the scope of a variable is described. Firstly a variable x of type int is declared that is known to all code within main. Then it is initialized. Then another scope of if statement is started. Then a variable y is declared that is known only to the block of if. Then their values are displayed. Then the block is closed. After that y is again initialized but it will display error that y is not known here but x is still known here because it is declared within the main not in the block of if. Program 13: Write a program that demonstrate the life time of variable. Solution: class LifeTime { /* this line contains a keyword class that shows that a class is going to start and LifeTime is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main(String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ int x; //a variable x of type int is declared. Semicolon is used to terminate the statement/line for(x = 0; x<3; x++) { /*a for loop is declared here it contains three parts initialization i.e initializes x to 0, condition i.e x is less than 3, iteration i.e increment of x. Firstly loop initialize x to zero and then it will check the condition that either it is true or not, if it is true then the next statement will be compiled, after that control/system will pass to the iteration process then it will check the condition if it is fulfilled then the statement given below will be executed again i.e it will go on increasing the value of x by 1 until the condition is not satisfied i.e x<10. Opening curly brace { will separate the block of code of for loop i.e it means begin of block*/ int y = -1; // y is initialized each time block is entered;
  • 37. 37 System.out.println("Y is : " + y); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of y */ y = 10; // changes the value of y by 10 i.e its life time is changed System.out.println("Y is now: " + y); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of y */ } // closing curly brace } is used to close the definition of for loop } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class Output:
  • 38. 38 Discussion: In this program a class of name LifeTime is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. It demonstrate the lifetime of a variable. Variable x is declared in the main but the 2nd variable y is initialized each time block is entered. In the block of for loop value of y can be changed and displayed not outside it. Program14: Write a code that demonstrate the basic arithmetic operators. Solution: class BasicMath { /* this line contains a keyword class that shows that a class is going to start and BasicMath is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main(String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ int a = 1 + 1; /* addition arithmetic operation using integers. Variable a of type int is declared and initialized. Semicolon is used to terminate the statement/line*/ int b = a * 3; /* multiplication arithmetic operation using integers. Variable b of type int is declared and initialized. Semicolon is used to terminate the statement/line*/ int c = b / 4; /* division arithmetic operation using integers. Variable c of type int is declared and initialized. Semicolon is used to terminate the statement/line*/
  • 39. 39 int d = c - a; /* subtraction arithmetic operation using integers. Variable d of type int is declared and initialized. Semicolon is used to terminate the statement/line*/ int e = -d; /*variable e of type int is initialized and stores the negative value of d. Semicolon is used to terminate the statement/line*/ System.out.println("a = " + a); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of a */ System.out.println("b = " + b); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of b */ System.out.println("c = " + c); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of c */ System.out.println("d = " + d); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of d */ System.out.println("e = " + e); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of e */ double da = 1 + 1; /* addition arithmetic operation using double. Variable da of type double is declared and initialized. Semicolon is used to terminate the statement/line*/ double db = da * 3;
  • 40. 40 /* multiplication arithmetic operation using double. Variable db of type double is declared and initialized. Semicolon is used to terminate the statement/line*/ double dc = db / 4; /* division arithmetic operation using double. Variable dc of type double is declared and initialized. Semicolon is used to terminate the statement/line*/ double dd = dc - a; /* subtraction arithmetic operation using double. Variable dd of type double is declared and initialized. Semicolon is used to terminate the statement/line*/ double de = -dd; /*variable de of type int is initialized and stores the negative value of dd. Semicolon is used to terminate the statement/line*/ System.out.println("da = " + da); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of da */ System.out.println("db = " + db); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of db */ System.out.println("dc = " + dc); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of dc */ System.out.println("dd = " + dd); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of dd */ System.out.println("de = " + de);
  • 41. 41 /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of de */ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class Output: Discussion: This program performs the basic arithmetic operations of multiplication, division, subtraction and addition using variables of type double and int and displays them.
  • 42. 42 Program 15: Write a program that demonstrates the use of modulus operator. Solution: class modulus { /* this line contains a keyword class that shows that a class is going to start and modulus is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main ( String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */ int x = 42; double y = 42.25; /* variables x and y of type int & double are initialized and declared respectively. Semicolon is used to terminate the statement/line*/ System.out.println("x mod 10 = " + x % 10); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of x % 10 */ System.out.println("y mod 10 = " + y % 10); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of y % 10 */ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class
  • 43. 43 Output: Discussion: In this program a class of name modulus is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. It displays the value of x and y’s modulus 10. Program 16: Write a program that demonstrates the use of assignment operator. Solution: class OpEquals { /* this line contains a keyword class that shows that a class is going to start and OpEquals is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main(String args[]) { /*this line of code shows that public is an access specifier that allows a programmer to control its visibility or access, means this class is accessible out of the class in which it is declared i.e anyone publically can access it. Static is a keyword that allows the main() method to be execute without instantiating an instance of that class. Void means this main() method has no return type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly brace { shows that form here the method’s definition is going to begin */
  • 44. 44 int a = 1; /* a variable a of type int in initialized and declared. Semicolon is used to terminate the statement/line*/ int b = 2; /* a variable b of type int in initialized and declared. Semicolon is used to terminate the statement/line*/ int c = 3; /* a variable c of type int in initialized and declared. Semicolon is used to terminate the statement/line*/ int d = 4; /* a variable d of type int in initialized and declared. Semicolon is used to terminate the statement/line*/ int e = 5; /* a variable e of type int in initialized and declared. Semicolon is used to terminate the statement/line*/ int f = 5; /* a variable f of type int in initialized and declared. Semicolon is used to terminate the statement/line*/ a += 5; // increment in a by 5. Semicolon is used to terminate the statement/line b *= 4; // multiply b by 4. Semicolon is used to terminate the statement/line c /= 6; // divide c by 6. Semicolon is used to terminate the statement/line d %= 8; // modulus of d by 8. Semicolon is used to terminate the statement/line f -= 10;
  • 45. 45 // decrement in f by 5. Semicolon is used to terminate the statement/line e += d * c; // increment in e by d * c. Semicolon is used to terminate the statement/line System.out.println(" a = " + a); /* System is a built in function used to give access to the system. Out is used for the output. Println is used to print or display & ln is used to goto the next line. This statement of code will print the string written in brackets and also print out the value of a. Semicolon is used to terminate the statement/line */ System.out.println(" b = " + b); /* This statement of code will print the string written in brackets and also print out the value of b. Semicolon is used to terminate the statement/line */ System.out.println(" c = " + c); /* This statement of code will print the string written in brackets and also print out the value of c. Semicolon is used to terminate the statement/line */ System.out.println(" d = " + d); /* This statement of code will print the string written in brackets and also print out the value of d. Semicolon is used to terminate the statement/line */ System.out.println(" e = " + e); /* This statement of code will print the string written in brackets and also print out the value of e. Semicolon is used to terminate the statement/line */ System.out.println(" f = " + f); /* This statement of code will print the string written in brackets and also print out the value of f. Semicolon is used to terminate the statement/line */ } // closing curly brace } is used to close the definition of main() method } // closing curly brace } is used to close the definition of class
  • 46. 46 Output: Discussion: In this program a class of name OpEquals is declared then its definition starts. Its definition is enclosed between the opening and closing of curly braces {}. Then main method of no return type is declared. This method is static and public. Its definition is enclosed between the opening and closing curly braces {}. It demonstrate several assignment operators i.e += , *=, %= and displays their values Program 17: Write a program that demonstrates the use of increment operator. Solution: class IncDec { /* this line contains a keyword class that shows that a class is going to start and IncDec is the name of this class. Opening curly brace { shows that form here a class’ definition is going to begin */ public static void main (String args[]) {