* Write a Program to print "Hello World".
class hello
{
public static void main(String args[])
{
System.out.println("Hello Wrold");
}
}
/*
Save file with class name.java E.g hello.java
goto >start > run >cmd.
select source PATH E.g D:\jigs\java>
Compile This program:- javac hello.java
Rum Program:-java hello
*/
* W.A.P to get the value from user and perform arithmetic operation.
import java.io.*;
class userget
{
public static void main(String arg[])
{
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.print("Enter Any num for A:=");
int a=Integer.parseInt(br.readLine());
System.out.print("Enter Any num for B:=");
int b=Integer.parseInt(br.readLine());
System.out.println("Addition:="+(a+b));
System.out.println("Substraction:="+(a-b));
System.out.println("Miltiplication:="+(a*b));
System.out.println("Division:="+(a/b));
}
catch(Exception e)
{
System.out.println(e);
}
}
}
* W.A.P to get value from Commandline.
class commandline
{
public static void main(String args[])
{
System.out.println(arg[0]);
System.out.println(arg[1]);
}
}
* W.A.P to represent static variable.
class hello
{
static int a=10;
static public void disp()
{
final int a=5;
System.out.println(a);
}
}
class static_var
{
public static void main(String args[])
{
hello h=new hello();
h.disp(); //or
hello.disp();
}
}
* W.A.P to use of boolean datatype.class boolean1
{
public static void main(String args[])
{
boolean b1="jigs"=="jigs";
boolean b2=new String("jigs")=="jigs";
boolean b3=new String("jigs")==new String("jigs");
System.out.println(b1 && b2 || b3);
}
}
* W.A.P to calculate factorial of a number.
class facto
{
public static void main(String args[])
{
int i,a,fact=1;
int n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.print(fact);
}
}
* W.A.P to enter two numbers and print the one number raised power of another number.
class power
{
public static void main(String args[])
{
int i,ans=1;
int n=Integer.parseInt(arg[0]);
int m=Integer.parseInt(arg[1]);
for(i=1;i<=m;i++)
{
ans=ans*n;
}
System.out.print(ans);
}
}
* W.A.P to swap two values.
class swap
{
public static void main(String args[])
{
int c;
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
c=a;
a=b;
b=c;
System.out.println(a);
System.out.println(b);
}
}
* W.A.P to check the number is prime number or not.
class prime
{
public static void main(String arg[])
{
int a,i,f=0;
a=Integer.parseInt(arg[0]);
for(i=2;i<a;i++)
{
if(a%i==0)
{
System.out.println("This Number isnot Prime");
f=1;
break;
}
}
if(f==0)
{
System.out.println("This Number is Prime");
}
}
}
* W.A.P to check the number is armstrong or not.
153 is armstrong number.
class armstrong
{
public static void main(String arg[])
{
int a=0,n,b,i,l;
l=Integer.parseInt(arg[0]);
n=l;
while(n!=0)
{
b=n%10;
a=a+(b*b*b);
n=n/10;
}
if(l==a)
{
System.out.println("armstrong");
}
else
{
System.out.println("not armstrong");
}
}
}
* W.A.P to check the number is palindron or not.
12321 is palindrom number.
class palindrom
{
public static void main(String arg[])
{
int a=0,e,b;
int n=Integer.parseInt(arg[0]);
b=n;
while(n!=0)
{
e=n%10;
a=(a*10)+e;
n=n/10;
}
if(a==b)
{
System.out.println("Palindrom Number");
}
else
{
System.out.println("Not Palindrom Number");
}
}
}
* W.A.P to print a number in reverse.
class revnum
{
public static void main(String arg[])
{
int a=0,e;
int n=Integer.parseInt(arg[0]);
while(n!=0)
{
e=n%10;
a=(a*10)+e;
n=n/10;
}
System.out.print(a);
}
}
* W.A.P to print a string in reverse without using reverse function.
import java.util.*;
import java.io.*;
class reversestring
{
public static void main(String args[])
{
String s, rev="";
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.print("Enter Any String:=");
s=br.readLine();
int len = s.length();
for (int i = len - 1 ; i >= 0 ; i-- )
{
rev = rev + s.charAt(i);
}
System.out.println("Reverse of string is "+rev);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print fibonacci series.
class fibonacci
{
public static void main(String arg[])
{
int i,a,b=1,c=0;
for(i=1;i<=10;i++)
{
a=b;
b=c;
c=a+b;
System.out.print(c);
System.out.print(" ");
}
}
}
* W.A.P to calculate this 1/1, 2/2, 3/3, 4/4, 5/5 series.
class serise
{
public static void main(String arg[])
{
float a,b,total=0,sum=0;
for(a=1;a<=5;a++)
{
for(b=1;b<=a;b++)
{
total=a/b;
}
sum=sum+total;
}
System.out.println(sum);
}
}
* W.A.P to calculate 1/1!, 2/2!, 3/3!, 4/4!, 5/5! series.
class serise2
{
public static void main(String arg[])
{
float a,b,total=0,sum=0,ans=1;
for(a=1;a<=5;a++)
{
ans=1;
for(b=a;b>=1;b--)
{
ans=ans*b;
}
total=a/ans;
sum=sum+total;
}
System.out.println(sum);
}
}
* W.A.P to calculate 1-2+3-4+5-6 series.
class serise3
{
public static void main(String arg[])
{
int a,total=0,sub=0;
for(a=1;a<=5;a=a+2)
{
sub=a-(a+1);
total=total+sub;
}
System.out.println(total);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print following pattern.
aaaaaa
bbbbb
cccc
ddd
ee
f
class pattern1
{
public static void main(String arg[])
{
char i,j;
for(i='a';i<='f';i++)
{
for(j=i;j<='f';j++)
{
System.out.print(i);
}
System.out.println("");
}
}
}
* W.A.P to print following pattern.
1
12
123
1234
12345
class pattern2
{
public static void main(String arg[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println("");
}
}
}
* W.A.P to print following pattern.
abcdef
bcdef
cdef
def
ef
f
class pattern4
{
public static void main(String arg[])
{
char i,j;
for(i='a';i<='f';i++)
{
for(j=i;j<='f';j++)
{
System.out.print(j);
}
System.out.println("");
}
}
}
* W.A.P to print following pattern.
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
class pattern6
{
public static void main(String arg[])
{
int i,j,l,k;
for(i=1;i<=5;i++)
{
for(k=5;k>=i;k--)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
System.out.print("*");
System.out.print(" ");
}
System.out.println(" ");
}
for(i=1;i<=5;i++)
{
for(k=1;k<=i;k++)
{
System.out.print(" ");
}
for(j=i;j<=5;j++)
{
System.out.print("*");
System.out.print(" ");
}
System.out.println(" ");
}
}
}
* W.A.P to print following pattern.
*
**
* *
* *
* *
* *
* *
* *
* *
**********
class pattern9
{
public static void main(String arg[])
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
{
if(j==1 || j==i || i==10)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
* W.A.P to print following pattern.
999999999
8 8
7 7
6 6
5 5
4 4
3 3
2 2
1
class pattern11
{
public static void main(String arg[])
{
int i,j;
for(i=9;i>=0;i--)
{
for(j=9;j>=i;j--)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
if(i==9 || j==i || j==1)
{
System.out.print(i);
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
* W.A.P to print following pattern.
* *
* *
* *
* *
**
**
* *
* *
* *
* *
class pattern12
{
public static void main(String arg[])
{
int i,j;
for(i=0;i<=9;i++)
{
for(j=0;j<=9;j++)
{
if(i==j || (j+i)==9)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use of this keyword.
class hello
{
String name;
int age;
hello(String name,int age)
{
this.name=name;
this.age=age;
System.out.println("Name is:="+name);
System.out.println("Age is:="+age);
}
}
public class thiskeyword
{
public static void main(String arg[])
{
hello h=new hello("jigs",23);
}
}
* W.A.P to represent of abstract class.
/*
1, abstract method has no body
2, no object of any abstract class
3, always extends by another class
it's contain atlist one abstract method
*/
abstract class xy
{
int x=5,y=8;
abstract void add();
}
class xydemo extends xy
{
void add()
{
System.out.println("Addition is:="+(x+y));
}
}
class abstract1
{
public static void main(String arg[])
{
xydemo d1=new xydemo();
d1.add();
}
}
* W.A.P to use of scanner class.
import java.util.*;
public class scannerdemo
{
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
String name;
int no;
System.out.print("Enter any Name:=");
name=s.nextLine();
System.out.print("Enter any Number:=");
no=s.nextInt();
System.out.println("Name="+name);
System.out.println("no="+no);
}
}
* W.A.P to use of stack class.
import java.util.*;
class Stackdemo
{
public static void main(String arg[])
{
Stack st=new Stack();
for(int a=0;a<arg.length;a++)
st.push(new Integer(arg[a]));
while(! st.empty())
{
Object obj=st.pop();
System.out.println(obj);
}
}
}
* W.A.P to use of string tokenizer class.
import java.util.*;
class Stringtokenizer
{
public static void main(String arg[])
{
String str="Hi I am Jigs Shekhada";
StringTokenizer st= new StringTokenizer(str," / ");
while(st.hasMoreTokens())
{
String s=st.nextToken();
System.out.println(s);
}
}
}
* W.A.P to represent boxing and autoboxing.
/*
boxing= primitive to non primitive
autoboxing=nonprimitive to primitive
*/
class wrap
{
public static void main(String arg[])
{
int p=7;
Integer a=new Integer(p);//boxing
System.out.println("value of object="+a.intValue());
int x=a.intValue();//unboxing
System.out.println("value of variable="+x);
Integer b=p;//autoboxing
System.out.println("value of object="+b);//both are
System.out.println("value of object="+b.intValue());//same
int y=b;//Autoboxing
System.out.println("Value of object="+y);
}
}
* W.A.P to use of list & set of collection.
/*
1, ordearing List
2, no duplicat value in Set
*/
import java.util.*;
class list_set
{
public static void main(String arg[])
{
List<Integer>t2=new ArrayList<Integer>();
//Set t2=new HashSet();
t2.add(2);
t2.add(1);
t2.add(3);
t2.add(3);
/*
t2.add("first");
t2.add(new Integer(5));
t2.add(new Float(5.5));
*/
System.out.println("liat is:="+t2);
}
}
* W.A.P to print System date.
import java.util.*;
class Datedemo
{
public static void main(String arg[])
{
Date d=new Date();
System.out.println(d);
}
}
* W.A.P to use of claendar class.
import java.util.*;
class Calendardemo
{
public static void main(String arg[])
{
Calendar cl=Calendar.getInstance();
System.out.println("Date:="+cl.get(Calendar.DATE));
System.out.println("Month:="+cl.get(Calendar.MONTH));
System.out.println("Year:="+cl.get(Calendar.YEAR));
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print 1 to 5 using array.
class Array
{
public static void main(String arg[])
{
int arr[]=new int[5];
int i;
for(i=0;i<5;i++)
{
arr[i]=i+1;
}
for(i=0;i<5;i++)
{
System.out.println(arr[i]);
}
}
}
* W.A.P to use of double dim array.
class Array_double
{
public static void main(String arg[])
{
int arr[][]=new int[3][3];
int i,j,a=1;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr[i][j]=a++;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print("\t"+arr[i][j]);
}
System.out.println(" ");
}
}
}
* W.A.P to print character using array.
class char_array
{
public static void main(String arg[])
{
char arr[]={'a','b','c','d','e'};
int i;
for(i=0;i<5;i++)
{
System.out.println(arr[i]);
}
}
}
* W.A.P to use of object of array.
import java.io.*;
class objectarray
{
int rno,m1,m2,m3;
String name;
void getvalue()
{
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.print("Enter Rno:=");
rno=Integer.parseInt(br.readLine());
System.out.print("Enter Name:=");
name=br.readLine();
System.out.print("Enter M1:=");
m1=Integer.parseInt(br.readLine());
System.out.print("Enter M2:=");
m2=Integer.parseInt(br.readLine());
System.out.print("Enter M3:=");
m3=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println(e);
}
}
void display()
{
System.out.println("Rno:="+rno);
System.out.println("Name:="+name);
System.out.println("M1:="+m1);
System.out.println("m2:="+m2);
System.out.println("m3:="+m3);
}
}
class objectofarray
{
public static void main(String arg[])
{
int i;
objectarray objarr[]=new objectarray[5];
for(i=0;i<5;i++)
{
objarr[i]=new objectarray();
}
for(i=0;i<5;i++)
{
objarr[i].getvalue();
}
for(i=0;i<5;i++)
{
objarr[i].display();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print message using method.
class hello
{
public void print()
{
System.out.println("hello method");
}
public void display()
{
System.out.println("display second method");
}
}
class simplemethod
{
public static void main(String arg[])
{
hello h=new hello();
h.print();
h.display();
hello h1=new hello();
h1.print();
}
}
* W.A.P to passing arguments to method.
class hello
{
int c;
public void add(int a,int b)
{
c=a+b;
System.out.println("addition is:="+c);
c=a-b;
System.out.println("Substraction is:="+c);
c=a*b;
System.out.println("Multiplication is:="+c);
c=a/b;
System.out.println("division is:="+c);
}
}
public class paramethod
{
public static void main(String arg[])
{
hello h=new hello();
h.add(20,10);
}
}
* W.A.P to method can return value.
public class methodreturn
{
public static void main(String arg[])
{
int x,y,z;
x=Integer.parseInt(arg[0]);
y=Integer.parseInt(arg[1]);
multi m=new multi();
z=m.mul(x,y);
System.out.println("ans:="+z);
}
}
class multi
{
public int mul(int x,int y)
{
return x*y;
}
}
* W.A.P to represent the Recurson.
class recurson
{
int power(int b,int p)
{
if(p==0)
return 1;
else
return b*power(b,p-1);
}
public static void main(String arg[])
{
recurson r=new recurson();
int b,p,m;
b=Integer.parseInt(arg[0]);
p=Integer.parseInt(arg[1]);
m=r.power(b,p);
System.out.println("power is:="+m);
}
}
* W.A.P to represent method overloadding.
/*
the return type and argument must be diffrent
*/
class over
{
int a,c;
float b;
public void display(int a)
{
this.a=a;
System.out.println(a);
}
public float display(float b)
{
this.b=b;
return(this.b);
}
}
class overloading
{
public static void main(String arg[])
{
float x;
over o=new over();
o.display(10);
x=o.display(20.5f);
System.out.println(x);
}
}
* W.A.P to represrnt methodoverriding.
class hello
{
public void print(int a,int b)
{
int c=a+b;
System.out.println("Addition is:="+c);
}
}
class hi extends hello
{
public void print(int a,int b)
{
int c=a-b;
System.out.println("Substraction is:="+c);
}
}
public class methodoverriding
{
public static void main(String arg[])
{
hi h=new hi();
hello he=new hello();
he.print(10,20);
h.print(10,20);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print message using constructor.
class hello
{
hello()//default constructor.
{
System.out.println("default const");
}
hello(int a,int b)// parameter constructor.
{
int c=a+b;
System.out.println("Addition is:="+c);
}
}
public class bothconst
{
public static void main(String arg[])
{
hello d=new hello();
hello h=new hello(20,10);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print data using inheritance.
class emp
{
String name;
int age;
emp(String name,int age)
{
this.name=name;
this.age=age;
System.out.println("Name is:="+name);
System.out.println("Age is:="+age);
}
}
class manager extends emp
{
String dept;
manager(String name,int age,String dept)
{
super(name,age);
this.dept=dept;
System.out.println("dept is :="+dept);
}
}
public class inhe_const
{
public static void main(String arg[])
{
manager m=new manager("jigs",23,"HR");
}
}
*W.A.P to use multiple constructor in inheritance.
class employee
{
String name;
int age;
double salary;
employee(String name,int age,double salary)
{
this.name=name;
this.age=age;
this.salary=salary;
System.out.println(name);
System.out.println(age);
System.out.println(salary);
}
employee(String name,int age)
{
this.name=name;
this.age=age;
System.out.println(name);
System.out.println(age);
}
employee(String name)
{
this.name=name;
System.out.println(name);
}
}
class manager extends employee
{
String dept;
manager(String name,int age,double salary,String dept)
{
super(name,age,salary);
this.dept=dept;
System.out.println(dept);
}
manager(String name,int age,String dept)
{
super(name,age);
this.dept=dept;
System.out.println(dept);
}
manager(String name,String dept)
{
super(name);
this.dept=dept;
System.out.println(dept);
}
}
public class inheritance
{
public static void main(String arg[])
{
manager m=new manager("NIIT",32,132.68,"civil");
manager m1=new manager("c.g.road",15,"it");
manager m2=new manager("vastrapur","computer");
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use of polymorphism.
abstract class Shape
{
abstract void draw();
}
class Circle extends Shape
{
void draw()
{
System.out.println("Circle");
}
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("Rectangle");
}
}
class Square extends Shape
{
void draw()
{
System.out.println("Square");
}
}
class Polymorphism
{
public static void main(String arg[])
{
Shape ref;
ref=new Circle();
ref.draw();
ref=new Rectangle();
ref.draw();
ref=new Square();
ref.draw();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use of interface.
interface A
{
public void display();
}
class B implements A
{
public void display()
{
System.out.println("Hello");
}
}
class hello_inf
{
public static void main(String arg[])
{
B b=new B();
b.display();
}
}
* W.A.P to comparable interface.
import java.util.*;
class student implements Comparable
{
int rno=0;
String fname,lname;
double gpa=0.0;
public student(int rno, String fname, String lname, double gpa)
{
if(rno==0 || fname==null || lname==null || gpa==0.0)
{
throw new IllegalArgumentException();
}
this.rno=rno;
this.fname=fname;
this.lname=lname;
this.gpa=gpa;
}
public int rno()
{
return rno;
}
public String fname()
{
return fname;
}
public String lname()
{
return lname;
}
public double gpa()
{
return gpa;
}
public int compareTo(Object o)
{
int f=rno-((student)o).rno;
if(f==0)
{
return 0;
}
else if(f<0.0)
{
return -1;
}
else
{
return 1;
}
}
}
class comparabletest
{
public static void main(String arg[])
{
TreeSet stuset=new TreeSet();
stuset.add(new student(101,"jigs","shekhada",9.1));
stuset.add(new student(102,"raju","sharma",8.0));
stuset.add(new student(103,"hars","kalola",7.0));
Object[] starray=stuset.toArray();
student s;
for(Object i:starray)
{
s=(student)i;
System.out.printf("\n Rollno:=%d name=%s%s gpa=%2f",s.rno(),s.fname(),s.lname(),s.gpa());
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P print data using package.
// First create folder name with jigs, then save following program inside it, and only compile but not run.
package jigs;
public class pack
{
public void display()
{
System.out.println("hello");
}
}
// go back from jigs folder and create new following program, and run this program.
import jigs.pack;
public class p1
{
public static void main(String arg[])
{
pack p=new pack();
p.display();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to generate exception in try block and print types of exception in catch block.
class Throw
{
public static void main(String arg[])
{
try
{
int a=Integer.parseInt(arg[0]);
int b=Integer.parseInt(arg[1]);
System.out.println("Division is:="+(a/b));
throw new NumberFormatException();
}
catch(ArithmeticException ae)
{
ae.printStackTrace();
}
catch(NumberFormatException nfe)
{
System.out.println("Exception catch");
}
/*catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Insufficient Argument");
}*/
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println("End of Pro");
}
}
}
* W.A.P to use of throws in exception.
class check
{
void division(int a,int b) throws ArithmeticException
{
System.out.println("Division"+(a/b));
}
}
class throwsdemo
{
public static void main(String arg[])
{
try
{
check ck=new check();
ck.division(6,0);
}
catch(ArithmeticException ae)
{
System.out.println("argument cannot be zero");
}
}
}
* W.A.P to represent catch search.
class CatchSearch
{
static String x[];
public static void main(String arg[])
{
x=arg;
try
{
System.out.println("Before A");
a();
System.out.println("After A");
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println("main:finally");
}
}
static void a()
{
try
{
System.out.println("Before B");
b();
System.out.println("After B");
}
catch(ArithmeticException ae)
{
System.out.println("Second Argument");
}
finally
{
System.out.println("A:finally");
}
}
static void b()
{
try
{
System.out.println("Before c");
c();
System.out.println("After c");
}
catch(NumberFormatException nfe)
{
System.out.println("Argument(s)");
}
finally
{
System.out.println("B:finally");
}
}
static void c()
{
try
{
System.out.println("Before D");
d();
System.out.println("After D");
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println("Insufficient Argument");
}
finally
{
System.out.println("C:finally");
}
}
static void d()
{
int a,b;
a=Integer.parseInt(x[0]);
b=Integer.parseInt(x[1]);
System.out.println("Before Division");
System.out.println(a/b);
System.out.println("After Division");
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print string or word using thread.
class ThreadB implements Runnable
{
public void run()
{
try
{
for(int a=1;a<=10;a++)
{
System.out.println("Threadb");
Thread.sleep(2000);
}
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
}
class ThreadDemo
{
public static void main(String arg[])
{
ThreadB obj=new ThreadB();
Thread t = new Thread(obj);
t.start();
}
}
* W.A.P to join two thread.
class x extends Thread
{
public void run()
{
try
{
for(int a=1;a<10;a++)
{
System.out.println("jignesh");
Thread.sleep(500);
}
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
}
class y extends Thread
{
public void run()
{
try
{
for(int a=1;a<=10;a++)
{
System.out.println("shekhada");
Thread.sleep(500);
}
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
}
class join
{
public static void main(String arg[])
{
x objx=new x();
y objy=new y();
objx.start();
objy.start();
try
{
objx.join();
objy.join();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("main thread");
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P program to write data in file.
import java.io.*;
public class WriteFile
{
public static void main(String args[])
{
File file=new File("F:\\jig.txt");
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(isr);
PrintWriter out=new PrintWriter(new FileWriter(file));
String s;
System.out.println("Enter File.txt");
System.out.println("[Type ctrl-d to stop.]");
while((s=in.readLine()) != null)
{
out.println(s);
}
in.close();
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
* W.A.P to read data from file.
import java.io.*;
public class ReadFile
{
public static void main(String args[])
{
File file=new File(args[0]); //give file name as command line.
try
{
BufferedReader in=new BufferedReader(new FileReader(file));
String s;
try
{
s=in.readLine();
while(s != null)
{
System.out.println("Read:="+s);
s=in.readLine();
}
}
finally
{
in.close();
}
}
catch(FileNotFoundException e)
{
System.out.println("file not found"+file);
}
catch(IOException e2)
{
e2.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use of socket programming.
// save following program socketclient.java
import java.net.*;
import java.io.*;
public class socketclient
{
public static void main(String arg[])
{
try
{
int port=Integer.parseInt(arg[0]);
String ip=System.getProperty("Server");
Socket s1=new Socket(ip,port);
BufferedReader br=new BufferedReader(new InputStreamReader(s1.getInputStream()));
System.out.println(br.readLine());
br.close();
s1.close();
}
catch(ConnectException cnn)
{
System.out.println("could not connect to Server");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
// save following program socketserver.java
import java.net.*;
import java.io.*;
public class socketserver
{
public static void main(String arg[])
{
ServerSocket s=null;
int port=Integer.parseInt(arg[0]);
//register your service on port 5432
try
{
s=new ServerSocket(port);
}
catch(IOException e)
{
e.printStackTrace();
}
// run the listen/accept loop forever
while(true)
{
try
{
//wait here and listen for a connection
Socket s1=s.accept();
System.out.println("Connection is accept:port="+s1.getPort());
//get outputStream associated with socket
OutputStream s1out=s1.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s1out));
//send your string
bw.write("hello net world");
bw.close();
s1.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
Note- How to run.
first compile socketserver.java // javac socketserver.java
run socketserver.java // javac socketserver 43
open new command prompt
first compile socketclient.java // javac socketclient.java
run socketclient.java // javac socketclient 43
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to applet life cycle.
import java.applet.*;
import java.awt.*;
/* <applet code="AppletLifeCycle" width=300 height=200>
</applet>
*/
public class AppletLifeCycle extends Applet
{
String str=" ";
public void init()
{
str+="init;";
}
public void start()
{
str+="start;";
}
public void stop()
{
str+="stop;";
}
public void destroy()
{
System.out.println("destroy");
}
public void paint(Graphics g)
{
g.drawString(str,10,40);
}
}
Note:= how to run.
compile = javac AppletLifeCycle.java
run = appletviewer AppletLifeCycle.java
* W.A.P to print Blue color string.
import java.applet.Applet;
import java.awt.*;
/* <applet code="BlueString" width=500 height=500>
</applet>
*/
public class BlueString extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawString("jignesh",100,50);
}
}
* W.A.P to draw arc in applet.
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
//<applet code="Arc" width=500 height=460> </applet>
public class Arc extends Applet implements Runnable
{
Random r=new Random();
int x=10,y=10,sang1=0,sang2=0,he=500,wi=500;
public void init()
{
Thread t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
try
{
repaint();
Thread.sleep(100);
if(x<wi-100)
x+=5;
if(y<he-100)
y+=5;
if(x>wi-100)
x=wi-100;
if(y>he-100)
y=he-100;
sang1+=10;
sang2+=10;
}
catch(Exception e)
{
}
}
}
public void paint(Graphics g)
{
Dimension d=getSize();
he=d.height;
wi=d.width;
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(x,20,100,100,sang1,90);
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(x,20,100,100,sang1 + 90,90)
// g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(x,20,100,100,sang1 + 180 ,90);
// g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(x,20,100,100,sang1 + 270 ,90);
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(10,y,100,100,sang2,90);
// g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(10,y,100,100,sang2 + 90,90);
// g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(10,y,100,100,sang2 + 180,90);
// g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(10,y,100,100,sang2 + 270,90);
}
}
* W.A.P to make digital clock in applet.
import java.applet.*;
import java.awt.*;
import java.util.Date;
/* <applet code="clockapplet" width=300 height=200>
</applet>
*/
public class clockapplet extends Applet implements Runnable
{
Thread timerThread;
/* Applet Lifestyle Methods */
public void start()
{
timerThread = new Thread(this, "Clock");
timerThread.start();
}
public void stop()
{
if (timerThread == null)
return;
timerThread.stop();
timerThread = null;
}
/* Runnable interface method */
public void run()
{
while (timerThread != null)
{
repaint(); // request a redraw
try
{
timerThread.sleep(1000);
}
catch (InterruptedException e)
{ /* do nothing*/ }
}
}
/* AWT method */
public void paint(Graphics g)
{
Date d = new Date();
g.drawString(d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(), 1, 10);
}
}
* W.A.P to draw Polygon in applet.
import java.applet.*;
import java.awt.*;
/* <applet code="DrawPolygon" width=300 height=300>
</applet>
*/
public class DrawPolygon extends Applet
{
public void paint(Graphics g)
{
int n=5;
int X[]=new int[n];
int Y[]=new int[n];
X[0]=10;
Y[0]=100;
X[1]=60;
Y[1]=10;
X[2]=70;
Y[2]=140;
X[3]=140;
Y[3]=90;
X[4]=190;
Y[4]=10;
g.drawPolygon(X,Y,n);
}
}
* W.A.P to print dimension in applet statusbar.
import java.applet.*;
import java.awt.*;
/* <applet code="showdim" width=300 height=200>
</applet>
*/
public class showdim extends Applet
{
public void paint(Graphics g)
{
Dimension d=getSize();
showStatus("width="+d.width + ";height="+d.height);
}
}
* W.A.P to draw ovel with color.
import java.applet.*;
import java.awt.*;
/*
<applet code="ovel" width=500 height=500>
</applet>
*/
public class ovel extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(120,120,220,220);
}
}
* W.A.P to draw circle in applet, when applet is resize then circle is in middel of applet.
import java.applet.*;
import java.awt.*;
/* <applet code="Circle" width=300 height=300>
</applet>
*/
public class Circle extends Applet
{
public void paint(Graphics g)
{
Dimension d = getSize();
int xc = d.width/2;
int yc = d.height/2;
int radius= (int) ((d.width < d.height) ?
0.4 * d.width:
0.4 * d.height);
g.drawOval(xc-radius,yc-radius,2 * radius,2 * radius);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use of cryptography using Ceasarcihper algorithm.
// save following program subsender.java
import java.net.*;
import java.io.*;
public class subsender
{
public static void main(String[] args)
{
try
{
InetAddress receiverHost = InetAddress.getByName(args[0]);
int receiverPort = Integer.parseInt(args[1]);
int fd,len1=0,i =0,temp =0;
char msg[] = new char[10];
String s = new String(args[2]);
char c[] = new char[10];
c =s.toCharArray();
len1=s.length();
for(i = 0 ; i < len1; i++)
{
temp=(int)(c[i])+6;
msg[i]= (char)(temp) ;
System.out.println("Encrypted message is: " + msg[i]);
temp=0;
}
String s1=new String();
s1=String.valueOf(msg);
System.out.println("Encrypted message is " + s1);
byte[ ] buffer = s1.getBytes( );
// instantiates a datagram socket for sending the data
DatagramSocket mySocket = new DatagramSocket();
DatagramPacket datagram = new DatagramPacket(buffer, buffer.length,receiverHost,receiverPort);
mySocket.send(datagram);
mySocket.close( );
}
catch (Exception ex)
{
ex.printStackTrace( );
}
}
}
// save following program subreciver.java
import java.net.*;
import java.io.*;
public class subreceiver
{
public static void main(String[] args)
{
if (args.length != 1)
System.out.println("This program requires one command line argument.");
else
{
int port = Integer.parseInt(args[0]);
final int MAX_LEN = 10;
int temp=0;
try
{
DatagramSocket mySocket = new DatagramSocket(port);
byte[ ] buffer = new byte[MAX_LEN];
DatagramPacket datagram =new DatagramPacket(buffer, MAX_LEN);
mySocket.receive(datagram);
String s = new String(buffer);
int len1,i;
s=s.trim();
len1=s.length();
char msg1[] = new char[10];
char msg[]=new char[10];
char asc[]=new char[10];
msg1 =s.toCharArray();
for(i=0;i<len1;i++)
{
asc[i]=msg1[i];
}
String s2=new String();
s2=String.valueOf(asc);
System.out.println("Encrypted message is:" + s2);
for(i=0;i<len1;i++)
{
temp=(int)(asc[i])-6;
msg[i]= (char)(temp) ;
temp=0;
}
String s1=new String();
s1=String.valueOf(msg);
System.out.println("Decreypted (Original) message is: " + s1);
mySocket.close( );
}
catch (Exception ex)
{
ex.printStackTrace( );
}
}
}
}
Note := how to run.
compile = javac subreciver.java
run = java subreciver 30
open new command prompt
compile= javac subsender.java
run= java subsender localhost 30
* W.A.P to use cryptography using onetimepad algorithm.
import java.net.*;
import java.io.*;
public class OneTimePadSender
{
public static void main(String[] args)
{
try
{
InetAddress receiverHost = InetAddress.getByName(args[0]);
int receiverPort = Integer.parseInt(args[1]);
String s = new String(args[2]);
int i;
String key="abcdefghijklmnopqrstuvwxyz";
String result="";
for(i=0;i<s.length();i++)
result+=(char)(s.charAt(i)^key.charAt(i));
System.out.println(result);
byte[ ] buffer = result.getBytes( );
DatagramSocket mySocket = new DatagramSocket();
DatagramPacket datagram = new DatagramPacket(buffer,buffer.length,receiverHost, receiverPort);
mySocket.send(datagram);
mySocket.close( );
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class OneTimePadReceiver
{
public static void main(String[] args)
{
try
{
int port = Integer.parseInt(args[0]);
final int MAX_LEN = 10;
int i;
DatagramSocket mySocket = new DatagramSocket(port);
byte[ ] buffer = new byte[MAX_LEN];
DatagramPacket datagram = new DatagramPacket(buffer,MAX_LEN);
mySocket.receive(datagram);
String s = new String(buffer);
String key="abcdefghijklmnopqrstuvwxyz";
String result="";
for(i=0;i<s.length();i++)
result=result+(char)(s.charAt(i)^key.charAt(i));
System.out.println(result);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
* W.A.P to use cryptography using p-box algorithm.
import java.net.*;
import java.io.*;
// Three command line arguments are expected, in order:
// <domain name or IP address of the receiver>
// <port number of the receiver's socket>
// <message string to send>
public class pboxsender
{
public static void main(String[] args)
{
if (args.length != 3)
System.out.println("This program requires three command line arguments");
else
{
try
{
InetAddress receiverHost = InetAddress.getByName(args[0]);
int receiverPort = Integer.parseInt(args[1]);
String s = new String(args[2]);
char c[] = new char[10];
c =s.toCharArray();
DatagramSocket mySocket = new DatagramSocket();
char temp[]= new char[9];
temp[0]=c[3];
temp[1]=c[6];
temp[2]=c[0];
temp[3]=c[7];
temp[4]=c[1];
temp[5]=c[2];
temp[6]=c[4];
temp[7]=c[5];
String s1=new String();
s1=String.valueOf(temp);
System.out.println("Plaintext is output of Sender P-box =: " + s1);
byte[ ] buffer = s1.getBytes( );
DatagramPacket datagram = new DatagramPacket(buffer, buffer.length,receiverHost, receiverPort);
mySocket.send(datagram);
mySocket.close( );
} // end try
catch (Exception ex)
{
ex.printStackTrace( );
}
} // end else
} // end main
} // end class
import java.net.*;
import java.io.*;
// This program requires one command line argument: port number of the receiver's socket.
//The receiver port number and the sender port number should be same.
public class pboxreceiver
{
public static void main(String[] args)
{
if (args.length != 1)
System.out.println("This program requires a command line argument.");
else
{
int port = Integer.parseInt(args[0]);
final int MAX_LEN = 10;
try
{
DatagramSocket mySocket = new DatagramSocket(port);
byte[ ] buffer = new byte[MAX_LEN];
DatagramPacket datagram = new DatagramPacket(buffer, MAX_LEN);
mySocket.receive(datagram);
String s = new String(buffer);
char c[] = new char[10];
c =s.toCharArray();
char temp[]= new char[9];
temp[3]=c[0];
temp[6]=c[1];
temp[0]=c[2];
temp[7]=c[3];
temp[1]=c[4];
temp[2]=c[5];
temp[4]=c[6];
temp[5]=c[7];
String s1=new String();
s1=String.valueOf(c);
System.out.println("Ciphertext is input of Receiver P-box=:" + s1);
String s2=new String();
s2=String.valueOf(temp);
System.out.println("Plaintext is output of Receiver P-box=:" + s2);
mySocket.close( );
} // end try
catch (Exception ex)
{
ex.printStackTrace( );
}
} // end else
} // end main
} // end class
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to create frame and add component.
//frame is a container that contain all component
import java.awt.*;
import javax.swing.*;
public class framedemo
{
private Frame f;
private Panel p;
private Button b,b1;
private TextField t1;
private TextArea ta;
private Label l;
private Checkbox cb;
private JRadioButton rb;
private JComboBox cbx;
private List li;
private JTree jt;
public framedemo(String title)
{
f=new Frame("hello out there:");
p=new Panel();
l=new Label("Enter Value");
b=new Button("Click");
b1=new Button("Enter");
t1=new TextField();
ta= new TextArea("Comments");
cb=new Checkbox("check");
rb=new JRadioButton("Get option");
cbx=new JComboBox();
li=new List();
jt=new JTree();
}
public void add()
{
f.setSize(170,170);
f.setBackground(Color.blue);
f.setVisible(true);
f.setLayout(new FlowLayout());
p.setBackground(Color.red);
f.add(p);
t1.setSize(100,100);
f.add(l);
f.add(t1);
f.add(cb);
f.add(rb);
f.add(cbx);
f.add(li);
f.add(ta);
f.add(jt);
f.add(b);
f.add(b1);
f.pack();
}
public static void main(String arg[])
{
framedemo fx=new framedemo("jignesh");
fx.add();
}
}
* W.A.P to create menubar in frame.
import java.awt.*;
public class Main
{
private Frame f;
private MenuBar mb;
public Main()
{
f=new Frame("menu");
mb=new MenuBar();
Menu m1=new Menu("File");
Menu m2=new Menu("Edit");
mb.add(m1);
mb.add(m2);
MenuItem mi1=new MenuItem("New");
MenuItem mi2=new MenuItem("Open");
MenuItem mi3=new MenuItem("Save");
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
m1.addSeparator();
m1.add(mi3);
MenuItem mi4=new MenuItem("Cut");
MenuItem mi5=new MenuItem("Copy");
MenuItem mi6=new MenuItem("Pest");
CheckboxMenuItem mi7=new CheckboxMenuItem("Persistent");
m2.add(mi4);
m2.addSeparator();
m2.add(mi5);
m2.addSeparator();
m2.add(mi6);
m2.addSeparator();
m2.add(mi7);
f.setMenuBar(mb);
f.setVisible(true);
}
public static void main(String arg[])
{
Main m=new Main();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use button event.
import java.awt.event.*;
import java.awt.*;
public class testbutton implements ActionListener
{
Frame f;
Button b;
String msg;
public testbutton()
{
f=new Frame("testbutton");
b=new Button("button");
}
public void launchframe()
{
f.add(b,BorderLayout.CENTER);
b.addActionListener(this);
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String ac=ae.getActionCommand();
System.out.println("ac"+ac);
}
public static void main(String args[])
{
testbutton tb=new testbutton();
tb.launchframe();
}
}
* W.A.P to use of mouse events.
import java.awt.*;
import java.awt.event.*;
public class mousedemo implements MouseListener,MouseMotionListener
{
Frame f;
Button b;
public mousedemo()
{
f=new Frame("testmouse");
b=new Button("click");
}
public void launchframe()
{
f.add(b);
f.setLayout(new FlowLayout());
f.addMouseListener(this);
f.addMouseMotionListener(this);
f.setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
f.setBackground(Color.red);
}
public void mousePressed(MouseEvent e)
{
f.setBackground(Color.blue);
}
public void mouseReleased(MouseEvent e)
{
f.setBackground(Color.black);
}
public void mouseEntered(MouseEvent e)
{
f.setBackground(Color.pink);
}
public void mouseExited(MouseEvent e)
{
f.setBackground(Color.white);
}
public void mouseDragged(MouseEvent e)
{
f.setBackground(Color.yellow);
}
public void mouseMoved(MouseEvent e)
{
f.setBackground(Color.green);
}
public static void main(String args[])
{
mousedemo mb=new mousedemo();
mb.launchframe();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use jdbc and insert record in database.
import java.sql.*;
import java.io.*;
public class insert_stud
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:db1");
Statement st=con.createStatement();
String s1 = "insert into stud (srno,sname) values (1,'jigs')";
st.execute(s1);
con.setAutoCommit(true);
System.out.println("success");
con.close();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e);
}
}
}
Note: How to connect database.
start> control panel> Administrative tools> Data sources(ODBC)> System DSN> Add> select database driver.
Microsoft Access Driver(*.mdb. *.accdb)
then click finish.
new window is open then click select button, give proper database path, clock ok, give data source name same as database name.
class hello
{
public static void main(String args[])
{
System.out.println("Hello Wrold");
}
}
/*
Save file with class name.java E.g hello.java
goto >start > run >cmd.
select source PATH E.g D:\jigs\java>
Compile This program:- javac hello.java
Rum Program:-java hello
*/
* W.A.P to get the value from user and perform arithmetic operation.
import java.io.*;
class userget
{
public static void main(String arg[])
{
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.print("Enter Any num for A:=");
int a=Integer.parseInt(br.readLine());
System.out.print("Enter Any num for B:=");
int b=Integer.parseInt(br.readLine());
System.out.println("Addition:="+(a+b));
System.out.println("Substraction:="+(a-b));
System.out.println("Miltiplication:="+(a*b));
System.out.println("Division:="+(a/b));
}
catch(Exception e)
{
System.out.println(e);
}
}
}
* W.A.P to get value from Commandline.
class commandline
{
public static void main(String args[])
{
System.out.println(arg[0]);
System.out.println(arg[1]);
}
}
* W.A.P to represent static variable.
class hello
{
static int a=10;
static public void disp()
{
final int a=5;
System.out.println(a);
}
}
class static_var
{
public static void main(String args[])
{
hello h=new hello();
h.disp(); //or
hello.disp();
}
}
* W.A.P to use of boolean datatype.class boolean1
{
public static void main(String args[])
{
boolean b1="jigs"=="jigs";
boolean b2=new String("jigs")=="jigs";
boolean b3=new String("jigs")==new String("jigs");
System.out.println(b1 && b2 || b3);
}
}
* W.A.P to calculate factorial of a number.
class facto
{
public static void main(String args[])
{
int i,a,fact=1;
int n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.print(fact);
}
}
* W.A.P to enter two numbers and print the one number raised power of another number.
class power
{
public static void main(String args[])
{
int i,ans=1;
int n=Integer.parseInt(arg[0]);
int m=Integer.parseInt(arg[1]);
for(i=1;i<=m;i++)
{
ans=ans*n;
}
System.out.print(ans);
}
}
* W.A.P to swap two values.
class swap
{
public static void main(String args[])
{
int c;
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
c=a;
a=b;
b=c;
System.out.println(a);
System.out.println(b);
}
}
* W.A.P to check the number is prime number or not.
class prime
{
public static void main(String arg[])
{
int a,i,f=0;
a=Integer.parseInt(arg[0]);
for(i=2;i<a;i++)
{
if(a%i==0)
{
System.out.println("This Number isnot Prime");
f=1;
break;
}
}
if(f==0)
{
System.out.println("This Number is Prime");
}
}
}
* W.A.P to check the number is armstrong or not.
153 is armstrong number.
class armstrong
{
public static void main(String arg[])
{
int a=0,n,b,i,l;
l=Integer.parseInt(arg[0]);
n=l;
while(n!=0)
{
b=n%10;
a=a+(b*b*b);
n=n/10;
}
if(l==a)
{
System.out.println("armstrong");
}
else
{
System.out.println("not armstrong");
}
}
}
* W.A.P to check the number is palindron or not.
12321 is palindrom number.
class palindrom
{
public static void main(String arg[])
{
int a=0,e,b;
int n=Integer.parseInt(arg[0]);
b=n;
while(n!=0)
{
e=n%10;
a=(a*10)+e;
n=n/10;
}
if(a==b)
{
System.out.println("Palindrom Number");
}
else
{
System.out.println("Not Palindrom Number");
}
}
}
* W.A.P to print a number in reverse.
class revnum
{
public static void main(String arg[])
{
int a=0,e;
int n=Integer.parseInt(arg[0]);
while(n!=0)
{
e=n%10;
a=(a*10)+e;
n=n/10;
}
System.out.print(a);
}
}
* W.A.P to print a string in reverse without using reverse function.
import java.util.*;
import java.io.*;
class reversestring
{
public static void main(String args[])
{
String s, rev="";
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.print("Enter Any String:=");
s=br.readLine();
int len = s.length();
for (int i = len - 1 ; i >= 0 ; i-- )
{
rev = rev + s.charAt(i);
}
System.out.println("Reverse of string is "+rev);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print fibonacci series.
class fibonacci
{
public static void main(String arg[])
{
int i,a,b=1,c=0;
for(i=1;i<=10;i++)
{
a=b;
b=c;
c=a+b;
System.out.print(c);
System.out.print(" ");
}
}
}
* W.A.P to calculate this 1/1, 2/2, 3/3, 4/4, 5/5 series.
class serise
{
public static void main(String arg[])
{
float a,b,total=0,sum=0;
for(a=1;a<=5;a++)
{
for(b=1;b<=a;b++)
{
total=a/b;
}
sum=sum+total;
}
System.out.println(sum);
}
}
* W.A.P to calculate 1/1!, 2/2!, 3/3!, 4/4!, 5/5! series.
class serise2
{
public static void main(String arg[])
{
float a,b,total=0,sum=0,ans=1;
for(a=1;a<=5;a++)
{
ans=1;
for(b=a;b>=1;b--)
{
ans=ans*b;
}
total=a/ans;
sum=sum+total;
}
System.out.println(sum);
}
}
* W.A.P to calculate 1-2+3-4+5-6 series.
class serise3
{
public static void main(String arg[])
{
int a,total=0,sub=0;
for(a=1;a<=5;a=a+2)
{
sub=a-(a+1);
total=total+sub;
}
System.out.println(total);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print following pattern.
aaaaaa
bbbbb
cccc
ddd
ee
f
class pattern1
{
public static void main(String arg[])
{
char i,j;
for(i='a';i<='f';i++)
{
for(j=i;j<='f';j++)
{
System.out.print(i);
}
System.out.println("");
}
}
}
* W.A.P to print following pattern.
1
12
123
1234
12345
class pattern2
{
public static void main(String arg[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println("");
}
}
}
* W.A.P to print following pattern.
abcdef
bcdef
cdef
def
ef
f
class pattern4
{
public static void main(String arg[])
{
char i,j;
for(i='a';i<='f';i++)
{
for(j=i;j<='f';j++)
{
System.out.print(j);
}
System.out.println("");
}
}
}
* W.A.P to print following pattern.
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
class pattern6
{
public static void main(String arg[])
{
int i,j,l,k;
for(i=1;i<=5;i++)
{
for(k=5;k>=i;k--)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
System.out.print("*");
System.out.print(" ");
}
System.out.println(" ");
}
for(i=1;i<=5;i++)
{
for(k=1;k<=i;k++)
{
System.out.print(" ");
}
for(j=i;j<=5;j++)
{
System.out.print("*");
System.out.print(" ");
}
System.out.println(" ");
}
}
}
* W.A.P to print following pattern.
*
**
* *
* *
* *
* *
* *
* *
* *
**********
class pattern9
{
public static void main(String arg[])
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
{
if(j==1 || j==i || i==10)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
* W.A.P to print following pattern.
999999999
8 8
7 7
6 6
5 5
4 4
3 3
2 2
1
class pattern11
{
public static void main(String arg[])
{
int i,j;
for(i=9;i>=0;i--)
{
for(j=9;j>=i;j--)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
if(i==9 || j==i || j==1)
{
System.out.print(i);
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
* W.A.P to print following pattern.
* *
* *
* *
* *
**
**
* *
* *
* *
* *
class pattern12
{
public static void main(String arg[])
{
int i,j;
for(i=0;i<=9;i++)
{
for(j=0;j<=9;j++)
{
if(i==j || (j+i)==9)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use of this keyword.
class hello
{
String name;
int age;
hello(String name,int age)
{
this.name=name;
this.age=age;
System.out.println("Name is:="+name);
System.out.println("Age is:="+age);
}
}
public class thiskeyword
{
public static void main(String arg[])
{
hello h=new hello("jigs",23);
}
}
* W.A.P to represent of abstract class.
/*
1, abstract method has no body
2, no object of any abstract class
3, always extends by another class
it's contain atlist one abstract method
*/
abstract class xy
{
int x=5,y=8;
abstract void add();
}
class xydemo extends xy
{
void add()
{
System.out.println("Addition is:="+(x+y));
}
}
class abstract1
{
public static void main(String arg[])
{
xydemo d1=new xydemo();
d1.add();
}
}
* W.A.P to use of scanner class.
import java.util.*;
public class scannerdemo
{
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
String name;
int no;
System.out.print("Enter any Name:=");
name=s.nextLine();
System.out.print("Enter any Number:=");
no=s.nextInt();
System.out.println("Name="+name);
System.out.println("no="+no);
}
}
* W.A.P to use of stack class.
import java.util.*;
class Stackdemo
{
public static void main(String arg[])
{
Stack st=new Stack();
for(int a=0;a<arg.length;a++)
st.push(new Integer(arg[a]));
while(! st.empty())
{
Object obj=st.pop();
System.out.println(obj);
}
}
}
* W.A.P to use of string tokenizer class.
import java.util.*;
class Stringtokenizer
{
public static void main(String arg[])
{
String str="Hi I am Jigs Shekhada";
StringTokenizer st= new StringTokenizer(str," / ");
while(st.hasMoreTokens())
{
String s=st.nextToken();
System.out.println(s);
}
}
}
* W.A.P to represent boxing and autoboxing.
/*
boxing= primitive to non primitive
autoboxing=nonprimitive to primitive
*/
class wrap
{
public static void main(String arg[])
{
int p=7;
Integer a=new Integer(p);//boxing
System.out.println("value of object="+a.intValue());
int x=a.intValue();//unboxing
System.out.println("value of variable="+x);
Integer b=p;//autoboxing
System.out.println("value of object="+b);//both are
System.out.println("value of object="+b.intValue());//same
int y=b;//Autoboxing
System.out.println("Value of object="+y);
}
}
* W.A.P to use of list & set of collection.
/*
1, ordearing List
2, no duplicat value in Set
*/
import java.util.*;
class list_set
{
public static void main(String arg[])
{
List<Integer>t2=new ArrayList<Integer>();
//Set t2=new HashSet();
t2.add(2);
t2.add(1);
t2.add(3);
t2.add(3);
/*
t2.add("first");
t2.add(new Integer(5));
t2.add(new Float(5.5));
*/
System.out.println("liat is:="+t2);
}
}
* W.A.P to print System date.
import java.util.*;
class Datedemo
{
public static void main(String arg[])
{
Date d=new Date();
System.out.println(d);
}
}
* W.A.P to use of claendar class.
import java.util.*;
class Calendardemo
{
public static void main(String arg[])
{
Calendar cl=Calendar.getInstance();
System.out.println("Date:="+cl.get(Calendar.DATE));
System.out.println("Month:="+cl.get(Calendar.MONTH));
System.out.println("Year:="+cl.get(Calendar.YEAR));
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print 1 to 5 using array.
class Array
{
public static void main(String arg[])
{
int arr[]=new int[5];
int i;
for(i=0;i<5;i++)
{
arr[i]=i+1;
}
for(i=0;i<5;i++)
{
System.out.println(arr[i]);
}
}
}
* W.A.P to use of double dim array.
class Array_double
{
public static void main(String arg[])
{
int arr[][]=new int[3][3];
int i,j,a=1;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr[i][j]=a++;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print("\t"+arr[i][j]);
}
System.out.println(" ");
}
}
}
* W.A.P to print character using array.
class char_array
{
public static void main(String arg[])
{
char arr[]={'a','b','c','d','e'};
int i;
for(i=0;i<5;i++)
{
System.out.println(arr[i]);
}
}
}
* W.A.P to use of object of array.
import java.io.*;
class objectarray
{
int rno,m1,m2,m3;
String name;
void getvalue()
{
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.print("Enter Rno:=");
rno=Integer.parseInt(br.readLine());
System.out.print("Enter Name:=");
name=br.readLine();
System.out.print("Enter M1:=");
m1=Integer.parseInt(br.readLine());
System.out.print("Enter M2:=");
m2=Integer.parseInt(br.readLine());
System.out.print("Enter M3:=");
m3=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println(e);
}
}
void display()
{
System.out.println("Rno:="+rno);
System.out.println("Name:="+name);
System.out.println("M1:="+m1);
System.out.println("m2:="+m2);
System.out.println("m3:="+m3);
}
}
class objectofarray
{
public static void main(String arg[])
{
int i;
objectarray objarr[]=new objectarray[5];
for(i=0;i<5;i++)
{
objarr[i]=new objectarray();
}
for(i=0;i<5;i++)
{
objarr[i].getvalue();
}
for(i=0;i<5;i++)
{
objarr[i].display();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print message using method.
class hello
{
public void print()
{
System.out.println("hello method");
}
public void display()
{
System.out.println("display second method");
}
}
class simplemethod
{
public static void main(String arg[])
{
hello h=new hello();
h.print();
h.display();
hello h1=new hello();
h1.print();
}
}
* W.A.P to passing arguments to method.
class hello
{
int c;
public void add(int a,int b)
{
c=a+b;
System.out.println("addition is:="+c);
c=a-b;
System.out.println("Substraction is:="+c);
c=a*b;
System.out.println("Multiplication is:="+c);
c=a/b;
System.out.println("division is:="+c);
}
}
public class paramethod
{
public static void main(String arg[])
{
hello h=new hello();
h.add(20,10);
}
}
* W.A.P to method can return value.
public class methodreturn
{
public static void main(String arg[])
{
int x,y,z;
x=Integer.parseInt(arg[0]);
y=Integer.parseInt(arg[1]);
multi m=new multi();
z=m.mul(x,y);
System.out.println("ans:="+z);
}
}
class multi
{
public int mul(int x,int y)
{
return x*y;
}
}
* W.A.P to represent the Recurson.
class recurson
{
int power(int b,int p)
{
if(p==0)
return 1;
else
return b*power(b,p-1);
}
public static void main(String arg[])
{
recurson r=new recurson();
int b,p,m;
b=Integer.parseInt(arg[0]);
p=Integer.parseInt(arg[1]);
m=r.power(b,p);
System.out.println("power is:="+m);
}
}
* W.A.P to represent method overloadding.
/*
the return type and argument must be diffrent
*/
class over
{
int a,c;
float b;
public void display(int a)
{
this.a=a;
System.out.println(a);
}
public float display(float b)
{
this.b=b;
return(this.b);
}
}
class overloading
{
public static void main(String arg[])
{
float x;
over o=new over();
o.display(10);
x=o.display(20.5f);
System.out.println(x);
}
}
* W.A.P to represrnt methodoverriding.
class hello
{
public void print(int a,int b)
{
int c=a+b;
System.out.println("Addition is:="+c);
}
}
class hi extends hello
{
public void print(int a,int b)
{
int c=a-b;
System.out.println("Substraction is:="+c);
}
}
public class methodoverriding
{
public static void main(String arg[])
{
hi h=new hi();
hello he=new hello();
he.print(10,20);
h.print(10,20);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print message using constructor.
class hello
{
hello()//default constructor.
{
System.out.println("default const");
}
hello(int a,int b)// parameter constructor.
{
int c=a+b;
System.out.println("Addition is:="+c);
}
}
public class bothconst
{
public static void main(String arg[])
{
hello d=new hello();
hello h=new hello(20,10);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print data using inheritance.
class emp
{
String name;
int age;
emp(String name,int age)
{
this.name=name;
this.age=age;
System.out.println("Name is:="+name);
System.out.println("Age is:="+age);
}
}
class manager extends emp
{
String dept;
manager(String name,int age,String dept)
{
super(name,age);
this.dept=dept;
System.out.println("dept is :="+dept);
}
}
public class inhe_const
{
public static void main(String arg[])
{
manager m=new manager("jigs",23,"HR");
}
}
*W.A.P to use multiple constructor in inheritance.
class employee
{
String name;
int age;
double salary;
employee(String name,int age,double salary)
{
this.name=name;
this.age=age;
this.salary=salary;
System.out.println(name);
System.out.println(age);
System.out.println(salary);
}
employee(String name,int age)
{
this.name=name;
this.age=age;
System.out.println(name);
System.out.println(age);
}
employee(String name)
{
this.name=name;
System.out.println(name);
}
}
class manager extends employee
{
String dept;
manager(String name,int age,double salary,String dept)
{
super(name,age,salary);
this.dept=dept;
System.out.println(dept);
}
manager(String name,int age,String dept)
{
super(name,age);
this.dept=dept;
System.out.println(dept);
}
manager(String name,String dept)
{
super(name);
this.dept=dept;
System.out.println(dept);
}
}
public class inheritance
{
public static void main(String arg[])
{
manager m=new manager("NIIT",32,132.68,"civil");
manager m1=new manager("c.g.road",15,"it");
manager m2=new manager("vastrapur","computer");
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use of polymorphism.
abstract class Shape
{
abstract void draw();
}
class Circle extends Shape
{
void draw()
{
System.out.println("Circle");
}
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("Rectangle");
}
}
class Square extends Shape
{
void draw()
{
System.out.println("Square");
}
}
class Polymorphism
{
public static void main(String arg[])
{
Shape ref;
ref=new Circle();
ref.draw();
ref=new Rectangle();
ref.draw();
ref=new Square();
ref.draw();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use of interface.
interface A
{
public void display();
}
class B implements A
{
public void display()
{
System.out.println("Hello");
}
}
class hello_inf
{
public static void main(String arg[])
{
B b=new B();
b.display();
}
}
* W.A.P to comparable interface.
import java.util.*;
class student implements Comparable
{
int rno=0;
String fname,lname;
double gpa=0.0;
public student(int rno, String fname, String lname, double gpa)
{
if(rno==0 || fname==null || lname==null || gpa==0.0)
{
throw new IllegalArgumentException();
}
this.rno=rno;
this.fname=fname;
this.lname=lname;
this.gpa=gpa;
}
public int rno()
{
return rno;
}
public String fname()
{
return fname;
}
public String lname()
{
return lname;
}
public double gpa()
{
return gpa;
}
public int compareTo(Object o)
{
int f=rno-((student)o).rno;
if(f==0)
{
return 0;
}
else if(f<0.0)
{
return -1;
}
else
{
return 1;
}
}
}
class comparabletest
{
public static void main(String arg[])
{
TreeSet stuset=new TreeSet();
stuset.add(new student(101,"jigs","shekhada",9.1));
stuset.add(new student(102,"raju","sharma",8.0));
stuset.add(new student(103,"hars","kalola",7.0));
Object[] starray=stuset.toArray();
student s;
for(Object i:starray)
{
s=(student)i;
System.out.printf("\n Rollno:=%d name=%s%s gpa=%2f",s.rno(),s.fname(),s.lname(),s.gpa());
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P print data using package.
// First create folder name with jigs, then save following program inside it, and only compile but not run.
package jigs;
public class pack
{
public void display()
{
System.out.println("hello");
}
}
// go back from jigs folder and create new following program, and run this program.
import jigs.pack;
public class p1
{
public static void main(String arg[])
{
pack p=new pack();
p.display();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to generate exception in try block and print types of exception in catch block.
class Throw
{
public static void main(String arg[])
{
try
{
int a=Integer.parseInt(arg[0]);
int b=Integer.parseInt(arg[1]);
System.out.println("Division is:="+(a/b));
throw new NumberFormatException();
}
catch(ArithmeticException ae)
{
ae.printStackTrace();
}
catch(NumberFormatException nfe)
{
System.out.println("Exception catch");
}
/*catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Insufficient Argument");
}*/
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println("End of Pro");
}
}
}
* W.A.P to use of throws in exception.
class check
{
void division(int a,int b) throws ArithmeticException
{
System.out.println("Division"+(a/b));
}
}
class throwsdemo
{
public static void main(String arg[])
{
try
{
check ck=new check();
ck.division(6,0);
}
catch(ArithmeticException ae)
{
System.out.println("argument cannot be zero");
}
}
}
* W.A.P to represent catch search.
class CatchSearch
{
static String x[];
public static void main(String arg[])
{
x=arg;
try
{
System.out.println("Before A");
a();
System.out.println("After A");
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println("main:finally");
}
}
static void a()
{
try
{
System.out.println("Before B");
b();
System.out.println("After B");
}
catch(ArithmeticException ae)
{
System.out.println("Second Argument");
}
finally
{
System.out.println("A:finally");
}
}
static void b()
{
try
{
System.out.println("Before c");
c();
System.out.println("After c");
}
catch(NumberFormatException nfe)
{
System.out.println("Argument(s)");
}
finally
{
System.out.println("B:finally");
}
}
static void c()
{
try
{
System.out.println("Before D");
d();
System.out.println("After D");
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println("Insufficient Argument");
}
finally
{
System.out.println("C:finally");
}
}
static void d()
{
int a,b;
a=Integer.parseInt(x[0]);
b=Integer.parseInt(x[1]);
System.out.println("Before Division");
System.out.println(a/b);
System.out.println("After Division");
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to print string or word using thread.
class ThreadB implements Runnable
{
public void run()
{
try
{
for(int a=1;a<=10;a++)
{
System.out.println("Threadb");
Thread.sleep(2000);
}
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
}
class ThreadDemo
{
public static void main(String arg[])
{
ThreadB obj=new ThreadB();
Thread t = new Thread(obj);
t.start();
}
}
* W.A.P to join two thread.
class x extends Thread
{
public void run()
{
try
{
for(int a=1;a<10;a++)
{
System.out.println("jignesh");
Thread.sleep(500);
}
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
}
class y extends Thread
{
public void run()
{
try
{
for(int a=1;a<=10;a++)
{
System.out.println("shekhada");
Thread.sleep(500);
}
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
}
class join
{
public static void main(String arg[])
{
x objx=new x();
y objy=new y();
objx.start();
objy.start();
try
{
objx.join();
objy.join();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("main thread");
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P program to write data in file.
import java.io.*;
public class WriteFile
{
public static void main(String args[])
{
File file=new File("F:\\jig.txt");
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(isr);
PrintWriter out=new PrintWriter(new FileWriter(file));
String s;
System.out.println("Enter File.txt");
System.out.println("[Type ctrl-d to stop.]");
while((s=in.readLine()) != null)
{
out.println(s);
}
in.close();
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
* W.A.P to read data from file.
import java.io.*;
public class ReadFile
{
public static void main(String args[])
{
File file=new File(args[0]); //give file name as command line.
try
{
BufferedReader in=new BufferedReader(new FileReader(file));
String s;
try
{
s=in.readLine();
while(s != null)
{
System.out.println("Read:="+s);
s=in.readLine();
}
}
finally
{
in.close();
}
}
catch(FileNotFoundException e)
{
System.out.println("file not found"+file);
}
catch(IOException e2)
{
e2.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use of socket programming.
// save following program socketclient.java
import java.net.*;
import java.io.*;
public class socketclient
{
public static void main(String arg[])
{
try
{
int port=Integer.parseInt(arg[0]);
String ip=System.getProperty("Server");
Socket s1=new Socket(ip,port);
BufferedReader br=new BufferedReader(new InputStreamReader(s1.getInputStream()));
System.out.println(br.readLine());
br.close();
s1.close();
}
catch(ConnectException cnn)
{
System.out.println("could not connect to Server");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
// save following program socketserver.java
import java.net.*;
import java.io.*;
public class socketserver
{
public static void main(String arg[])
{
ServerSocket s=null;
int port=Integer.parseInt(arg[0]);
//register your service on port 5432
try
{
s=new ServerSocket(port);
}
catch(IOException e)
{
e.printStackTrace();
}
// run the listen/accept loop forever
while(true)
{
try
{
//wait here and listen for a connection
Socket s1=s.accept();
System.out.println("Connection is accept:port="+s1.getPort());
//get outputStream associated with socket
OutputStream s1out=s1.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s1out));
//send your string
bw.write("hello net world");
bw.close();
s1.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
Note- How to run.
first compile socketserver.java // javac socketserver.java
run socketserver.java // javac socketserver 43
open new command prompt
first compile socketclient.java // javac socketclient.java
run socketclient.java // javac socketclient 43
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to applet life cycle.
import java.applet.*;
import java.awt.*;
/* <applet code="AppletLifeCycle" width=300 height=200>
</applet>
*/
public class AppletLifeCycle extends Applet
{
String str=" ";
public void init()
{
str+="init;";
}
public void start()
{
str+="start;";
}
public void stop()
{
str+="stop;";
}
public void destroy()
{
System.out.println("destroy");
}
public void paint(Graphics g)
{
g.drawString(str,10,40);
}
}
Note:= how to run.
compile = javac AppletLifeCycle.java
run = appletviewer AppletLifeCycle.java
* W.A.P to print Blue color string.
import java.applet.Applet;
import java.awt.*;
/* <applet code="BlueString" width=500 height=500>
</applet>
*/
public class BlueString extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawString("jignesh",100,50);
}
}
* W.A.P to draw arc in applet.
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
//<applet code="Arc" width=500 height=460> </applet>
public class Arc extends Applet implements Runnable
{
Random r=new Random();
int x=10,y=10,sang1=0,sang2=0,he=500,wi=500;
public void init()
{
Thread t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
try
{
repaint();
Thread.sleep(100);
if(x<wi-100)
x+=5;
if(y<he-100)
y+=5;
if(x>wi-100)
x=wi-100;
if(y>he-100)
y=he-100;
sang1+=10;
sang2+=10;
}
catch(Exception e)
{
}
}
}
public void paint(Graphics g)
{
Dimension d=getSize();
he=d.height;
wi=d.width;
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(x,20,100,100,sang1,90);
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(x,20,100,100,sang1 + 90,90)
// g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(x,20,100,100,sang1 + 180 ,90);
// g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(x,20,100,100,sang1 + 270 ,90);
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(10,y,100,100,sang2,90);
// g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(10,y,100,100,sang2 + 90,90);
// g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(10,y,100,100,sang2 + 180,90);
// g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillArc(10,y,100,100,sang2 + 270,90);
}
}
* W.A.P to make digital clock in applet.
import java.applet.*;
import java.awt.*;
import java.util.Date;
/* <applet code="clockapplet" width=300 height=200>
</applet>
*/
public class clockapplet extends Applet implements Runnable
{
Thread timerThread;
/* Applet Lifestyle Methods */
public void start()
{
timerThread = new Thread(this, "Clock");
timerThread.start();
}
public void stop()
{
if (timerThread == null)
return;
timerThread.stop();
timerThread = null;
}
/* Runnable interface method */
public void run()
{
while (timerThread != null)
{
repaint(); // request a redraw
try
{
timerThread.sleep(1000);
}
catch (InterruptedException e)
{ /* do nothing*/ }
}
}
/* AWT method */
public void paint(Graphics g)
{
Date d = new Date();
g.drawString(d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(), 1, 10);
}
}
* W.A.P to draw Polygon in applet.
import java.applet.*;
import java.awt.*;
/* <applet code="DrawPolygon" width=300 height=300>
</applet>
*/
public class DrawPolygon extends Applet
{
public void paint(Graphics g)
{
int n=5;
int X[]=new int[n];
int Y[]=new int[n];
X[0]=10;
Y[0]=100;
X[1]=60;
Y[1]=10;
X[2]=70;
Y[2]=140;
X[3]=140;
Y[3]=90;
X[4]=190;
Y[4]=10;
g.drawPolygon(X,Y,n);
}
}
* W.A.P to print dimension in applet statusbar.
import java.applet.*;
import java.awt.*;
/* <applet code="showdim" width=300 height=200>
</applet>
*/
public class showdim extends Applet
{
public void paint(Graphics g)
{
Dimension d=getSize();
showStatus("width="+d.width + ";height="+d.height);
}
}
* W.A.P to draw ovel with color.
import java.applet.*;
import java.awt.*;
/*
<applet code="ovel" width=500 height=500>
</applet>
*/
public class ovel extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(120,120,220,220);
}
}
* W.A.P to draw circle in applet, when applet is resize then circle is in middel of applet.
import java.applet.*;
import java.awt.*;
/* <applet code="Circle" width=300 height=300>
</applet>
*/
public class Circle extends Applet
{
public void paint(Graphics g)
{
Dimension d = getSize();
int xc = d.width/2;
int yc = d.height/2;
int radius= (int) ((d.width < d.height) ?
0.4 * d.width:
0.4 * d.height);
g.drawOval(xc-radius,yc-radius,2 * radius,2 * radius);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use of cryptography using Ceasarcihper algorithm.
// save following program subsender.java
import java.net.*;
import java.io.*;
public class subsender
{
public static void main(String[] args)
{
try
{
InetAddress receiverHost = InetAddress.getByName(args[0]);
int receiverPort = Integer.parseInt(args[1]);
int fd,len1=0,i =0,temp =0;
char msg[] = new char[10];
String s = new String(args[2]);
char c[] = new char[10];
c =s.toCharArray();
len1=s.length();
for(i = 0 ; i < len1; i++)
{
temp=(int)(c[i])+6;
msg[i]= (char)(temp) ;
System.out.println("Encrypted message is: " + msg[i]);
temp=0;
}
String s1=new String();
s1=String.valueOf(msg);
System.out.println("Encrypted message is " + s1);
byte[ ] buffer = s1.getBytes( );
// instantiates a datagram socket for sending the data
DatagramSocket mySocket = new DatagramSocket();
DatagramPacket datagram = new DatagramPacket(buffer, buffer.length,receiverHost,receiverPort);
mySocket.send(datagram);
mySocket.close( );
}
catch (Exception ex)
{
ex.printStackTrace( );
}
}
}
// save following program subreciver.java
import java.net.*;
import java.io.*;
public class subreceiver
{
public static void main(String[] args)
{
if (args.length != 1)
System.out.println("This program requires one command line argument.");
else
{
int port = Integer.parseInt(args[0]);
final int MAX_LEN = 10;
int temp=0;
try
{
DatagramSocket mySocket = new DatagramSocket(port);
byte[ ] buffer = new byte[MAX_LEN];
DatagramPacket datagram =new DatagramPacket(buffer, MAX_LEN);
mySocket.receive(datagram);
String s = new String(buffer);
int len1,i;
s=s.trim();
len1=s.length();
char msg1[] = new char[10];
char msg[]=new char[10];
char asc[]=new char[10];
msg1 =s.toCharArray();
for(i=0;i<len1;i++)
{
asc[i]=msg1[i];
}
String s2=new String();
s2=String.valueOf(asc);
System.out.println("Encrypted message is:" + s2);
for(i=0;i<len1;i++)
{
temp=(int)(asc[i])-6;
msg[i]= (char)(temp) ;
temp=0;
}
String s1=new String();
s1=String.valueOf(msg);
System.out.println("Decreypted (Original) message is: " + s1);
mySocket.close( );
}
catch (Exception ex)
{
ex.printStackTrace( );
}
}
}
}
Note := how to run.
compile = javac subreciver.java
run = java subreciver 30
open new command prompt
compile= javac subsender.java
run= java subsender localhost 30
* W.A.P to use cryptography using onetimepad algorithm.
import java.net.*;
import java.io.*;
public class OneTimePadSender
{
public static void main(String[] args)
{
try
{
InetAddress receiverHost = InetAddress.getByName(args[0]);
int receiverPort = Integer.parseInt(args[1]);
String s = new String(args[2]);
int i;
String key="abcdefghijklmnopqrstuvwxyz";
String result="";
for(i=0;i<s.length();i++)
result+=(char)(s.charAt(i)^key.charAt(i));
System.out.println(result);
byte[ ] buffer = result.getBytes( );
DatagramSocket mySocket = new DatagramSocket();
DatagramPacket datagram = new DatagramPacket(buffer,buffer.length,receiverHost, receiverPort);
mySocket.send(datagram);
mySocket.close( );
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class OneTimePadReceiver
{
public static void main(String[] args)
{
try
{
int port = Integer.parseInt(args[0]);
final int MAX_LEN = 10;
int i;
DatagramSocket mySocket = new DatagramSocket(port);
byte[ ] buffer = new byte[MAX_LEN];
DatagramPacket datagram = new DatagramPacket(buffer,MAX_LEN);
mySocket.receive(datagram);
String s = new String(buffer);
String key="abcdefghijklmnopqrstuvwxyz";
String result="";
for(i=0;i<s.length();i++)
result=result+(char)(s.charAt(i)^key.charAt(i));
System.out.println(result);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
* W.A.P to use cryptography using p-box algorithm.
import java.net.*;
import java.io.*;
// Three command line arguments are expected, in order:
// <domain name or IP address of the receiver>
// <port number of the receiver's socket>
// <message string to send>
public class pboxsender
{
public static void main(String[] args)
{
if (args.length != 3)
System.out.println("This program requires three command line arguments");
else
{
try
{
InetAddress receiverHost = InetAddress.getByName(args[0]);
int receiverPort = Integer.parseInt(args[1]);
String s = new String(args[2]);
char c[] = new char[10];
c =s.toCharArray();
DatagramSocket mySocket = new DatagramSocket();
char temp[]= new char[9];
temp[0]=c[3];
temp[1]=c[6];
temp[2]=c[0];
temp[3]=c[7];
temp[4]=c[1];
temp[5]=c[2];
temp[6]=c[4];
temp[7]=c[5];
String s1=new String();
s1=String.valueOf(temp);
System.out.println("Plaintext is output of Sender P-box =: " + s1);
byte[ ] buffer = s1.getBytes( );
DatagramPacket datagram = new DatagramPacket(buffer, buffer.length,receiverHost, receiverPort);
mySocket.send(datagram);
mySocket.close( );
} // end try
catch (Exception ex)
{
ex.printStackTrace( );
}
} // end else
} // end main
} // end class
import java.net.*;
import java.io.*;
// This program requires one command line argument: port number of the receiver's socket.
//The receiver port number and the sender port number should be same.
public class pboxreceiver
{
public static void main(String[] args)
{
if (args.length != 1)
System.out.println("This program requires a command line argument.");
else
{
int port = Integer.parseInt(args[0]);
final int MAX_LEN = 10;
try
{
DatagramSocket mySocket = new DatagramSocket(port);
byte[ ] buffer = new byte[MAX_LEN];
DatagramPacket datagram = new DatagramPacket(buffer, MAX_LEN);
mySocket.receive(datagram);
String s = new String(buffer);
char c[] = new char[10];
c =s.toCharArray();
char temp[]= new char[9];
temp[3]=c[0];
temp[6]=c[1];
temp[0]=c[2];
temp[7]=c[3];
temp[1]=c[4];
temp[2]=c[5];
temp[4]=c[6];
temp[5]=c[7];
String s1=new String();
s1=String.valueOf(c);
System.out.println("Ciphertext is input of Receiver P-box=:" + s1);
String s2=new String();
s2=String.valueOf(temp);
System.out.println("Plaintext is output of Receiver P-box=:" + s2);
mySocket.close( );
} // end try
catch (Exception ex)
{
ex.printStackTrace( );
}
} // end else
} // end main
} // end class
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to create frame and add component.
//frame is a container that contain all component
import java.awt.*;
import javax.swing.*;
public class framedemo
{
private Frame f;
private Panel p;
private Button b,b1;
private TextField t1;
private TextArea ta;
private Label l;
private Checkbox cb;
private JRadioButton rb;
private JComboBox cbx;
private List li;
private JTree jt;
public framedemo(String title)
{
f=new Frame("hello out there:");
p=new Panel();
l=new Label("Enter Value");
b=new Button("Click");
b1=new Button("Enter");
t1=new TextField();
ta= new TextArea("Comments");
cb=new Checkbox("check");
rb=new JRadioButton("Get option");
cbx=new JComboBox();
li=new List();
jt=new JTree();
}
public void add()
{
f.setSize(170,170);
f.setBackground(Color.blue);
f.setVisible(true);
f.setLayout(new FlowLayout());
p.setBackground(Color.red);
f.add(p);
t1.setSize(100,100);
f.add(l);
f.add(t1);
f.add(cb);
f.add(rb);
f.add(cbx);
f.add(li);
f.add(ta);
f.add(jt);
f.add(b);
f.add(b1);
f.pack();
}
public static void main(String arg[])
{
framedemo fx=new framedemo("jignesh");
fx.add();
}
}
* W.A.P to create menubar in frame.
import java.awt.*;
public class Main
{
private Frame f;
private MenuBar mb;
public Main()
{
f=new Frame("menu");
mb=new MenuBar();
Menu m1=new Menu("File");
Menu m2=new Menu("Edit");
mb.add(m1);
mb.add(m2);
MenuItem mi1=new MenuItem("New");
MenuItem mi2=new MenuItem("Open");
MenuItem mi3=new MenuItem("Save");
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
m1.addSeparator();
m1.add(mi3);
MenuItem mi4=new MenuItem("Cut");
MenuItem mi5=new MenuItem("Copy");
MenuItem mi6=new MenuItem("Pest");
CheckboxMenuItem mi7=new CheckboxMenuItem("Persistent");
m2.add(mi4);
m2.addSeparator();
m2.add(mi5);
m2.addSeparator();
m2.add(mi6);
m2.addSeparator();
m2.add(mi7);
f.setMenuBar(mb);
f.setVisible(true);
}
public static void main(String arg[])
{
Main m=new Main();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use button event.
import java.awt.event.*;
import java.awt.*;
public class testbutton implements ActionListener
{
Frame f;
Button b;
String msg;
public testbutton()
{
f=new Frame("testbutton");
b=new Button("button");
}
public void launchframe()
{
f.add(b,BorderLayout.CENTER);
b.addActionListener(this);
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String ac=ae.getActionCommand();
System.out.println("ac"+ac);
}
public static void main(String args[])
{
testbutton tb=new testbutton();
tb.launchframe();
}
}
* W.A.P to use of mouse events.
import java.awt.*;
import java.awt.event.*;
public class mousedemo implements MouseListener,MouseMotionListener
{
Frame f;
Button b;
public mousedemo()
{
f=new Frame("testmouse");
b=new Button("click");
}
public void launchframe()
{
f.add(b);
f.setLayout(new FlowLayout());
f.addMouseListener(this);
f.addMouseMotionListener(this);
f.setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
f.setBackground(Color.red);
}
public void mousePressed(MouseEvent e)
{
f.setBackground(Color.blue);
}
public void mouseReleased(MouseEvent e)
{
f.setBackground(Color.black);
}
public void mouseEntered(MouseEvent e)
{
f.setBackground(Color.pink);
}
public void mouseExited(MouseEvent e)
{
f.setBackground(Color.white);
}
public void mouseDragged(MouseEvent e)
{
f.setBackground(Color.yellow);
}
public void mouseMoved(MouseEvent e)
{
f.setBackground(Color.green);
}
public static void main(String args[])
{
mousedemo mb=new mousedemo();
mb.launchframe();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
* W.A.P to use jdbc and insert record in database.
import java.sql.*;
import java.io.*;
public class insert_stud
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:db1");
Statement st=con.createStatement();
String s1 = "insert into stud (srno,sname) values (1,'jigs')";
st.execute(s1);
con.setAutoCommit(true);
System.out.println("success");
con.close();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e);
}
}
}
Note: How to connect database.
start> control panel> Administrative tools> Data sources(ODBC)> System DSN> Add> select database driver.
Microsoft Access Driver(*.mdb. *.accdb)
then click finish.
new window is open then click select button, give proper database path, clock ok, give data source name same as database name.
No comments:
Post a Comment