Friday, September 25, 2009

Difference between Struts html tags and normal html tags


What is the difference Normal HTML tags and Struts HTML tags in the way of performance?

Apache struts provided the struts html tags as the replacement of normal html tags for the Jsps. These Struts html tags are directly bind to the form bean using ActionForm where as normal html tags cannot bind with form beans. HTML tags are static but Struts tags are Dynamic (At the run-time struts tags are called)


But coming to performance half of the code in struts tags is pre-compiled one. If we use normal html tags web container has to load the jsp from the scratch. Where as struts tags loads with the pre-compiled code. So that struts tags improves the performance.




Thursday, September 24, 2009

Inner classes in Java

There are four types inner classes in Java. Those are
1. Regular Inner classes
2. Method-local inner classes
3. Anonymous inner classes
4. Static nested classes.

Javascript Dynamic row Addition

Please copy this code and create as test.html and see the functionality
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function addRow(div,value,x){

var ni = document.getElementById(div);
var numi = document.getElementById(value);
var num = (document.getElementById(value).value -1)+ 2;
numi.value = num;

var divIdName = "my"+num+x+"Div";
var newdiv = document.createElement(div);
newdiv.setAttribute("id",divIdName);
newdiv.innerHTML = "<table width=\'100%\' class=\'listTable\' >"
+"<tr class=\'listTR"+(num%2)+"\' onMouseOver=this.style.backgroundColor=\'DEDEBE\' onMouseOut=this.style.backgroundColor=\'\'>"
+"<td><input type='text' name='studNo'/></td>"
+"<td><input type='text' name='studName'/></td>"
+"<td><a href=\"javascript:;\" class=\"menu\" onclick=\"removeEvent(\'"+divIdName+"\',\'"+div+"\')\">Remove</a></td>"
+"</tr></table>";
ni.appendChild(newdiv);
}
function removeEvent(divNum,div){
var d = document.getElementById(div);
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
</script>
</head>
<body>
<form action="">
<table>
<tr bgcolor="#737328"><td>
<table>
<tr><td>Student No</td><td>Student Name</td><td>Actions</td></tr>
<tr><td><input type="text" name="studNo"/></td><td><input type="text" name="studName"/></td><td> </td></tr>
</table>
</td></tr>
<tr bgcolor="#737328"><td>
<input type="hidden" name="theValue" id="theValue" value="0"/>
<div id="myDiv"></div>
</td></tr>
<tr>
<td><p><a href="javascript:;" onclick="addRow('myDiv','theValue','');"><strong>Add More</strong></a></p></td>
</tr>
</table>
</form>
</body>
</html>

Adding Dynamic rows to table using javascript

Please copy this code and create as test.html and see the functionality

<html>
<head>
<script LANGUAGE="JavaScript">
function addRow(tableID){
var table = document.getElementById(tableID);

var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "text";
element1.name = "empNo";
cell1.appendChild(element1);

var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
element2.type = "text";
cell2.appendChild(element2);

var cell3 = row.insertCell(2);
var element3 = document.createElement("textarea");
element3.setAttribute("name","mytextarea");
element3.setAttribute("cols","10");
element3.setAttribute("rows","1");
cell3.appendChild(element3);
}
function deleteRow(tableID) {

var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount > 1) {
table.deleteRow(rowCount - 1);
}

}
</script>
</head>
<body>
<form name="f1" id="f1">
<input type="button"value="Add" onclick="addRow('datatable')">
<table id="datatable" cellspacing="0" border="1" bgcolor="#738711">
<tbody>
<tr>
<td><input type="text" ></td><td><input type="text"></td><td><textarea rows="1" cols="10"></textarea></td>

<td><a href="javascript:void(0);" onclick="deleteRow('datatable')" >Remove</a></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

Wednesday, September 23, 2009

Java Interview Questions

Please post answers for fallowing Questions:

