Saturday, 30 June 2012

JAVA AWT COMPLETE EXAMPLE 1

JAVA AWT COMPLETE EXAMPLE 1




import java.awt.*;
class buttondemo

{
buttondemo()
{
Frame f=new Frame();
f.setLayout(null);
f.setSize(1000,1000);
f.setVisible(true);
Button b=new Button();
b.setBounds(10,100,40,30);
b.setVisible(true);
f.add(b);

Button b1=new Button();
b1.setBounds(80,100,40,30);
b1.setVisible(true);
f.add(b1);

TextField t=new TextField();
t.setVisible(true);
t.setBounds(200,100,40,40);
f.add(t);

Label l1=new Label("demo");
l1.setBounds(100,150,50,25);
l1.setVisible(true);
Label l2=new Label("Label");
l2.setBounds(150,150,50,25);
l2.setVisible(true);
f.add(l1);
f.add(l2);

Checkbox c=new Checkbox("mac os");
Checkbox c1=new Checkbox("mac os1");
Checkbox c2=new Checkbox("mac os2");
c.setBounds(60,105,104,105);
c1.setBounds(261,114,214,115);
c2.setBounds(321,214,114,105);
f.setVisible(true);
c.setVisible(true);c1.setVisible(true);c2.setVisible(true);
f.add(c);f.add(c1);f.add(c2);

Choice c6=new Choice();
c6.setBounds(523,210,235,225);
c6.add("xp");
c6.add("mac");
f.add(c6);


}
public static void main(String args[])
{
new buttondemo();
}
}





Download Android Codes From SkyBlueAndroid

Sunday, 24 June 2012

nested try in exception handling

nested try in exception handling


class nestedtrydemo
{
public static void main(String args[])
{
try
{
int a=1;
int b=22/a;
System.out.println("a"+a);
try
{

int c[]={1};
c[42]=99;
}


catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array exception");
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array exception");
}

}
}


Saturday, 23 June 2012

multiple catch in exception handling in java -example program

multiple catch in exception handling in java -example program



class multicatch
{
public static void main(String args[])
{
try{
int a=0;
System.out.println("a="+a);
int b=42/a;
int c[]={1};
c[42]=99;
}
catch(ArithmeticException e)
{
System.out.println("division by zero"+e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array exception");
}
}
}




Arithmetic Exception in java example

Arithmetic Exception in java example



import java.io.*;
class exceptiondemo
{
public static void main(String args[]) throws IOException
{

int d,a;
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

try{
a=Integer.parseInt(br.readLine());
d=Integer.parseInt(br.readLine());

int c=d/0;
System.out.println("this wiil not be printed");
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println("division by zero");
}System.out.println("after catch");
}
}

user defined package in java a simple example

user defined package in java a simple example




 packagedemo.java




package p1;
public class packagedemo
{
public void show1()
{
int a=10;
int b=20;
int c=a+b;
System.out.println("Method show1 of Class packagedemo"+c);
}

}


steps


1  compile  the program

2 javac packagedemo.java

3 create the package  by using the next step

4 javac -d . packagedemo.java

5  import to the next calss

demopack.java



import p1.packagedemo; 
class demopack
{
public   void show2(){
System.out.println("Method m3 of Class c3");
}
public static void main(String args[]){
packagedemo obj1 = new packagedemo();
obj1.show1();

}
}

Thursday, 21 June 2012

interface in java with example

interface in java with example


interface area
{
void area();
}
class triangle implements area
{
int length=10,breadth=10,area;
public void area()
{
area=length*breadth;
System.out.println("Area is"+area);
}
}
class square implements area
{
int a=20,sqarea;
public void area()
{
sqarea=a*a;
System.out.println("Area of square is"+sqarea);
}
}
class interfac
{
public static void main(String args[])
{
triangle tri=new triangle();
tri.area();
square sq=new square();
sq.area();
}
}

abstract class example

abstract class example



abstract class A
{
abstract void show();
void show2()
{
System.out.println("show 2");
}
}
class B extends A
{
void show()
{
System.out.println("show 1");
}
}
class abstractclass
{
public static void main(String args[])
{
B ob=new B();
ob.show();
ob.show2();
}
}

method overriding  in java example 2


class Animal
{
public void move()
{
System.out.println("animals ");
}
}
class dog extends Animal
{
public void move()
{
System.out.println("dog");
}
}
public class overridetest
{
public static void main(String args[])
{
Animal a=new Animal();
dog d=new dog();
a.move();
d.move();
}
}

method overriding in java example 2

method overriding in java example 2



class A { 
int i, j; 
A(int a, int b) { 
i = a; 
j = b; 
} 
// display i and j 
void show() { 
System.out.println("i and j: " + i + " " + j); 
} 
}

class B extends A { 
int k; 
B(int a, int b, int c) { 
super(a, b); 
k = c; 
} 
// display k – this overrides show() in A 
void show() { 
System.out.println("k: " + k); 
} 
}

class Override1 { 
public static void main(String args[]) { 
B subOb = new B(1, 2, 3); 
subOb.show();
A ob1=new  A(10,20);
ob1.show();
} 
}

method overriding in java

method overriding in java




class area
{
int length,breadth,area;
int area(int length,int breadth)
{
area=length*breadth;
return area;
}
void show()
{
System.out.println("Area is "+area);
}
//final void show()
//{
//System.out.println("Area is "+area);
//}
}
class volume extends area
{
int height,volume;
int volume1(int length,int breadth,int height)
{
super.show();
volume=length*breadth*height;
return volume;
}
void show()
{
System.out.println("Volume is "+volume);
}
}
class override
{
public static void main(String args[])
{
volume ob=new volume();
ob.area(10,10);
ob.volume1(10,10,10);
ob.show();
}
}

use of static keyword in java example


use of static keyword in java example


import java.io.*;
class overloaddemo
{
void test()
{
System.out.println("No arguments");
}
void test(int a)
{
System.out.println("a"+a);
}
void test(int a,int b)throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
int c=a+b;
System.out.println("sum"+c);
}
}
class over
{
public static void main(String args[]) throws IOException
{
int a=0,b=0;
overloaddemo ob=new overloaddemo();
ob.test();
ob.test(10);
ob.test(a,b);
}
static
{
System.out.println("hai");
}
}

