Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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:...............................................................................................
3
Discussion:................................................................................................................
4
Discussion:................................................................................................................
5
Discussion:................................................................................................................
6
Discussion:................................................................................................................
7
Discussion:................................................................................................................
8
Discussion:................................................................................................................
9
Discussion:................................................................................................................
10
Solution:.................................................................................................................
11
Solution:.................................................................................................................
12
Program 1:
Write a simple program that explains the syntax of java code or
demonstrate how to write it.
Solution:
class...
13
Output:
Discussion:
In this program a class of name Example is declared then its definition starts. Its
definition is e...
14
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that for...
15
Discussion:
In this program a class of name Example2 is declared then its definition starts. Its
definition is enclosed...
16
System.out.println (" 'X' is less than 'Y' ");
/* System is a built in function used to give access to the system. Out ...
17
Output:
Discussion:
In this program a class of name ifsample is declared then its definition starts. Its
definition is ...
18
/* this line of code shows that public is an access specifier that allows a programmer to control
its visibility or acc...
19
Output:
Discussion:
In this program a class of name fortest is declared then its definition starts. Its definition
is e...
20
/*this line of code shows that public is an access specifier that allows a programmer to control
its visibility or acce...
21
} /* closing curly brace } is used to close the definition of for loop.
Closing curly brace { will separate the block o...
22
Program 6:
Write a program that calculates the distance traveled by light in
thousand days using long variables.
Soluti...
23
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to prin...
24
Program 7:
Write a code to calculate area using double variables
Solution:
class Area {
/* this line contains a keyword...
25
Output:
Discussion:
In this program a class of name Area is declared then its definition starts. Its
definition is encl...
26
type. In parenthesis a parameter args[], an array of type String is declared. Then opening curly
brace { shows that for...
27
Discussion:
In this program a class of name CharDemo is declared then its definition starts. Its
definition is enclosed...
28
ch1++;
/*an increment operator is used here to increase the value of ch1. Semicolon is used to terminate
the statement/...
29
} // closing curly brace } is used to close the definition of main() method
} // closing curly brace } is used to close...
30
Program 10:
Write a program that demonstrate the use of Boolean values.
Solution:
class BoolTest {
/* this line contain...
31
// a boolean value can control the if statement
System.out.println("This statement is executed ");
/* System is a built...
32
Output:
Discussion:
In this program a class of name BoolTest is declared then its definition starts. Its
definition is ...
33
anyone publically can access it. Static is a keyword that allows the main() method to be execute
without instantiating ...
34
Discussion:
In this program three local variables a, b and c are declared. C is dynamically initialized
that is by call...
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;
// ...
36
Discussion:
In this program the scope of a variable is described. Firstly a variable x of type int is
declared that is ...
37
System.out.println("Y is : " + y);
/* System is a built in function used to give access to the system. Out is used for ...
38
Discussion:
In this program a class of name LifeTime is declared then its definition starts. Its
definition is enclosed...
39
int d = c - a;
/* subtraction arithmetic operation using integers. Variable d of type int is declared and
initialized. ...
40
/* multiplication arithmetic operation using double. Variable db of type double is declared and
initialized. Semicolon ...
41
/* System is a built in function used to give access to the system. Out is used for the output.
Println is used to prin...
42
Program 15:
Write a program that demonstrates the use of modulus operator.
Solution:
class modulus {
/* this line conta...
43
Output:
Discussion:
In this program a class of name modulus is declared then its definition starts. Its
definition is e...
44
int a = 1;
/* a variable a of type int in initialized and declared. Semicolon is used to terminate the
statement/line*/...
45
// decrement in f by 5. Semicolon is used to terminate the statement/line
e += d * c;
// increment in e by d * c. Semic...
46
Output:
Discussion:
In this program a class of name OpEquals is declared then its definition starts.
Its definition is ...
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Manual of JAVA (more than Half)
Upcoming SlideShare
Loading in …5
×

0

Share

Download to read offline

Manual of JAVA (more than Half)

Download to read offline

Manual of java more than half contains at least 77 programs.. remaining is also uploaded.. Hope u guys love it because it is the best version

Related Books

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

Manual of JAVA (more than Half)

  1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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[]) {

Manual of java more than half contains at least 77 programs.. remaining is also uploaded.. Hope u guys love it because it is the best version

Views

Total views

377

On Slideshare

0

From embeds

0

Number of embeds

5

Actions

Downloads

17

Shares

0

Comments

0

Likes

0

×