1. What is serial version id which generated in Eclipse.
2. What is the difference between implementing an interface and extending a class
3. What is servlet lifecycle?
4. can static variables are bind with objects?
5. what is the difference between Servlet and Servlet filter?
6. What is the difference between filter and listener?
7. What are features swing has over the awt?
8. what are the features of hibernate?. What is hibernate?
9. What are adapting classes?
10. What are the differences between string and StringBuffer?
11. What is mutable?
12. What are the differences between Arraylist and Vector?
13. How can you deploy an application in tomcat and JBoss?
14. How many layers do you used to develop the web application?

Wednesday, September 16, 2009

Hibernate Questions

Please post the Answers for the fallowing Questions:

1Q: How the web server or applications server knows that you are using Hibernate?
2Q: What are the parameters has to use to create a database resource in the hibernate.cfg.xml?
3Q: What is hivernate mapping file and what is the use of it?
4Q: What is ORM mapping software and how hibernate differs from other ORM mapping softwares?
5Q: What are the features of hibernate?
6Q: What is sessionFactory and when the sessionFactory is created?
7Q: What is the use of dialect in the hibernate mapping file?
8Q: What is HQL ?
9Q: What are the ways you have to get data from database in hiberanate?
10Q: What is the difference between Hibernate session and HTTP session.

Monday, September 14, 2009

Example to create thread by implementing runnable interface

