Friday, November 12, 2010

JAVA PROGRAMS

Post by: Shankar, Prabhu, VenugopalaReddy, Ragu, Myilvel, Revathy

Prog 1:
import java.lang.*;
class sam
{
        public static void main(String ar[])
        {
                System.out.println("hai machi");
        }
}
class sample
{
        public static void main(String ar[])
        {
                System.out.println("how are you");
        }
}
Output:
how are you

Prog 2:
import java.lang.*;
class Number
{
        int x;
}
class Sample
{
        public static void main(String args[])
        {
        Number n1=null;
        Number n2=null;
        System.out.println("n1 : " + n1);
        System.out.println("n2 : " + n2);
        n1 = new Number();
        n2 = new Number();
        System.out.println("n1 : " + n1);
        System.out.println("n2 : " + n2);
        System.out.println("n1.x  : " + n1.x);
        System.out.println("n2.x  : " + n2.x);
        n1.x = 10;
        n2.x = 20;
        System.out.println("n1.x  : " + n1.x);
        System.out.println("n2.x  : " + n2.x);
        }
}
Output:
n1 : null
n2 : null
n1 : Number@19821f
n2 : Number@addbf1
n1.x  : 0
n2.x  : 0
n1.x  : 10
n2.x  : 20

Prog 3:
import java.lang.*;
import java.io.*;
class sample1
{
public static void main(String arg[]) throws IOException
{
int a,b,c;
BufferedReader obj=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the value");
a=Integer.parseInt(obj.readLine());
System.out.println("Enter the value");
b=Integer.parseInt(obj.readLine());
c=a+b;
System.out.printf("the value of a%d",a);
System.out.printf("the value of b%d",b);
System.out.printf("the value of c%d",c);
}
}

Output:
Enter the value
15
Enter the value
20
the value of a15the value of b20the value of c35




Prog 4://Defining a class with object or reference variable
import java.lang.*;
class Number
{
        int x;
}
class Sample
{
        public static void main(String args[])
        {
        Number n1=null;
        Number n2=null;
        System.out.println("n1 : " + n1);
        System.out.println("n2 : " + n2);
        n1 = new Number();
        n2 = new Number();
        System.out.println("n1 : " + n1);
        System.out.println("n2 : " + n2);
        System.out.println("n1.x  : " + n1.x);
        System.out.println("n2.x  : " + n2.x);
        n1.x = 10;
        n2.x = 20;
        System.out.println("n1.x  : " + n1.x);
        System.out.println("n2.x  : " + n2.x);
        }
}

Output:
n1 : null
n2 : null
n1 : Number@19821f
n2 : Number@addbf1
n1.x  : 0
n2.x  : 0
n1.x  : 10
n2.x  : 20



Prog 5://defining a method with an object
import java.lang.*;
class demo
{
public static void main(String aa[])
        {
        Alpha A=new Alpha();
        Beta B=new Beta();
        System.out.println(" I am in demo class");
        int a=100;
        System.out.println("the value of a :" + a);
        A.fun1();
        }
}
class Alpha
{
void fun1()
   {
        System.out.println("I am in Fun1 method");
         int b=200;
        System.out.println("the value of b : " +b);
        B.fun2();
   }
}
class Beta
{
void fun2()
   {
        System.out.println("I am in fun2 method");
        int c=300;
        System.out.println("the value of c : " +c);
   }
}

Output:
I am in demo class
the value of a :100
I am in Fun1 method
the value of b : 200
I am in fun2 method
the value of c : 300

Prog 8:
import java.lang.*;
class Alpha
{
        int num;
}
class sample
{
        public static void main(String args[])
        {
        Alpha a1;
        a1 = new Alpha();
        System.out.println("a1 : " + a1);
        a1.num = 123;
        System.out.println("a1.num = : " + a1.num);
        Alpha a2 = null;
        System.out.println("a2 : " + a2);
        a2 = a1;
        System.out.println("a2 : " + a2);
        System.out.println("a2.num = : " + a2.num);
        a2.num = 555;
        System.out.println("a1.num = : " + a1.num);
        System.out.println("a2.num = : " + a2.num);
}
}

Output:
a1 : Alpha@3e25a5
a1.num= : 123
a2 : null
a2 : Alpha@3e25a5
a2.num = : 123
a1.num = : 555
a2.num = : 555