method overloading in java example


method overloading in java example




import java.io.*;
class overloaddemo
{
void test()
{
System.out.println("No arguments");
}
void test(int a)
{
System.out.println("a"+a);
}
void test(int a,int b)throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
int c=a+b;
System.out.println("sum"+c);
}
}
class over
{
public static void main(String args[]) throws IOException
{
int a=0,b=0;
overloaddemo ob=new overloaddemo();
ob.test();
ob.test(10);
ob.test(a,b);
}
//static
//{
//System.out.println("hai");
//}
}

constructor in java Example


constructor in java Example 



class box
{
double width;
double height;
double length;

box(double w,double h,double l)
{
width=w;
height=h;
length=l;
}
double volume()
{
return width*height*length;
}
}
class boxdemo
{
public static void main(String args[])
{
box b1=new box(10,20,30);
box b2=new box(3,6,9);
double vol;
vol=b1.volume();

System.out.println(vol);
vol=b2.volume();

System.out.println(vol);
}

}

read values from keyboard


read values from keyboard



java.io.*;
class mul
{
int a;
int b;
int c;
mul()throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
}
int result()
{
int c =a*b;
return c;
}

}


class vol
{
public static void main(String args[]) throws IOException
{
mul ob=new mul();
int d=ob.result();
System.out.println(d);


}
}

Sunday, 17 June 2012

java program for finding palindrome number


java program for finding palindrome number



import java.io.*;
class Palindrome{

      public static void main(String args[]) throws IOException
 {

BufferedReader br =new BufferedReader(new InputStreamReader (System.in));
 System.out.println("eter a number");
          int num = Integer.parseInt(br.readLine());

          int n = num; //used at last time check

          int reverse=0,remainder;

          while(num > 0){

                remainder = num % 10;

                reverse = reverse * 10 + remainder;

                num = num / 10;

           }

          if(reverse == n)

              System.out.println(n+" is a Palindrome Number");

          else

              System.out.println(n+" is not a Palindrome Number");

     }

}

Reverse of a number in java


Reverse of  a number in java



import java.io.*;

class Reverse{

      public static void main(String args[]) throws IOException
 {
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number");

          int num = Integer.parseInt(br.readLine());

          int remainder, result=0;

          while(num>0){

              remainder = num%10;

              result = result * 10 + remainder;

              num = num/10;

         }

         System.out.println("Reverse number is : "+result);

    }

}

factorial of number in java


factorial of number in java


import java.io.*;
class fact
{
public static void main(String args[]) throws IOException
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("eter the number");
int a=Integer.parseInt(br.readLine());
int fact=1;
System.out.println("factroial "+a+":");
for(int i=1;i<=a;i++)
{
fact=fact*i;
}
System.out.println(fact);

}
}

Read value using Buffered Reader in java


Read value using Buffered Reader




import java.io.*;
class readerdemo1
{
public static void main(String args[])throws IOException
{
int a,b,c;
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

System.out.println("ente the numbers");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
c=a+b;
System.out.println(c);
}
}


Random number in java


find 5 random numbers with in 100


class randomnumber
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
System.out.println((int)(Math.random()*100));
}
}
}



output

12
13
67
88
34

Saturday, 2 June 2012

super keyword with example

super keyword


the main use of the the super keyword is to enable the subclass to refer its super class

the main two use of this super keyword is given below


  • used to access the super class constructors.
  • used to access the methode variable