class Thread1{
static public void main(String[] args){
Thread myThreadA = new Thread(new MyThread1(),"threadA");
Thread myThreadB = new Thread(new MyThread1(),"threadB");
//run the two threads
myThreadA.start();
myThreadB.start();

try{
Thread.currentThread().sleep(1000);
}catch(InterruptedException e){}
//Display info about the main thread
System.out.println(Thread.currentThread());
}
}
class MyThread1 implements Runnable{
public void run(){
for(int i= 0;i <10;>
System.out.println(Thread.currentThread());
}
}

Write into file Example

import java.io.*;

class Bistream{
public static void main (String args[]){
try{
BufferedInputStream in =new BufferedInputStream(new FileInputStream("test.txt"));
int i,j=1;
while((i = in.read ())!= -1){
System.out.print((char)i);
if (j==10)
in.skip(10);
if (j==20)
in.mark(199); //mark at 20th byte so that we can come back
if(j==30)
in.reset();
j++;
}
in.close();
} catch (Exception e){};
}
}

Wednesday, September 9, 2009

Java Collection Frame work

Java collection pages are more useful in the development of the applications. Collection is an object which stores the objects such as ArrayList, Vector...etc

The basic interface in collection frame work is Collection. Other interfaces are Set, List, SortedList.
List:
1. When sequence matters
2. When you know index position
3. Duplicates are allowed.

Set:
1. When uniqueness matters
2. You can never have more then one element referencing the same object
3. Does not allow Duplicates.

Map:
1. Key value pairs
2. Maps know the value associated with the given key.
3. You can have two keys that refer the same value but you cannot have a duplicate key(A key can be any object).

The basic advantages of Collection frame work are:
By providing data structures and Algorithms internally the reduces the Programming effort.

Saturday, April 11, 2009

JAVA PROGRAMMS1

class Bistream{
Void static main (String args[]){
Try{
BufferedInputStream in = new new BufferedInputStream (new FileInputStream("test.txt))); //create an object of fileinputstream type
//and construct BufferedInputStream from it.
Int i;
while((i = in.read ())!= -1)
System.out.Print(i);
in.close();
} catch (exception e){};
}
}


--------------------------------------------------


/* Program to create thread by implementing runnable interface
Author : Team -J
Version : 1.0 */
class Thread1{
static public void main(String[] args){
Thread myThreadA = new Thread(new MyThread1(),"threadA");
Thread myThreadB = new Thread(new MyThread1(),"threadB");
//run the two threads
myThreadA.start();
myThreadB.start();

try{
Thread.currentThread().sleep(1000);
}catch(InterruptedException e){}
//Display info about the main thread
System.out.println(Thread.currentThread());
}
}
class MyThread1 implements Runnable{
public void run(){
for(int i= 0;i <10; i++)
System.out.println(Thread.currentThread());
}
}


-------------------------------------


/* Program to create thread by implementing runnable interface
Author : Team -J
Version : 1.0 */
class Thread2{
static public void main(String[] args){
Thread myThreadA = new Thread(new MyThread2(),"threadA");
Thread myThreadB = new Thread(new MyThread2(),"threadB");
//run the two threads
myThreadA.start();
myThreadB.start();

try{
Thread.currentThread().sleep(1000);
}catch(InterruptedException e){}
//Display info about the main thread
System.out.println(Thread.currentThread());
}
}
class MyThread2 extends Thread{
public void run(){
for(int i= 0;i <10; i++)
System.out.println(Thread.currentThread());
}
}


-------------------------------------------------


/* Program to demonstrate producer/consumer problem
Author : Team -J
Version : 1.0 */
class sharedvar{
int i;
sharedvar(){
i=0;
}
int incvar(){
return i++;
}
int getvar(){
return i;
}
}
class MyThread3A extends Thread{
sharedvar v;
MyThread3A(sharedvar v){
this.v = v;
}
public void run(){
for(int i= 0;i <10; i++)
System.out.println("Value of shared variable "+ v.getvar());
}

}
class MyThread3B extends Thread{
sharedvar v;
MyThread3B(sharedvar v){
this.v = v;
}
public void run(){
for(int i= 0;i <10; i++){
System.out.println("value of shared variable after incrimenting "+v.incvar());
}
}
}
class Thread3{
static public void main(String[] args){
sharedvar v = new sharedvar();
Thread myThread3A = (Thread) new MyThread3A (v);
myThread3A.start();
Thread myThread3B = (Thread) new MyThread3B (v);
myThread3B.start();
}
}

--------------------------------


/* Program to demonstrate producer/consumer problem
Author : Team -J
Version : 1.0 */
class sharedvar{
int i;
sharedvar(){
i=0;
}
int incvar(){
return i++;
}
int getvar(){
return i;
}
}
class MyThread3A extends Thread{
sharedvar v;
MyThread3A(sharedvar v){
this.v = v;
Thread t = new Thread (this,"Thread3A");
t.run();
}
public void run(){
for(int i= 0;i <10; i++)
System.out.println("Value of shared variable "+ v.getvar());
}

}
class MyThread3B extends Thread{
sharedvar v;
MyThread3B(sharedvar v){
this.v = v;
Thread t = new Thread (this,"Thread3B");
t.run();
}
public void run(){
for(int i= 0;i <10; i++){
System.out.println("value of shared variable after incrimenting "+v.incvar());
}
}
}
class Thread3{
static public void main(String[] args){
sharedvar v = new sharedvar();
new MyThread3A (v);
new MyThread3B (v);
}
}

------------------------------------------------------------

/* Program to solve producer/consumer problem
Author : Team -J
Version : 1.0 */
class sharedvar{
int i;
boolean isvarset;
sharedvar(){
i=0;
isvarset=false;
}
synchronized int incvar(){
if( isvarset) //if var is set wait till the other thread reads it
try{
wait();
}catch(InterruptedException e){};
i++;
isvarset = true;
notify();
return i;
}
synchronized int getvar(){
if (!isvarset)try{
wait();
}catch(InterruptedException e){};
isvarset=false;
notify(); //notify the other thread which is waiting to incerment the value
return i;
}
}
//consumer
class MyThread4A extends Thread{
sharedvar v;
MyThread4A(sharedvar v){
this.v = v;
}
public void run(){
for(int i= 0;i <10; i++)
System.out.println("Value of shared variable "+ v.getvar());
}

}
//producer
class MyThread4B extends Thread{
sharedvar v;
MyThread4B(sharedvar v){
this.v = v;
}
public void run(){
for(int i= 0;i <10; i++)
System.out.println("value of shared variable after incrementing "+v.incvar());

}
}
class Thread4{
static public void main(String[] args){
sharedvar v = new sharedvar();
Thread myThread4A = (Thread) new MyThread4A (v);
myThread4A.start();
Thread myThread4B = (Thread) new MyThread4B (v);
myThread4B.start();
}
}

------------------------------------------------------------
/*Version : 1.0
Program to compute x pow 1 /1 ! + x pow 2 / 2! + x pow 3/ 3!
using for construct
Author : Team - J
*/
public class for2
{
public static void main(String args[]){
int i,j,no=3;
float sum=1.0f;
int prod,nopowx;
for(i=1 ; i<5;i++){
//find no power i
nopowx=no;
for(j=1;j
nopowx= nopowx * no;
System.out.println(" power = " + nopowx);
//find factorial of i
prod=1;
for(j=1;j<=i;j++){
prod=prod * j;
}
System.out.println(prod);
sum=sum + nopowx/prod;
}
System.out.println(" sum = " + sum);
}

}


---------------------------------------

/*Version : 1
This program displays a pattern
&&&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&&&
using for construct
Author : Team - J
*/
public class for3
{
public static void main(String args[]){
int no=10;
int i=1,j;
for(i=1;i<10;i++)
{
if ( (i %2 ) == 0)
System.out.print(" ");
else
System.out.print("&");

for(j=2;j <10;j++)
System.out.print("&");
if ( (i %2 ) == 0)
System.out.print(" ");
else
System.out.print("&");
System.out.println();
}

}
}


-----------------------------------------------------

/*Version : 1
This program uses break and continue
using for construct
Author : Team - J
*/
public class for4
{
public static void main(String args[]){
int no=10;
int i=1,j;
for(i=1;i<10;i++)
{
if ( (i %2 ) == 0)
continue;
/* break;*/
System.out.println(i);
}

}
}


-------------------------------------------------

/*Version : 1
This program uses break and continue
using for construct
Author : Team - J
*/
public class for5
{
public static void main(String args[]){
int no=10;
int i=1,j;
outer_for: for(j=0;j<10;j++){
for(i=1;i<10;i++)
{
if ( (i %2 ) == 0)
/* continue;*/
break outer_for;
System.out.println(i);
}
}

}
}


---------------------------------------


/*Version : 1
This program prints the following pattern
*********
*******
*****
***
*
using for construct
Author : Team - J
*/
public class for6
{
public static void main(String args[]){
int max=9,lines=5,spaces=0;
int i=1,j,k;
for(i=0;i
// print spaces first
for(k=0;k
System.out.print(" ");
for(j=0;j<(max- spaces*2);j++)
System.out.print("*");
spaces++;
System.out.println();
}

}
}



-----------------------------


/*Version : 1.0
This program prints the following pattern
*
***
*****
*******
*********
using for construct
Author : Team - J
*/
public class for7
{
public static void main(String args[]){
int stars=1,lines=5,spaces=5;
int i=1,j,k;
for(i=0;i
// print spaces first
for(k=0;k
System.out.print(" ");
for(j=0;j<(stars);j++)
System.out.print("*");
spaces--;
stars +=2;
System.out.println();
}

}
}


--------------------------------


/*Version : 1
This program prints the following pattern
*
***
*****
*******
*********
using for construct
Author : Team - J
*/
public class for8
{
public static void main(String args[]){
int stars=1,lines=5;
int i=1,j,k;
for(i=0;i
for(j=0;j<(stars);j++)
System.out.print("*");
stars+=2;
System.out.println();
}

}
}


---------------------------------------


/*Version : 1
This program prints the following pattern
using for construct
Author : Team - J
*/
public class for9
{
public static void main(String args[]){
int stars=1,lines=5;
int i=1,j,k;
for(i=0;;i++){
System.out.println(i);
}

}
}


-----------------------------------------

/*Version : 1.0
Program to show how multiple statements can be included in For init
using for construct
Author : Team - J
*/
public class for10
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
int prod,nopowx;
for(i=1,j=0,no=3 ; i<5;i++,f++,k+=99){
//find no power i
nopowx=no;
for(j=1;j
nopowx= nopowx * no;
System.out.println(" power = " + nopowx);
//find factorial of i
prod=1;
for(j=1;j<=i;j++){
prod=prod * j;
}
System.out.println(prod);
sum=sum + nopowx/prod;
}
System.out.println(" sum = " + sum);
System.out.println(i + " " + j + " " + f + " " + k);
}
}


-------------------------------

/*Version : 1.0
Program to show how to use multiple expression statements in For init
using for construct
Author : Team - J
*/
public class formultiexp
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
int prod,nopowx;
for(i=1,j=0,no=3,System.out.println("in For init") ; i<5;i++,f++,k+=99,System.out.println("in For update"),System.out.println("in For update2")){
System.out.println("in For body");
}

}
}


----------------------------------------------------


/*Version : 1
Program using for construct
1+1/1!+1/2!+1/3!+1/4!
Author : Team - J
*/
public class forone
{
public static void main(String args[]){
int i,j,no;
float sum=1.0f;
int prod;
for(i=1 ; i<5;i++){
//find factorial of i
prod=1;
for(j=1;j<=i;j++){
prod=prod * j;

}
System.out.println(prod);
sum=sum + 1.0F/prod;
}
System.out.println(" sum = " + sum);
}
}


---------------------------

ForStatement:
for ( ForInit opt ; Expression opt ; ForUpdate opt )
Statement | Block of Statements

ForInit:
StatementExpressionList
LocalVariableDeclaration
ForUpdate:
StatementExpressionList
StatementExpressionList:
StatementExpression
StatementExpressionList , StatementExpression

---------------------------------------------



/*Version : 1.0
Program to show how to use labled continue
using for construct
Author : Team - J
*/
public class lablecontinuefor
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
lab1:
System.out.println(" lable one");
//lab1:
//System.out.println(" lable one second time");
int prod,nopowx;

lab1:
//while(true){
//System.out.println(" in while");
lab2:
for(i=1; i<3;i++){

System.out.println(" value of i = " +i);
lab3:
for(j=1;j<=4;j++){
System.out.println(" value of j = " + j);
if( j ==1)
continue lab3;
System.out.println(" After break lab3");
if( j ==2)
continue lab3;
System.out.println(" After break lab3 ..");
if( j ==3)
continue lab1;
System.out.println(" After break lab1");
}
}
//}

}
}


--------------------------------------------


/*Version : 1.0
Program to show how to use labled break
using for construct
Author : Team - J
*/
public class lablefor
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
lab1:
System.out.println(" lable one");
//lab1:
//System.out.println(" lable one second time");
int prod,nopowx;

lab1:
while(true){
System.out.println(" in while");
lab2:
for(i=1; i<3;i++){

System.out.println(" value of i = " +i);
lab3:
for(j=1;j<=4;j++){
System.out.println(" value of j = " + j);
if( j ==1)
break lab3;
System.out.println(" After break lab3");
if( j ==2)
break lab3;
System.out.println(" After break lab2");
if( j ==3)
break lab1;
System.out.println(" After break lab1");
}
}
}

}
}

