Thursday, 8 September 2011

Read a File

import java.io.*;
class file
{
public static void main(String args[])throws IOException
{
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the File-Name u want to read");
String f=in.readLine();
FileInputStream f1=new FileInputStream(f);
in=new DataInputStream(f1);
String strline;
while((strline=in.readLine())!=null)
{
System.out.println(strline);
}
in.close();
}
catch(Exception e)
{
System.out.println("error"+ e.getMessage());
}
}
}

Martix Multiplication

import java.io.*;
class matmul
{
public static void main(String args[])throws IOException
{
int a[][]=new int[5][5];
int b[][]=new int[5][5];
int c[][]=new int[5][5];
int i,j,r1,r2,c1,c2,k;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter row & column of Matrix A:");
r1=Integer.parseInt(in.readLine());
c1=Integer.parseInt(in.readLine());
System.out.println("Enter row & column of Matrix B:");
r2=Integer.parseInt(in.readLine());
c2=Integer.parseInt(in.readLine());
if(c1==r2)
{
System.out.println("enter the elements for matrix a");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
a[i][j]=Integer.parseInt(in.readLine());
}
}
System.out.println("enter the elements for matrix b");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
b[i][j]=Integer.parseInt(in.readLine());
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
System.out.println("matrix a is");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println("");
}
System.out.println("matrix b is");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.println("");
}
System.out.println("matrix c is");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println("");
}
}
else
System.out.println("matrix can't be multiplied");
}
};

Interface

import java.io.*;

class student
{
int rno;
void getno(int n)
{
rno=n;
}
void dispno()
{
System.out.println("Roll No:"+rno);
}
}

class exam extends student
{
int m1,m2;
void getmarks(int mark1,int mark2)
{
m1=mark1;
m2=mark2;
}
void dispmarks()
{
System.out.println("mark 1:"+m1);
System.out.println("mark 2:"+m2);
}
}
interface sports
{
int sprt=50;
void disp_sprt();
}


class result extends exam implements sports
{
int total;
public void disp_sprt()
{
System.out.println("sports mark="+sprt);
}
void display()
{
dispno();
dispmarks();
disp_sprt();
total=m1+m2+sprt;
System.out.println("total="+total);
}
}

class inheri
{
public static void main(String args[])
{
result s=new result();
s.getno(123);
s.getmarks(70,80);
s.display();
}
}

Count Lines-Vowels-Words in a File.

import java.io.*;
import java.util.*;
class count
{
public static void main(String args[])throws IOException
{
int n=0,ct=0,wo=0;
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter the file do u want to read");
String f=d.readLine();
FileInputStream f1=new FileInputStream(f);
DataInputStream in=new DataInputStream(f1);
String strline;
while((strline=in.readLine())!=null)
{
n=n+1;
for(int i=0;i<strline.length();i++)
{
char c=strline.charAt(i);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
{
ct++;
}
}
StringTokenizer st=new StringTokenizer(strline);
while(st.hasMoreTokens())
{
String s=st.nextToken();
wo++;
}
}
System.out.println("File Contains");
System.out.println(n + " Lines");
System.out.println(ct + " Vowels");
System.out.println(wo + " Words"
);
}
}

Armstrong - Prime - Perfect

import java.io.*;
class arm
{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
int no;
int r,n1,i,n,j,flag=0;
System.out.println("Enter a number: ");
no=Integer.parseInt(in.readLine());

System.out.println("");

//armstrong

n1=no;
int s=0;
while(n1>0)
{
r=n1%10;
s=s+(r*r*r);
n1=n1/10;
}
if(s==no)
{
System.out.println(s+" is armstrong");
}

System.out.println("");
System.out.println("");

//prime

n1=no;
for(j=2;j<=n1-1;j++)
{
if(n1%j==0)
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==0)
System.out.println(n1+"prime");

System.out.println("");
System.out.println("");

//perfect

n1=no;
int s1=0;
for(j=1;j<=n1-1;j++)
{
if(n1%j==0)
s1=s1+j;
}
if(s1==n1)
System.out.println(s1+"perfect");

}
};