Prog 9:
import java.lang.*;
class point
{
        int x;
        int y;
}
class sample
{
public static void main(String args[])
{
point p1=new point;
point p2=new point;
System.out.println("p1 :" + p1);
System.out.println("p2 :" + p2);
p1.x=5;
p1.y=6;
p2.x=5;
p2.y=6;
System.out.println("p1.x :" + p1.x + "p1.y =" +p1.y);
System.out.println("p2.x :" + p2.x + "p2.y =" +p2.y);
if(p1==p2)
System.out.println("p1 equal to p2.");
else
System.out.println("p1 not equal to p2.");
if(p1.x == p2.x && p1.y == p2.y)
System.out.println("p1 value are equal to p2 value.");
else
System.out.println("p1 value are not equal to p2.");
p1=p2;
if(p1 ==p2)
System.out.println("p1 equal to p2.");
else
System.out.println("p1 not equal to p2.");
}
}



Output:
p1 :point@addbf1
p2 :point@42e816
p1.x :5p1.y =6
p2.x :5p2.y =6
p1 not equal to p2.
p1 value are equal to p2 value.
p1 equal to p2.


Prog 10:// command line arguments
import java.lang.*;
class sample
{
  public static void main(String args[])
  {
      System.out.println(" Command line Arguments");
      System.out.println(" args  :" + args);
      System.out.println(" args[0] :" + args[0]);
      System.out.println(" args[1] :" + args[1]);
   }
}

Output:

Command line Arguments
 args  :[Ljava.lang.String;@3e25a5
 args[0] :myil
 args[1] :56964986

Prog 11:
import java.lang.*;
class sample
{
  public static void main(String args[])
  {
 System.out.println(" Command line Arguments");
        System.out.println(" args  :" +args);
        System.out.println(" args.length  :" + args.length);
  for(int i=0;i<args.length:i++)
{
                System.out.println(" args["+i+"]  :" + args[i]);
}
     }
}

Output:

Command line Arguments
 args  :[Ljava.lang.String;@19821f
 args.length  :2
 args[0]  :ajbatch
 args[1]  :geth


Prog 12:
import java.lang.*;
import java.util.*;
class sample
{
        public static void main(String args[])
         {
Scanner scan = new Scanner(System.in);
System.out.println(" enter the integer");
 int i = scan.nextInt();
System.out.println(" enter the string");
scan.nextLine();
String s= scan.nextLine();
System.out.println(" enter th double");
double d = scan.nextDouble();
System.out.println(" i : " + i);
System.out.println(" s : " + s);
System.out.println(" d : " + d);
}
}


Output:
enter the integer
1
 enter the string
ram
 enter th double
25.00
 i : 1
 s : ram
 d : 25.0

Prog 13:
import java.lang.*;
class sample
{
  public static void main(String args[])
  {
int x[];
x= new int[5];
x[0] = 10;
x[1] = 20;
x[2] = 30;
x[3] = 40;
x[4] = 50;
System.out.println("x.length :" +x.length);
for(int i=0;i<x.length;i++)
System.out.println(x[i] + " ");
System.out.println();
System.out.println("x :" + x[3]);
     }
}

Ouput:

x.length :5
10
20
30
40
50

x :40
Prog 14:
import java.lang.*;
import java.util.*;
class sample
{
  public static void main(String args[])
  {

Scanner scan = new Scanner(System.in);
System.out.println(" enter size");
 int size = scan.nextInt();
int ar1[] = new int[size];
 for(int i=0;i<size;i++)
{
System.out.println("enter x [" + i + "] : ");
ar1[i] = scan.nextInt();
}
System.out.println(" Array value :");
for(int i=0;i<size;i++)
{
System.out.println("ar1[" + i +"] : " + ar1[i] );
}
     }
}
Output:

enter size
5
enter x [0] :
1
enter x [1] :
2
enter x [2] :
3
enter x [3] :
4
enter x [4] :
5
 Array value :
ar1[0] : 1
ar1[1] : 2
ar1[2] : 3
ar1[3] : 4
ar1[4] : 5


Prog 15:
import java.lang.*;
class sample
{
public static void main(String args[])
    {
int x[][];
x = new int [3][4];
x[0][0]=10;
x[0][1]=20;
x[0][2]=30;
x[0][3]=40;

x[1][0]=50;
x[1][1]=60;
x[1][2]=70;
x[1][3]=80;

x[2][0]=90;
x[2][1]=20;
x[2][2]=30;
x[2][3]=40;
System.out.println("Array value");
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
System.out.println(x[i][j] + "  ");
}
System.out.println();
}
    }
}

Output:
Array value
10  
20 
30 
40 

50 
60 
70 
80 

90 
20 
30 
40 