Example program


  public class a
{
public void show()
{
system.out.println("class a");
}
}
public class b
{
public void show()
{
super.show();
system.out.println("class b");
}
class c
{
public static void main(string args[])
{
b ob1=new b();
ob1.show();
}
}


output


class a
class b



Example for user defined package

Example for  user defined package 



package p1;
class c1
{
public void m1(){
System.out.println("Method m1 of Class c1");
}
public static void main(String args[]){
c1 obj = new c1();
obj.m1();
}
}
             

use this package
  before compile the package to create the package



save the program as sample.java

the in cmt box compile the program


javac - d . sample.java



then our package is created


Use this package to our program




import p1.*;
class c3
{
public   void m3()
{
System.out.println("Method m3 of Class c3");
}
public static void main(String args[])
{
c1 obj1 = new c1();
obj1.m1();
}
}





packages

packages


packages are a collection of related class.there are mainly two types of packages are there,


  •  user defined packages
  •  pre defined packages


the main advantages of the package is to organize your classes in to a ordered manner,and easy to use this.

syntax for creating a user defined package


  package <package name>;
 

Friday, 1 June 2012

Java Constructor program

Java Constructor program


  class rectangle
{
double length,height;
rectangle()
{
length=10;
height=10;
}
double volume()
{
double volume=length*height;
System.out.println("Volume is"+volume);
return volume;
}
}
class constructor
{
public static void main(String args[])
{
rectangle rec=new rectangle();
rec.volume();
}
}

Student details program in java


Student details program in java


class name
{
String name="anju";
int age=20;
}
class address extends name
{
String address="rose";
String city="tvm";
void show()
{
System.out.println("NAME="+name+"\nAGE="+age+"\nADDRESS="+address+"\nCITY="+city);
}
}
class number extends name
{
int phoneno=1234;
int mobno=9876;
void display()
{
System.out.println("NAME="+name+"\nAGE="+age+"\nPHONENO="+phoneno+"\nMOBILENO="+mobno);
}
}
class student
{
public static void main(String args[])
{
address add=new address();
add.show();
number num=new number();
num.display();
}
}

Multilevel inheritance Example 2


Multilevel inheritance Example 2


class name
{
int rollno,age;
int id(int rollno,int age)
{
//return rollno;

}
}
class mark  extends name
{
int m1,m2,m3;
int marks(int m1,int m2,int m3)
{
return m1;

}
}
 class total extends mark
 {
int total;
void calc()
{
total=m1+m2+m3;
}
void show()
{
System.out.println("ROLLNO="+rollno+" AGE="+age+" MARK1="+m1+" MARK2="+m2+" MARK3="+m3+" TOTAL="+total);
}
}
class paramulti
{
public static void main(String args[])
{
total ob=new total();
ob.id(001,20);
ob.marks(30,30,40);
ob.calc();
ob.show();
}
}

Multilevel Inheritance in Java example 1


Multilevel Inheritance in Java example 1

class name
{
String name="anu";
int age=20;
}
class mark extends name
{
int m1=30,m2=30,m3=30;
}
class student extends mark
{
int total;
void calc()
{
total=m1+m2+m3;
}
void show()
{
System.out.println("NAME:" +name+"\nAGE:"+age+"\nMARK1="+m1+"\nMARK2="+m2+"\nMARK3="+m3+"\nTOTAL:"+total);
}
}
class multilevel
{
public static void main(String args[])
{
student ob=new student();
ob.calc();
ob.show();
}
}

Inheritance in Java example 4


Inheritance in Java example 4

class inhr
{
int sum;
int sum(int a,int b)
{
sum=a+b;
return sum;
}

}

class inhr1 extends inhr
{
void display()
{
sum(10,10);
System.out.println(sum);
}
}

class inhrmain
{
public static void main(String arg[])
{
inhr1 obj=new inhr1();
obj.display();
}
}


Inheritance in Java example 3


Inheritance in Java example 3

class box1
{
int length;
box1(int x)
{
length=x;
}
void show()
{
System.out.println("super x:"+length);
}
}
class box2 extends box1
{
int breadth;
box2(int x,int y)
{
super(x);
super.show();
System.out.println("super x:"+length);
breadth=y;
}
}
class box3 extends box2
{
int height;
box3(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return length*breadth*height;
}
}
class conmul
{
public static void main(String args[])
{
box3 ob=new box3(10,10,10);
int volume=ob.volume();
System.out.println("Volume is "+volume);
}
}


Inheritance in Java example 2


Inheritance in Java example 2


class rollno
{
int rollno;
rollno(int x)
{
rollno=x;
System.out.println(rollno);
}
}
class age extends rollno
{
int age;
age(int x,int y)
{
//super(x);
super.rollno();
age=y;
}
void show()
{
System.out.println("ROLLNO="+rollno+"\nAGE="+age);
}
}
class mark extends rollno
{
int mark;
mark(int x,int z)
{
super(x);
mark=z;
}
void display()
{
System.out.println("ROLLNO="+rollno+"\nMARK="+mark);
}
}
class conh
{
public static void main(String args[])
{
age ob1=new age(00,20);
ob1.show();
mark ob2=new mark(01,50);
ob2.display();
}
}