---------------------------------------------

JAVA3
/*Version : 1.0
Program to compute x pow 1 /1 ! + x pow 2 / 2! + x pow 3/ 3!
using for construct
Author : Team - J
*/
public class for2
{
public static void main(String args[]){
int i,j,no=3;
float sum=1.0f;
int prod,nopowx;
for(i=1 ; i<5;i++){
//find no power i
nopowx=no;
for(j=1;j
nopowx= nopowx * no;
System.out.println(" power = " + nopowx);
//find factorial of i
prod=1;
for(j=1;j<=i;j++){
prod=prod * j;
}
System.out.println(prod);
sum=sum + nopowx/prod;
}
System.out.println(" sum = " + sum);
}

}


---------------------------------------

/*Version : 1
This program displays a pattern
&&&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&&&
using for construct
Author : Team - J
*/
public class for3
{
public static void main(String args[]){
int no=10;
int i=1,j;
for(i=1;i<10;i++)
{
if ( (i %2 ) == 0)
System.out.print(" ");
else
System.out.print("&");

for(j=2;j <10;j++)
System.out.print("&");
if ( (i %2 ) == 0)
System.out.print(" ");
else
System.out.print("&");
System.out.println();
}

}
}


-----------------------------------------------------

/*Version : 1
This program uses break and continue
using for construct
Author : Team - J
*/
public class for4
{
public static void main(String args[]){
int no=10;
int i=1,j;
for(i=1;i<10;i++)
{
if ( (i %2 ) == 0)
continue;
/* break;*/
System.out.println(i);
}

}
}


-------------------------------------------------

/*Version : 1
This program uses break and continue
using for construct
Author : Team - J
*/
public class for5
{
public static void main(String args[]){
int no=10;
int i=1,j;
outer_for: for(j=0;j<10;j++){
for(i=1;i<10;i++)
{
if ( (i %2 ) == 0)
/* continue;*/
break outer_for;
System.out.println(i);
}
}

}
}


---------------------------------------


/*Version : 1
This program prints the following pattern
*********
*******
*****
***
*
using for construct
Author : Team - J
*/
public class for6
{
public static void main(String args[]){
int max=9,lines=5,spaces=0;
int i=1,j,k;
for(i=0;i
// print spaces first
for(k=0;k
System.out.print(" ");
for(j=0;j<(max- spaces*2);j++)
System.out.print("*");
spaces++;
System.out.println();
}

}
}



-----------------------------


/*Version : 1.0
This program prints the following pattern
*
***
*****
*******
*********
using for construct
Author : Team - J
*/
public class for7
{
public static void main(String args[]){
int stars=1,lines=5,spaces=5;
int i=1,j,k;
for(i=0;i
// print spaces first
for(k=0;k
System.out.print(" ");
for(j=0;j<(stars);j++)
System.out.print("*");
spaces--;
stars +=2;
System.out.println();
}

}
}


--------------------------------


/*Version : 1
This program prints the following pattern
*
***
*****
*******
*********
using for construct
Author : Team - J
*/
public class for8
{
public static void main(String args[]){
int stars=1,lines=5;
int i=1,j,k;
for(i=0;i
for(j=0;j<(stars);j++)
System.out.print("*");
stars+=2;
System.out.println();
}

}
}


---------------------------------------


/*Version : 1
This program prints the following pattern
using for construct
Author : Team - J
*/
public class for9
{
public static void main(String args[]){
int stars=1,lines=5;
int i=1,j,k;
for(i=0;;i++){
System.out.println(i);
}

}
}


-----------------------------------------

/*Version : 1.0
Program to show how multiple statements can be included in For init
using for construct
Author : Team - J
*/
public class for10
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
int prod,nopowx;
for(i=1,j=0,no=3 ; i<5;i++,f++,k+=99){
//find no power i
nopowx=no;
for(j=1;j
nopowx= nopowx * no;
System.out.println(" power = " + nopowx);
//find factorial of i
prod=1;
for(j=1;j<=i;j++){
prod=prod * j;
}
System.out.println(prod);
sum=sum + nopowx/prod;
}
System.out.println(" sum = " + sum);
System.out.println(i + " " + j + " " + f + " " + k);
}
}


-------------------------------

/*Version : 1.0
Program to show how to use multiple expression statements in For init
using for construct
Author : Team - J
*/
public class formultiexp
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
int prod,nopowx;
for(i=1,j=0,no=3,System.out.println("in For init") ; i<5;i++,f++,k+=99,System.out.println("in For update"),System.out.println("in For update2")){
System.out.println("in For body");
}

}
}