Database Program

 import java.sql.*;
 import java.io.*;
 class database
 {
        public static void main(String args[])throws IOException
        {
        String str;
        int ch;
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                try
                {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection c=DriverManager.getConnection("jdbc:odbc:master");
                Statement s=c.createStatement();
               
do
{
System.out.println("\n1->create\n2->insert or update or modify or alter\n3->view\n4->exit\n");
ch=Integer.parseInt(br.readLine());

                switch(ch)
                {
                case 1:
                        {
                        str=br.readLine();
                        boolean a=s.execute(str);
                        if(!a)
                        System.out.println("Table created");
                        }
                        break;
                case 2:
                        {
                        str=br.readLine();
                        int r=s.executeUpdate(str);
                        }
                        break;
                case 3:
                        {
                        str=br.readLine();
                        ResultSet rs=s.executeQuery(str);
                        ResultSetMetaData rsmd = rs.getMetaData();
                        int x=rsmd.getColumnCount();
                        for(int j=1;j<=x;j++)
                        {
                        System.out.print(rsmd.getColumnName(j)+"\t\t");
                        }
                        while(rs.next())
                        {
                        System.out.println("");

                                for(int i=1;i<=x;i++)
                                {
                                System.out.print(rs.getObject(i)+"\t");
                                }

                        }
                        }
                        break;
                }
}while(ch!=4);


                }catch(Exception e)
                {
                System.out.println(e);
                }
          }
}

Encryption & Decryption

import java.io.*;
public class encrypt
{
char[] temp=new char[100];
char current,s;
int asc;
String s1;
int i=0;
void getdata(int y)throws IOException
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("enter the text file name to encrypt or decrypt");
s1=in.readLine();
File file=new File(s1);
if(!file.exists())
{
System.out.println(s1+"does not exists");
return;
}
FileInputStream fis=new FileInputStream(file);
while(fis.available()>0)
{
temp[i]=(char)fis.read();
i=i+1;
}
int n=i;
FileOutputStream fos=new FileOutputStream(file,false);
for(int j=0;j<n;j++)
{
current=temp[j];
asc=(int)current;
asc=asc+y;
s=(char)asc;
fos.write(s);
}
fos.close();
}
public static void main(String args[])throws IOException
{
encrypt x=new encrypt();
char[] temp=new char[100];
int asc;
int i=0;
String s1;
DataInputStream in=new DataInputStream(System.in);
try
{
System.out.println("1.encryption");
System.out.println("2.decryption");
System.out.println("\tenter ur choice");
int ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
System.out.println("encrypting the file");
x.getdata(4);
System.out.println("the file is encrypted successfully");
break;
case 2:
System.out.println("decrypting the file");
x.getdata(-4);
System.out.println("the file is decrypted successfully");
break;
}
}
catch(IOException e)
{
System.out.println("file not found");
}
}
}

Exception handling in Java

import java.io.*;
class exc1
{
    public static void main(String arg[])throws IOException
      {
             DataInputStream d=new DataInputStream(System.in);
             int c,d1,d2,i,j,sum=0;
             double ans;
             int a[]=new int[5];
             int b[]=new int[5];
             try
             {
             System.out.print("Enter the Divedent=");
             d1=Integer.parseInt(d.readLine());
             System.out.print("Enter the divisor=");
             d2=Integer.parseInt(d.readLine());
             ans=d1/d2;
             System.out.println("The answer ="+ans);
           
             System.out.println("Enter the number in array one by one");
             for(i=0;i<5;i++)
             {
              a[i]=Integer.parseInt(d.readLine());
             }
             System.out.print("Enter which index number do you want to print=");
             c=Integer.parseInt(d.readLine());
             System.out.println(a[c]);
           
             System.out.println("Enter the Integer value one by one to Sum");
             for(j=0;j<5;j++)
             {
             b[j]=Integer.parseInt(d.readLine());
             }
             }
             catch(ArrayIndexOutOfBoundsException e )
              {
                System.out.println("ArrayIndexOutOfBoundsException");
              }
                        
              catch(ArithmeticException e1)
              {
                        System.out.print("Divided by Zero");
              }
                  
          catch(NumberFormatException e2)
          {
            System.out.println("NumberFormatException");
            for(j=0;j<5;j++)
            sum=sum+b[j];
            System.out.print("The Sum of remaining valid integer ="+sum);
          }
}
}