Prog 16:
import java.lang.*;
import java.util.*;
class sample
{
  public static void main(String args[])
  {
        int x[][];
        Scanner scan = new Scanner(System.in);
        System.out.println(" enter rows");
        int row = scan.nextInt();
        Scanner scan1 = new Scanner(System.in);
        System.out.println(" enter col");
        int col = scan1.nextInt();
        x = new int [row][col];
        for(int i=0;i<row;i++)
        {
                for(int j=0;j<col;j++)
                {
                        System.out.print("x[" + i + "][" + j + "]  = ");
                        x[i][j] = scan.nextInt();
                }
        }
        System.out.println("Array value");
        for(int i=0;i<row;i++)
        {
                for(int j=0;j<col;j++)
                {
                        System.out.print(x[i][j] + "  ");
                }
                System.out.println();
        }
     }
}
Output:

enter rows
3
 enter col
3
x[0][0]  = 1
x[0][1]  = 0
x[0][2]  = 0
x[1][0]  = 0
x[1][1]  = 1
x[1][2]  = 0
x[2][0]  = 0
x[2][1]  = 0
x[2][2]  = 1
Array value
1  0  0 
0  1  0 
0   0  1
Prog 17:// Jagged Array

import java.lang.*;
class sample
{
    public static void main(String args[])
    {
            int x[][];
            x= new int[3][];
           
            x[0] = new int[2];
            x[1] = new int[5];
            x[2] = new int[3];

            x[0][0] = 10;
            x[0][1] = 20;

            x[1][0] = 30;
            x[1][1] = 40;
            x[1][2] = 50;
            x[1][3] = 60;
            x[1][4] = 70;

            x[2][0] = 80;
            x[2][1] = 90;
            x[2][2] = 11;
            System.out.println("Array valuse");
            for(int i=0;i<x.length;i++)
            {
                    for(int j=0;j<x[i].length;j++)
                    {
                    System.out.print(x[i][j] + "  ");
                    }
                    System.out.println();
    }
            System.out.println();
            System.out.print("x :" + x);
            System.out.print("x.length :" + x.length);

            System.out.print("x[0] :" + x[0]);
            System.out.print("x[0].length :" + x[0].length);

            System.out.print("x[1] :" + x[1]);
            System.out.print("x[1].length :" + x[1].length);

            System.out.print("x[2] :" + x[2]);
            System.out.print("x[2].length :" + x[2].length);
    }
}
 
Array valuse
10  20 
30  40  50  60  70 
80  90  11 