----------------------------------------------------


/*Version : 1
Program using for construct
1+1/1!+1/2!+1/3!+1/4!
Author : Team - J
*/
public class forone
{
public static void main(String args[]){
int i,j,no;
float sum=1.0f;
int prod;
for(i=1 ; i<5;i++){
//find factorial of i
prod=1;
for(j=1;j<=i;j++){
prod=prod * j;

}
System.out.println(prod);
sum=sum + 1.0F/prod;
}
System.out.println(" sum = " + sum);
}
}


---------------------------

ForStatement:
for ( ForInit opt ; Expression opt ; ForUpdate opt )
Statement | Block of Statements

ForInit:
StatementExpressionList
LocalVariableDeclaration
ForUpdate:
StatementExpressionList
StatementExpressionList:
StatementExpression
StatementExpressionList , StatementExpression

---------------------------------------------



/*Version : 1.0
Program to show how to use labled continue
using for construct
Author : Team - J
*/
public class lablecontinuefor
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
lab1:
System.out.println(" lable one");
//lab1:
//System.out.println(" lable one second time");
int prod,nopowx;

lab1:
//while(true){
//System.out.println(" in while");
lab2:
for(i=1; i<3;i++){

System.out.println(" value of i = " +i);
lab3:
for(j=1;j<=4;j++){
System.out.println(" value of j = " + j);
if( j ==1)
continue lab3;
System.out.println(" After break lab3");
if( j ==2)
continue lab3;
System.out.println(" After break lab3 ..");
if( j ==3)
continue lab1;
System.out.println(" After break lab1");
}
}
//}

}
}