Wednesday, 7 September 2011

Thread

import java.io.*;

public class T1 extends Thread
{
public void run()
{
for(int i=0; i < 5; i++)
{
System.out.println("Child Thread : " + i);
try
{
Thread.sleep(50);
}
catch(InterruptedException ie)
{
System.out.println("Child thread interrupted! " + ie);
}
}
System.out.println("Child thread finished!");
};

public static void main(String[] args) throws IOException
{
Thread t = new Thread(new T1(), "My Thread");
t.start();
for(int i=0; i < 5; i++)
{
System.out.println("Main thread : " + i);
try
{
Thread.sleep(100);
}
catch(InterruptedException ie)
{
System.out.println("Child thread interrupted! " + ie);
}
}
System.out.println("Main thread finished!");
}
}

Synchronized Thread

import java.io.*;
class display
{
synchronized void show(int n)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
}
System.out.println(n+"the multiplication table");
}
}
class fivetable extends Thread
{
display d;
fivetable(display d)
{
this.d=d;
}
public void run()
{
d.show(5);
for(int i=1;i<=5;i++)
System.out.println(i+"*"+5+"="+(i*5));
}
}
class seventable extends Thread
{
display d;
seventable (display d)
{
this.d=d;
}
public void run()
{
d.show(7);
for(int i=1;i<=7;i++)
System.out.println(i+"*"+7+"="+(i*7));
}
}
class tentable extends Thread
{
display d;
tentable (display d)
{
this.d=d;
}
public void run()
{
d.show(10);
for(int i=1;i<=10;i++)
System.out.println(i+"*"+10+"="+(i*10));
}
}
class thread1
{
public static void main(String args[])
{
display d=new display();
fivetable t1=new fivetable(d);
seventable t2=new seventable(d);
tentable t3=new tentable(d);
t1.start();
t2.start();
t3.start();
}
}

Multi Thread

import java.io.*;

class A extends Thread
{
public void run()
{
for(int i=1;i<=3;i++)
{
System.out.println(i+" Execution of thread A");
}
System.out.println("Exit A");
}
};


class B extends Thread
{
public void run()
{
for(int j=1;j<=3;j++)
{
System.out.println(j +" execution of thread B");
}
System.out.println("Exit B");
}
};

class C extends Thread
{
public void run()
{
for(int k=1;k<=3;k++)
{
System.out.println(k +" execution of thread c");
}
System.out.println("Exit C");
}
};

class multithread
{
public static void main(String args[])throws IOException
{
new A().start();
new B().start();
new C().start();
}
};

Single Thread

import java.io.*;
public class single
{
public static void main(String st[]) throws IOException
{
Thread th=new Thread();
System.out.println("Numbers are printed line by line after 5 sec");
 try
{
for(int i=1;i<=10;i++)
{
 System.out.println(i);
 th.sleep(5000);
}
}
catch(InterruptedException e)
{
System.out.println("Thread Interrupted");
e.printStackTrace();
}
}
}

Tuesday, 6 September 2011

Java Programming Lab Cycle 1


Java Programming
Lab Cycle 1
1.Write a program to show how to handle the following exceptions
          1.Array Index out of bounds exception
          2.Divide by zero exception
          3.Arithmetic exception
2.Write a program to encrypt and decrypt a file
3.Write a program to implement the concept of single thread, multi thread, synchronized thread.
4.Write  a database program about the following.
          1.To read data from database with fields such as name,id,city and country.
          2.To insert a set of values into the table using Java Frame.
          3.To print and update the salary for a given employee number.
5.Write a program whether the Given number is armstrong or prime or perfect or not?
6.Write a program to count no. of words,vowels,lines in a given file.
7.Write a java program to show inheritance.
8.Write a program to show matrix multiplication.
9.Write a program to print the contents of file using filestream class.