x :[[I@3e25a5x.length :3x[0] :[I@19821fx[0].length :2x[1] :[I@addbf1x[1].length :5x[2] :[I@42e816x[2].length :3BUILD SUCCESSFUL (total time: 0 seconds)

Prog 18:
import java.lang.*;
import java.util.*;
class Student
{
    int sno;
    String sname;
    Scanner scan = new Scanner(System.in);
    void getStudentDetails()
    {
            System.out.print("enter sno :");
            sno = scan.nextInt();
            scan.nextLine();
            System.out.print("enter sname :");
            sname = scan.nextInt();
    }
    void printStudentnDetails()
    {
            System.out.println("sno : " + "sname  :" + sname);
    }
}
class sample
{
      public static void main(String args[])
     {
    Scanner scanf = new Scanner(System.in);
    System.out.print("enter n :");
    int n = scanf.nextInt();
    Student st[];
    st = new Student[n];
    System.out.println("st : " + st);
    for(int i=0;i<n;i++)
    {
            st[i] = new Student();
            System.out.println("st[i] :"+ st[i]);
            st[i].getStudentDetails();
    }
    System.out.println("Students details");
    for(int i=0;i<n;i++)
    {
            st[i].printStudentnDetails();
    }
   }
}
enter n :2
st : [LStudent;@10b30a7
st[i] :Student@1a758cb
enter sno :1
enter sname :abc
st[i] :Student@1b67f74
enter sno :2
enter sname :def
Students details
sno : 1sname  :abc
sno : 2sname  :def


prog 19:
import java.lang.*;
class Employee
{
        int eno;
        String ename;
        void setEmpDetails(int eno,String ename)
        {
                System.out.println("this :" + this);
                this.eno = eno;
                this.ename = ename;
        }
        void printEmpDetails()
        {
                System.out.println("this :" + this);
                System.out.println("eno :" + eno + "ename :" + ename);
        }
}
class sample
{
        public static void main(String args[])
        {
                Employee e1 = new Employee();
                System.out.println("e1 :" + e1);
                e1.setEmpDetails(123,"shankar");
                e1.printEmpDetails();

                Employee e2 = new Employee();
                System.out.println("e2 :" + e2);
                e2.setEmpDetails(124,"shan");
                e2.printEmpDetails();
        }
}

e1 :Employee@19821f
this :Employee@19821f
this :Employee@19821f
eno :123ename :shankar
e2 :Employee@addbf1
this :Employee@addbf1
this :Employee@addbf1
eno :124ename :shan
Prog 20:
import java.lang.*;
class sample
{
        void method(int x, int y)
        {
                System.out.println("2 int args");
                System.out.println("x :" + x + "y :" + y);
        }
        void method(char x)
        {
                System.out.println("1 char args");
                System.out.println("x :" + x );                    
        }
        void method(double x, double y, double z)
        {
                System.out.println("3 double args");
                System.out.println("x :" + x + "y :" + y + "z :" + z);             
        }
}
class mainclass
{
        public static void main(String args[])
        {
                sample s = new sample();
                s.method(10,20);
                s.method(10.45,20.23,30.21);
                s.method('s');

                s.method('a','b');
                s.method(10,20,30);
                s.method((char)66);
                s.method('a','b','c');
        }
}

2 int args
x :10y :20
3 double args
x :10.45y :20.23z :30.21
1 char args
x :s
2 int args
x :97y :98
3 double args
x :10.0y :20.0z :30.0
1 char args
x :B
3 double args
x :97.0y :98.0z :99.0
Prog 21:
import java.lang.*;

class cons
{
        cons()  // constructor
        {
                System.out.println("I am constructor");
        }
        void fun()
        {
                System.out.println("I am normal");
        }
}
class demo
{
        public static void main(String as[])
        {
                cons c = new cons();
                c.fun();
        }
}

I am constructor
I am normal

prog 22:
import java.lang.*;
class student
{
        int sno;
        String sname;
        student()
        {
                System.out.println("initilized by constructor");
                sno=-1111;
                sname="******";        
        }
        void printstudentdetails()
        {
                System.out.print("sno:"+sno+"sname:"+sname);
        }
}
class sample
{
        public static void main(String args[])
        {
                student st1=new student();
                st1.printstudentdetails();

                student st2=new student();
                st2.printstudentdetails();
        }
}

o/p:

initilized by constructor
sno:-1111sname:******initilized by constructor
sno:-1111sname:******

prog 23:

import java.lang.*;
class student
{
        int sno;
        String sname;
        student(int sno, String sname)
        {
                System.out.println("initilized by arg constructor");
                this.sno=sno;
                this.sname=sname;             
        }
        void printstudentdetails()
        {
                System.out.println("this :" + this);
                System.out.print("sno:"+sno+"sname:"+sname);
        }
}
class sample
{
        public static void main(String args[])
        {
                student st1=new student(123,"shankar");
                st1.printstudentdetails();

                student st2=new student(13,"sha");
                st2.printstudentdetails();
        }
}

o/p:

initilized by arg constructor
this :student@42e816
sno:123sname:shankarinitilized by arg constructor
this :student@9304b1
sno:13sname:sha

prog 24:
// constructor overloading
import java.lang.*;
class student
{
        int sno;
        String sname;
        student()
        {
                System.out.println("initilized by no arg constructor");
                sno=-1111;
                sname="unknown";             
        }
        student(int sno)
        {
                System.out.println("initilized by arg constructor");
                this.sno=sno;
                sname="mohan the great";  
        }
        student(int sno, String sname)
        {
                System.out.println("initilized by arg constructor");
                this.sno=sno;
                this.sname=sname;             
        }
        student(student ss)
        {
                System.out.println("initilized by copy constructor");
                sno=ss.sno;
                sname=ss.sname;
        }
        void printstudentdetails()
        {
                System.out.print("sno:"+sno+"sname:"+sname);
        }

}
class sample
{
        public static void main(String args[])
        {
                student st1=new student();
                st1.printstudentdetails();

                student st2=new student(1233);
                st2.printstudentdetails();

                student st3=new student(1235,"prabhu");
                st3.printstudentdetails();

                student st4=new student(st2);
                st4.printstudentdetails();

        }
}

o/p:

initilized by no arg constructor
sno:-1111sname:unknowninitilized by arg constructor
sno:1233sname:mohan the greatinitilized by arg constructor
sno:1235sname:prabhuinitilized by copy constructor
sno:1233sname:mohan the great

1 comment:

  1. wat a great JAVA developers !!! vazhga CTS !!! lols ... Suganya's fren

    ReplyDelete