--------------------------------------------


/*Version : 1.0
Program to show how to use labled break
using for construct
Author : Team - J
*/
public class lablefor
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
lab1:
System.out.println(" lable one");
//lab1:
//System.out.println(" lable one second time");
int prod,nopowx;

lab1:
while(true){
System.out.println(" in while");
lab2:
for(i=1; i<3;i++){

System.out.println(" value of i = " +i);
lab3:
for(j=1;j<=4;j++){
System.out.println(" value of j = " + j);
if( j ==1)
break lab3;
System.out.println(" After break lab3");
if( j ==2)
break lab3;
System.out.println(" After break lab2");
if( j ==3)
break lab1;
System.out.println(" After break lab1");
}
}
}

}
}

---------------------------------------------

JAVA3
/*Version : 1.0
Program to compute x pow 1 /1 ! + x pow 2 / 2! + x pow 3/ 3!
using for construct
Author : Team - J
*/
public class for2
{
public static void main(String args[]){
int i,j,no=3;
float sum=1.0f;
int prod,nopowx;
for(i=1 ; i<5;i++){
//find no power i
nopowx=no;
for(j=1;j
nopowx= nopowx * no;
System.out.println(" power = " + nopowx);
//find factorial of i
prod=1;
for(j=1;j<=i;j++){
prod=prod * j;
}
System.out.println(prod);
sum=sum + nopowx/prod;
}
System.out.println(" sum = " + sum);
}

}


---------------------------------------

/*Version : 1
This program displays a pattern
&&&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&&&
using for construct
Author : Team - J
*/
public class for3
{
public static void main(String args[]){
int no=10;
int i=1,j;
for(i=1;i<10;i++)
{
if ( (i %2 ) == 0)
System.out.print(" ");
else
System.out.print("&");

for(j=2;j <10;j++)
System.out.print("&");
if ( (i %2 ) == 0)
System.out.print(" ");
else
System.out.print("&");
System.out.println();
}

}
}


-----------------------------------------------------

/*Version : 1
This program uses break and continue
using for construct
Author : Team - J
*/
public class for4
{
public static void main(String args[]){
int no=10;
int i=1,j;
for(i=1;i<10;i++)
{
if ( (i %2 ) == 0)
continue;
/* break;*/
System.out.println(i);
}

}
}


-------------------------------------------------

/*Version : 1
This program uses break and continue
using for construct
Author : Team - J
*/
public class for5
{
public static void main(String args[]){
int no=10;
int i=1,j;
outer_for: for(j=0;j<10;j++){
for(i=1;i<10;i++)
{
if ( (i %2 ) == 0)
/* continue;*/
break outer_for;
System.out.println(i);
}
}

}
}


---------------------------------------


/*Version : 1
This program prints the following pattern
*********
*******
*****
***
*
using for construct
Author : Team - J
*/
public class for6
{
public static void main(String args[]){
int max=9,lines=5,spaces=0;
int i=1,j,k;
for(i=0;i
// print spaces first
for(k=0;k
System.out.print(" ");
for(j=0;j<(max- spaces*2);j++)
System.out.print("*");
spaces++;
System.out.println();
}

}
}



-----------------------------


/*Version : 1.0
This program prints the following pattern
*
***
*****
*******
*********
using for construct
Author : Team - J
*/
public class for7
{
public static void main(String args[]){
int stars=1,lines=5,spaces=5;
int i=1,j,k;
for(i=0;i
// print spaces first
for(k=0;k
System.out.print(" ");
for(j=0;j<(stars);j++)
System.out.print("*");
spaces--;
stars +=2;
System.out.println();
}

}
}


--------------------------------


/*Version : 1
This program prints the following pattern
*
***
*****
*******
*********
using for construct
Author : Team - J
*/
public class for8
{
public static void main(String args[]){
int stars=1,lines=5;
int i=1,j,k;
for(i=0;i
for(j=0;j<(stars);j++)
System.out.print("*");
stars+=2;
System.out.println();
}

}
}


---------------------------------------


/*Version : 1
This program prints the following pattern
using for construct
Author : Team - J
*/
public class for9
{
public static void main(String args[]){
int stars=1,lines=5;
int i=1,j,k;
for(i=0;;i++){
System.out.println(i);
}

}
}


-----------------------------------------

/*Version : 1.0
Program to show how multiple statements can be included in For init
using for construct
Author : Team - J
*/
public class for10
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
int prod,nopowx;
for(i=1,j=0,no=3 ; i<5;i++,f++,k+=99){
//find no power i
nopowx=no;
for(j=1;j
nopowx= nopowx * no;
System.out.println(" power = " + nopowx);
//find factorial of i
prod=1;
for(j=1;j<=i;j++){
prod=prod * j;
}
System.out.println(prod);
sum=sum + nopowx/prod;
}
System.out.println(" sum = " + sum);
System.out.println(i + " " + j + " " + f + " " + k);
}
}


-------------------------------

/*Version : 1.0
Program to show how to use multiple expression statements in For init
using for construct
Author : Team - J
*/
public class formultiexp
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
int prod,nopowx;
for(i=1,j=0,no=3,System.out.println("in For init") ; i<5;i++,f++,k+=99,System.out.println("in For update"),System.out.println("in For update2")){
System.out.println("in For body");
}

}
}


