EXAMPLE-1
/*
Demonstrate a block
of code.
Call this file
"Test.java"
*/
class Test {
public static void
main(String args[]) {
int x, y;
y = 20;
// the target of this loop is a block
for(x = 0;
x<10; x++) {
System.out.println("This is x: " + x);
System.out.println("This is y: " + y);
y = y - 2;
}
}
}
OUTPUT:
C:\SATYA>javac test.java
C:\satya>java test
This is x=0
This is y=20
This is x=1
This is y=18
This is x=2
This is y=16
This is x=3
This is y=14
This is x=4
This is y=12
This is x=5
This is y=10
This is x=6
This is y=8
This is x=7
This is y=6
This is x=8
This is y=4
This is x=9
This is y=2
C:\satya>
EXAMPLE-2
/*
This is a simple Java program.
Call this file
"demo1.java".
*/
class Demo1 {
// Your program
begins with a call to main().
public static void
main(String args[]) {
System.out.println("This is a simple Java program.");
}
}
Output:
C:\satya>javac Demo1.java
C:\satya>java Demo1
This is a simple java program
C:\satya>
EXAMPLE-3
/*
Here is another
short example.
Call this file
"demo2.java".
*/class Demo2 {
public static void main(String args[]) {
int num; // this
declares a variable called num
num = 10; // this
assigns num the value 10
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");
System.out.println(num);
}
}
Output:
C:\satya>javac Demo2.java
C:\satya>java Demo2
This is num:10
The value of num * 2 is 20
C:\satya>
EXAMPLE-4
/*
Demonstrate the for
loop.
Call this file
"Demo3.java".
*/
class Demo3 {
public static void
main(String args[]) {
int x;
for(x = 0;
x<10; x = x+1)
System.out.println("This
is x: " + x);
}
}
Output:
C:\satya>javac Demo3.java
C:\satya>java Demo3
This is x=0
This is x=1
This is x=2
This is x=3
This is x=4
This is x=5
This is x=6
This is x=7
This is x=8
This is x=9
C:\satya>
EXAMPLE-5
/*
Demonstrate the if.
Call this file
"Sample.java".
*/
class Sample {
public static void
main(String args[]) {
int x, y;
x = 10;
y = 20;
if(x < y)
System.out.println("x is less than y");
x = x * 2;
if(x == y)
System.out.println("x now equal to y");
x = x * 2;
if(x > y)
System.out.println("x now greater than y");
// this won't
display anything
if(x == y)
System.out.println("you won't see this");
}
}
Output:
C:\satya>javac Sample.java
C:\satya>java Sample
X is less than y
X now equal to y
X now greater than y
C:\satya>
EXAMPLE-7
// Demonstrate a one-dimensional array.
class Array {
public static void
main(String args[]) {
int month_days[];
month_days = new
int[12];
month_days[0] =
31;
month_days[1] =
28;
month_days[2] =
31;
month_days[3] =
30;
month_days[4] =
31;
month_days[5] =
30;
month_days[6] =
31;
month_days[7] =
31;
month_days[8] =
30;
month_days[9] =
31;
month_days[10] =
30;
month_days[11] =
31;
System.out.println("April
has " + month_days[3] + " days.");
}
}
OUTPUT:
C:\SATYA>javac Array.java
C:\satya>java Array
April has 30 days
C:\satya>
EXAMPLE-8
// An improvied version of the previous program.
class Test1 {
public static void
main(String args[]) {
int month_days[] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
System.out.println("April has " + month_days[3] + "
days.");
}
}
OUTPUT:
C:\SATYA>javac Test1.java
C:\satya>java Test1
April has 30 days
C:\satya>
EXAMPLE-9
// Average an array of values.
class Average {
public static void
main(String args[]) {
double nums[] =
{10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for(i=0; i<5;
i++)
result = result
+ nums[i];
System.out.println("Average is " + result / 5);
}
}
OUTPUT:
C:\SATYA>javac Average.java
C:\satya>java Average
Average is 12.299999999999999
C:\satya>
EXAMPLE-10
// Demonstrate boolean values.
class Simple {
public static void
main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.pr
intln("b is " + b);
// a boolean value
can control the if statement
if(b)
System.out.println("This is executed.");
b = false;
if(b)
System.out.println("This is not executed.");
// outcome of a
relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
OUTPUT:
C:\SATYA>javac Simple.java
C:\satya>java Simple
b is false
b is false
This is expected
10>9 is true
C:\satya>
EXAMPLE-11
// Demonstrate char data type.
class simple2 {
public static void
main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
OUTPUT:
C:\SATYA>javac simple2.java
C:\satya>java simple2
Ch1 and ch2:x y
C:\satya>
EXAMPLE-12
// char variables behave like integers.
Class charDemo {
public static void
main(String args[]) {
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++; //
increment ch1
System.out.println("ch1 is now " + ch1);
}
}
OUTPUT:
C:\SATYA>javac charDemo.java
C:\satya>java charDemo
Ch1 contains x
Ch1 is now y
C:\satya>
EXAMPLE-13
// Demonstrate casts.
class Conver {
public static void
main(String args[]) {
byte b;
int i = 257;
double d =
323.142;
System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}
OUTPUT:
C:\SATYA>javac Conver.java
C:\satya>java Conver
Conversion of int to byte
i and b 257 1
Conversion of double to int
d and i 323.142 323
Conversion of double to byte
d and b 323.142 67
c:\satya>
EXAMPLE-14
// Demonstrate dynamic initialization.
class Dynamic {
public static void
main(String args[]) {
double a = 3.0,
b = 4.0;
// c is
dynamically initialized
double c =
Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}
OUTPUT:
C:\SATYA>javac Dynamic.java
C:\satya>java Dynamic
Hypotenuse is 5.0
C:\satya>
EXAMPLE-15
// Demonstrate lifetime of a variable.
class LifeTime {
public static void
main(String args[]) {
int x;
for(x = 0; x <
3; x++) {
int y = -1; // y
is initialized each time block is entered
System.out.println("y iz: " + y); // this always prints -1
y = 100;
System.out.println("y is now: " + y);
}
}
}
OUTPUT:
C:\SATYA>javac LifeTime.java
C:\satya>java LifeTime
Y is:-1
Y is now:100
Y is:-1
Y is now:100
Y is:-1
Y is now:100
C:\satya>
EXAMPLE-16
// Compute distance light travels using long variables.
class Light Demo
{
public static void
main(String args[]) {
int lightspeed;
long days;
long seconds;
long distance;
// approximate
speed of light in miles per second
lightspeed =
186000;
days = 1000; //
specify number of days here
seconds = days *
24 * 60 * 60; // convert to seconds
distance =
lightspeed * seconds; // compute distance
System.out.print("In
" + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}
}
OUTPUT:
C:\SATYA>javac LightDemo.java
C:\satya>java LightDemo
In 1000 days light will travel about 16070400000000 miles
C:\satya>
EXAMPLE-17
// Initialize a two-dimensional array.
class Matrix {
public static void
main(String args[]) {
double m[][] = {
{ 0*0, 1*0, 2*0,
3*0 },
{ 0*1, 1*1, 2*1,
3*1 },
{ 0*2, 1*2, 2*2,
3*2 },
{ 0*3, 1*3, 2*3,
3*3 }
};
int i, j;
for(i=0; i<4;
i++) {
for(j=0; j<4;
j++)
System.out.print(m[i][j] + " ");
System.out.println();
}
}
}
OUTPUT:
C:\SATYA>javac Matrix.java
C:\satya>java matrix
0.0 0.0 0.0 0.0
0.0 1.0 2.0 3.0
0.0 2.0 4.0 6.0
0.0 3.0 6.0 9.0
C:\satya>
EXAMPLE-18
class Promote {
public static void
main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f
* b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - "
+ (d * s));
System.out.println("result = " + result);
}
}
OUTPUT:
C:\SATYA>javac Promote.java
C:\satya>java Promote
238.14+515-126.3616
Result=626.7784146484375
C:\satya>
EXAMPLE-19
// Demonstrate block scope.
class Scope {
public static void
main(String args[]) {
int x; // known to
all code within main
x = 10;
if(x == 10) { //
start new scope
int y = 20; //
known only to this block
// x and y both
known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; //
Error! y not known here
// x is still
known here.
System.out.println("x is " + x);
}
}
OUTPUT:
C:\SATYA>javac Scope.java
C:\satya>java Scope
X and y:10 20
X is 40
EXAMPLE-20
// This program will not compile
class ScopeErr {
public static void
main(String args[]) {
int bar = 1;
class threeDMatrix {
{ // creates a new scope
int bar = 2; //
Compile time error -- bar already defined!
}
}
}
OUTPUT:
C:\SATYA>javac ScopeErr.java
C:\satya>java ScopeErr
Compile run time—bar already defined
C:\satya>
EXAMPLE-21
// Demonstrate a three-dimensional array.
Class threeDarray
public static void
main(String args[]) {
int threeD[][][] =
new int[3][4][5];
int i, j, k;
for(i=0; i<3;
i++)
for(j=0; j<4;
j++)
for(k=0;
k<5; k++)
threeD[i][j][k] = i * j * k;
for(i=0; i<3;
i++) {
for(j=0; j<4;
j++) {
for(k=0;
k<5; k++)
System.out.print(threeD[i][j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}
OUTPUT:
C:\SATYA>javac threeDarrray.java
C:\satya>java threeDarray
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 0 0 0 0
0 2 4 6 8
0 4 8 12 16
0 6 12 18 24
C:\satya>
EXAMPLE-22
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void
main(String args[]) {
int twoD[][] = new
int[4][];
twoD[0] = new
int[1];
twoD[1] = new
int[2];
twoD[2] = new
int[3];
twoD[3] = new
int[4];
int i, j, k = 0;
for(i=0; i<4;
i++)
for(j=0;
j<i+1; j++) {
twoD[i][j] =
k;
k++;
}
for(i=0; i<4;
i++) {
for(j=0;
j<i+1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
OUTPUT:
C:\SATYA>javac TwoDAgain.java
C:\satya>java TwoDAgain
0
1 2
3 4 5
6 7 8 9
C:\satya>
EXAMPLE-23
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void
main(String args[]) {
int twoD[][]= new
int[4][5];
int i, j, k = 0;
for(i=0; i<4;
i++)
for(j=0; j<5;
j++) {
twoD[i][j] =
k;
k++;
}
for(i=0; i<4;
i++) {
for(j=0; j<5;
j++)
System.out.print(twoD[i][j] + "
");
System.out.println();
}
}
}
OUTPUT:
C:\SATYA>javac TwoDarrray.java
C:\satya>java TwoDarray
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
C:\satya>
EXAMPLE-24
// Demonstrate the basic arithmetic operators.
class Basic {
public static void
main(String args[]) {
// arithmetic
using integers
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a
= " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
// arithmetic
using doubles
System.out.println("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da *
3;
double dc = db /
4;
double dd = dc -
a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}
OUTPUT:
C:\SATYA>javac Basic.java
C:\satya>java Basic
Integer Arthimetic
a=2
b=6
c=1
d=-1
e=1
Floating point Arithmetic
da=2.0
db=6.0
dc=1.5
dd=-0.5
de=0.5
c:\satya>
EXAMPLE-25
// Demonstrate the bitwise logical operators.
class BitLogic {
public static void
main(String args[]) {
String binary[] =
{
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"
};
int a = 3; // 0 +
2 + 1 or 0011 in binary
int b = 6; // 4 +
2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a &
b) | (a & ~b);
int g = ~a &
0x0f;
System.out.println(" a
= " + binary[a]);
System.out.println(" b = " + binary[b]);
System.out.println(" a|b
= " + binary[c]);
System.out.println("
a&b = " + binary[d]);
System.out.println(" a^b
= " + binary[e]);
System.out.println("~a&b|a&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
}
}
OUTPUT:
C:\SATYA>javac BitLogic.java
C:\satya>java BitLogic
a=0011
b=0110
a|b=0111
a&b=0010
a^b=0101
~a&b|a&~b=0101
~a=1100
C:\satya>
EXAMPLE-26
// Demonstrate the boolean logical operators.
Class vera
{
public static void
main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a
& b;
boolean e = a ^ b;
boolean f = (!a
& b) | (a & !b);
boolean g = !a;
System.out.println(" a
= " + a);
System.out.println(" b
= " + b);
System.out.println(" a|b
= " + c);
System.out.println("
a&b = " + d);
System.out.println(" a^b
= " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println(" !a = " + g);
}
}
OUTPUT:
C:\SATYA>javac vera.java
C:\satya>java vera
a=true
b=false
a|b=true
a&b=false
a^b=true
!a&b|a&!b=true
!a=false
C:\satya>
EXAMPLE-27
// Left shifting a byte value.
class Shift {
public static void
main(String args[]) {
byte a = 64, b;
int i;
i = a << 2;
b = (byte) (a
<< 2);
System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);
}
}
OUTPUT:
C:\SATYA>javac Shift.java
C:\satya>java Shift
Original value of a:64
i and b:256 0
c:\satya>
EXAMPLE-28
// Unsigned shifting a byte value.
class ByteUShift {
static public void
main(String args[]) {
char hex[] = {
'0', '1', '2',
'3', '4', '5', '6', '7',
'8', '9', 'a',
'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
byte c = (byte) (b
>> 4);
byte d = (byte) (b
>>> 4);
byte e = (byte)
((b & 0xff) >> 4);
System.out.println("
b = 0x"
+ hex[(b
>> 4) & 0x0f] + hex[b & 0x0f]);
System.out.println("
b >> 4 = 0x"
+ hex[(c
>> 4) & 0x0f] + hex[c & 0x0f]);
System.out.println(" b
>>> 4 = 0x"
+ hex[(d
>> 4) & 0x0f] + hex[d & 0x0f]);
System.out.println("(b & 0xff) >> 4 = 0x"
+ hex[(e
>> 4) & 0x0f] + hex[e & 0x0f]);
}
}
OUTPUT:
C:\SATYA>javac ByteUShift.java
C:\satya>java ByteUshift
b=0*f1
b>>4=0*ff
b>>>4=0*ff
<b
& 0*ff> >>4=0*0f
C:\satya>
EXAMPLE-29
// Masking sign extension.
class HexByte {
static public void
main(String args[]) {
char hex[] = {
'0', '1', '2',
'3', '4', '5', '6', '7',
'8', '9', 'a',
'b', 'c', 'd', 'e', 'f'
};
byte b = (byte)
0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] +
hex[b & 0x0f]);
}}
OUTPUT:
C:\SATYA>javac HexByte.java
C:\satya>java HexByte
b=0*f1
c:\satya>
EXAMPLE-30
// Demonstrate ++ and --.
class IncDec {
public static void
main(String args[]) {
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
OUTPUT:
C:\SATYA>javac IncDec.java
C:\satya>java IncDec
a=2
b=3
c=4
d=1
c:\satya>
EXAMPLE-31
// Demonstrate the % operator.
class Modulus {
public static void
main(String args[]) {
int x = 42;
double y = 42.3;
System.out.println("x mod 10 = " + x % 10);
System.out.println("y mod 10 = " + y % 10);
}
}
OUTPUT
C:\Sai>javac Modulus.java
C:\Sai>java Modulus
X mod 10 = 2
Y mod 10 = 2.99999999999997
C:\Sai>
EXAMPLE-32
// Left shifting as a quick way to multiply by 2.
class Multi{
public static void
main(String args[]) {
int i;
int num =
0xFFFFFFE;
for(i=0; i<4;
i++) {
num = num
<< 1;
System.out.println(num);
} } }
OUTPUT
C:\Sai>javac Multi.java
C:\Sai>java Multi
5367870908
1073741816
2147483632
-32
C:\Sai>
EXAMPLE-33
class Equals {
public static void
main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}}
OUTPUT
C:\Sai>javac Equals.java
C:\Sai>java Equals
a = 3
b = 1
c = 6
C:\Sai>
EXAMPLE-34
// Demonstrate several assignment operators.
class op{
public static void
main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a += 5;
b *= 4;
c += a * b;
c %= 6;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
OUTPUT
C:\Sai>javac op.java
C:\Sai>java op
a = 6
b = 8
c = 3
C:\Sai>
EXAMPLE-35
// Demonstrate ?.
class Ternary {
public static void
main(String args[]) {
int i, k;
i = 10;
k = i < 0 ?
-i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
i = -10;
k = i < 0 ?
-i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
}
}
OUTPUT
C:\Sai>javac Ternary.java
C:\Sai>java Ternary
Absolute value of 10 is 10
Absolute value of -10 is -10
C:\Sai>
EXAMPLE-36
// Using break as a civilized form of goto.
Class br{
public static void
main(String args[]) {
boolean t =
true;
first: {
second: {
third: {
System.out.println("Before the break.");
if(t)
break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}
OUTPUT
C:\Sai>javac br.java
C:\Sai>java br
Before the break
This is after second block.
C:\Sai
EXAMPLE-37
// This program contains an error.
class BreakErr {
public static void
main(String args[]) {
one: for(int
i=0; i<3; i++) {
System.out.print("Pass " + i + ": ");
}
for(int j=0;
j<100; j++) {
if(j == 10)
//break one; // WRONG
System.out.print(j + " ");
}
}
}
OUTPUT
C:\Sai>javac BreakErr.java
C:\Sai>java BreakErr
Pass 0: Pass 1:
Pass 2: 10
C:\Sai>
EXAMPLE-38
// Using break to exit a loop.
class BreakLoop {
public static void
main(String args[]) {
for(int i=0;
i<100; i++) {
if(i == 10)
break; // terminate loop if i is 10
System.out.println("i: " + i);
}
System.out.println("Loop
complete.");
}
}
OUTPUT
C:\Sai>javac BreakLoop.java
C:\Sai>java BreakLoop
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
Loop complete.
C:\Sai>
EXAMPLE-39
// Using break to exit a while loop.
class BreakLoop2 {
public static void
main(String args[]) {
int i = 0;
while(i <
100) {
if(i == 10)
break; // terminate loop if i is 10
System.out.println("i:
" + i);
i++;
}
System.out.println("Loop
complete.");
}
}
OUTPUT
C:\Sai>javac BreakLoop2.java
C:\Sai>java BreakLoop2
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
Loop complete.
C:\Sai>
EXAMPLE-40
// Using break to exit from nested loops
class Loop4 {
public static void
main(String args[]) {
outer: for(int
i=0; i<3; i++) {
System.out.print("Pass " + i + ": ");
for(int j=0;
j<100; j++) {
if(j == 10)
break outer; // exit both loops
System.out.print(j + " ");
}
System.out.println("This will not print");
}
System.out.println("Loops complete.");
}
}
OUTPUT
C:\SATYA>javac Loop4.java
C:\SATYA>java Loop4
Pass 0: 0 1 2 3 4 5 6 7 8 9 Loops complete
C:\SATYA>
EXAMPLE-41
// Using the comma.
class pro{
public static void
main(String args[]) {
int a, b;
for(a=1, b=4;
a<b; a++, b--) {
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
}
OUTPUT
C:\SATYA>javac pro.java
C:\SATYA>java pro
a = 1
b = 4
a = 2
b = 3
C:\SATYA>
EXAMPLE-43
// Demonstrate continue.
class Continue {
public static void
main(String args[]) {
for(int i=0;
i<10; i++) {
System.out.print(i + " ");
if (i%2 == 0)
continue;
System.out.println("");
}
}
}
OUTPUT
C:\SATYA>javac Continue.java
C:\SATYA>java Continue
0 1
2 3
4 5
6 7
8 9
C:\SATYA>
EXAMPLE-44
// Using continue with a label.
class ContinueLabel {
public static void
main(String args[]) {
outer: for (int i=0; i<10; i++) {
for(int
j=0; j<10; j++) {
if(j >
i) {
System.out.println();
continue outer;
}
System.out.print(" " + (i * j));
}
}
System.out.println();
}
}
OUTPUT
C:\SATYA>javac ContinueLabel.java
C:\SATYA>java ContinueLabel
0
0 1
0 2 4
0 3 6
9
0 4 8
12 16
0 5 10
15 20 25
0 6 12
18 24 30 36
0 7 14
21 28 35 42 49
0 8 16
24 32 40 48 56
64
0 9 18 27 36
45 54 63
72 81
C:\SATYA>
EXAMPLE-45
// Demonstrate the do-while loop.
class DoWhile {
public static void
main(String args[]) {
int n = 10;
do {
System.out.println("tick " + n);
n--;
} while(n >
0);
}
}
OUTPUT
C:\SATYA>javac DoWhile.java
C:\SATYA>java
DoWhile
Tick 10
Tick 9
Tick 8
Tick 7
Tick 6
Tick 5
Tick 4
Tick 3
Tick 2
Tick 1
C:\SATYA>
EXAMPLE-46
// Test for primes.
class Prime {
public static void
main(String args[]) {
int num;
boolean isPrime
= true;
num = 14;
for(int i=2; i
< num/2; i++) {
if((num % i)
== 0) {
isPrime =
false;
break;
}
}
if(isPrime)
System.out.println("Prime");
else
System.out.println("Not Prime");
}
}
OUTPUT
C:\SATYA>javac Prime.java
C:\SATYA>java
Prime
Not Prime
C:\SATYA>
EXAMPLE-48
// Declare a loop control variable inside the for.
class ForTick1 {
public static void
main(String args[]) {
// here, n is
declared inside of the for loop
for(int n=10;
n>0; n--)
System.out.println("tick " + n);
}
}
OUTPUT
C:\SATYA>javac ForTick1.java
C:\SATYA>java
ForTick1
Tick 10
Tick 9
Tick 8
Tick 7
Tick 6
Tick 5
Tick 4
Tick 3
Tick 2
Tick 1
C:\SATYA>
EXAMPLE-49
// Parts of the for loop can be empty.
class ForVar {
public static void
main(String args[]) {
int i;
boolean done =
false;
i = 0;
for( ; !done; )
{
System.out.println("i is " + i);
if(i == 10)
done = true;
i++;
}
}
}
OUTPUT
C:\SATYA>javac ForVar.java
C:\SATYA>java
ForVar
i is 0
i is 1
i is 2
i is 3
i is 4
i is 5
i is 6
i is 7
i is 8
i is 9
i is 10
C:\SATYA>
EXAMPLE-50
// Demonstrate if-else-if statements.
class pro2{
public static void
main(String args[]) {
int month = 4;
// April
String season;
if(month == 12
|| month == 1 || month == 2)
season =
"Winter";
else if(month ==
3 || month == 4 || month == 5)
season =
"Spring";
else if(month ==
6 || month == 7 || month == 8)
season =
"Summer";
else if(month ==
9 || month == 10 || month == 11)
season =
"Autumn";
else
season =
"Bogus Month";
System.out.println("April is in the " + season +
".");
}
}
OUTPUT
C:\SATYA>javac pro2.java
C:\SATYA>java
pro2
April is in the Spring.
C:\SATYA>
EXAMPLE-51
// Using a do-while to process a menu selection -- a
simple help system.
class Menu {
public static void
main(String args[])
throws
java.io.IOException {
char choice;
do {
System.out.println("Help on:");
System.out.println(" 1. if");
System.out.println(" 2.
switch");
System.out.println(" 3.
while");
System.out.println(" 4.
do-while");
System.out.println(" 5.
for\n");
System.out.println("Choose one:");
choice =
(char) System.in.read();
} while( choice
< '1' || choice > '5');
System.out.println("\n");
switch(choice) {
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\n");
System.out.println("switch(expression) {");
System.out.println(" case
constant:");
System.out.println("
statement sequence");
System.out.println(" break;");
System.out.println(" //
...");
System.out.println("}");
break;
case '3':
System.out.println("The while:\n");
System.out.println("while(condition) statement;");
break;
case '4':
System.out.println("The do-while:\n");
System.out.println("do {");
System.out.println("
statement;");
System.out.println("} while (condition);");
break;
case '5':
System.out.println("The for:\n");
System.out.print("for(init; condition; iteration)");
System.out.println(" statement;");
break;
}
}
}
OUTPUT
C:\SATYA>javac Menu.java
C:\SATYA>java
Menu
Help on:
1. if
2. switch
3. while
4. do-while
5. for
Choose one: 1
if<condition> statement;
else statement;
C:\SATYA>
EXAMPLE-52
// In a switch, break statements are optional.
Class pro1{
public static void
main(String args[]) {
for(int i=0;
i<12; i++)
switch(i) {
case 0:
case 1:
case 2:
case 3:
case 4:
System.out.println("i is less than 5");
break;
case 5:
case 6:
case 7:
case 8:
case 9:
System.out.println("i is less than 10");
break;
default:
System.out.println("i is 10 or more.");
}
}
}
OUTPUT
C:\SATYA>javac pro1.java
C:\SATYA>java
pro1
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is 10 or more
i is 10 or more
C:\SATYA>
EXAMPLE-53
// Loops may be nested.
class Nested {
public static void
main(String args[]) {
int i, k;
for(i=0;
i<10; i++) {
for(k=i;k<10;k++)
System.out.print(".");
System.out.println();
}
}
}
OUTPUT
C:\SATYA>javac Nested.java
C:\SATYA>java
Nested
. . . . . . . . . . .
. . . . . . . . . .
. . . . . . . . .
. . . . . . . .
. . . . . . .
. . . . . .
. . . . .
. . . .
. . .
. .
.
C:\SATYA>
EXAMPLE-54
// The target of a loop can be empty.
class target{
public static void
main(String args[]) {
int i, j;
i = 100;
j = 200;
// find midpoint
between i and j
while(++i <
--j) ; // no body in this loop
System.out.println("Midpoint is " + i);
}
}
OUTPUT
C:\SATYA>javac target.java
C:\SATYA>java
target
Midpoint is 150
C:\SATYA>
EXAMPLE-55
// Demonstrate return.
class Return {
public static void
main(String args[]) {
boolean k= true;
System.out.println("Before the return.");
if(k) return //
return to caller
System.out.println("This won't execute.");
}
}
OUTPUT
Error
EXAMPLE-56
class Sample3{
public static void
main(String args[]) {
int a, b;
b = 4;
for(a=1; a<b; a++) {
System.out.println("a = " + a);
System.out.println("b = " + b);
b--;
}
}
}
OUTPUT
C:\SATYA>javac Sample3.java
C:\SATYA>java
Sample
a = 1
b = 4
a = 2
b = 3
C:\SATYA>
EXAMPLE-57
// A simple example of the switch.
Class Switch3{
public static void
main(String args[]) {
for(int i=0;
i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}
OUTPUT
C:\SATYA>javac Switch3.java
C:\SATYA>java
Switch3
i is zero
i is one
i is two
i is three
i is greater than 3
i is greater than 3
C:\SATYA>
EXAMPLE-58
// An improved version of the season program.
class Session{
public static void
main(String args[]) {
int month = 4;
String season;
switch (month)
{
case 12:
case 1:
case 2:
season =
"Winter";
break;
case 3:
case 4:
case 5:
season =
"Spring";
break;
case 6:
case 7:
case 8:
season =
"Summer";
break;
case 9:
case 10:
case 11:
season =
"Autumn";
break;
default:
season =
"Bogus Month";
}
System.out.println("April is in the " + season +
".");
}
}
OUTPUT:
C:\SATYA>javac Session.java
C:\satya>java Session
April is in the spring
C:\satya>
EXSAMPLE-59
// Demonstrate the while loop.
class Loop
public static void
main(String args[]) {
int n = 10;
while(n > 0) {
System.out.println("tick " + n);
n--;
}
}
}
OUTPUT:
C:\SATYA>javac Loop.java
C:\satya>java Loop
Tick 10
Tick 9
Tick 8
Tick 7
Tick 6
Tick 5
Tick 4
Tick 3
Tick 2
Tick 1
C:\satya>
No comments:
Post a Comment