----------------------------------------------------


/*Version : 1
Program using for construct
1+1/1!+1/2!+1/3!+1/4!
Author : Team - J
*/
public class forone
{
public static void main(String args[]){
int i,j,no;
float sum=1.0f;
int prod;
for(i=1 ; i<5;i++){
//find factorial of i
prod=1;
for(j=1;j<=i;j++){
prod=prod * j;

}
System.out.println(prod);
sum=sum + 1.0F/prod;
}
System.out.println(" sum = " + sum);
}
}


---------------------------

ForStatement:
for ( ForInit opt ; Expression opt ; ForUpdate opt )
Statement | Block of Statements

ForInit:
StatementExpressionList
LocalVariableDeclaration
ForUpdate:
StatementExpressionList
StatementExpressionList:
StatementExpression
StatementExpressionList , StatementExpression

---------------------------------------------



/*Version : 1.0
Program to show how to use labled continue
using for construct
Author : Team - J
*/
public class lablecontinuefor
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
lab1:
System.out.println(" lable one");
//lab1:
//System.out.println(" lable one second time");
int prod,nopowx;

lab1:
//while(true){
//System.out.println(" in while");
lab2:
for(i=1; i<3;i++){

System.out.println(" value of i = " +i);
lab3:
for(j=1;j<=4;j++){
System.out.println(" value of j = " + j);
if( j ==1)
continue lab3;
System.out.println(" After break lab3");
if( j ==2)
continue lab3;
System.out.println(" After break lab3 ..");
if( j ==3)
continue lab1;
System.out.println(" After break lab1");
}
}
//}

}
}


--------------------------------------------


/*Version : 1.0
Program to show how to use labled break
using for construct
Author : Team - J
*/
public class lablefor
{
public static void main(String args[]){
int i,j,no=3,f=0,k=10;
float sum=1.0f;
lab1:
System.out.println(" lable one");
//lab1:
//System.out.println(" lable one second time");
int prod,nopowx;

lab1:
while(true){
System.out.println(" in while");
lab2:
for(i=1; i<3;i++){

System.out.println(" value of i = " +i);
lab3:
for(j=1;j<=4;j++){
System.out.println(" value of j = " + j);
if( j ==1)
break lab3;
System.out.println(" After break lab3");
if( j ==2)
break lab3;
System.out.println(" After break lab2");
if( j ==3)
break lab1;
System.out.println(" After break lab1");
}
}
}

}
}