JAVA Programming
Thursday, 3 July 2014
Saturday, 26 April 2014
Ex 30: Write a program in Java, which takes the name of a file from user, read the contents of the file and display it on the console.
import java.io.*;
class s08_05
{
public static void main(String args[])
{
FileInputStream fis=null;
Try
{
fis=new FileInputStream(args[0]);
byte ch;
while((ch=(byte)fis.read())!=-1)
{System.out.print((char)ch); }
}
catch(IOException e)
{ System.out.println("interrupted");}
finally
{
try
{fis.close();}
catch(IOException e)
{System.out.println("error in closing");}
}
}
}
class s08_05
{
public static void main(String args[])
{
FileInputStream fis=null;
Try
{
fis=new FileInputStream(args[0]);
byte ch;
while((ch=(byte)fis.read())!=-1)
{System.out.print((char)ch); }
}
catch(IOException e)
{ System.out.println("interrupted");}
finally
{
try
{fis.close();}
catch(IOException e)
{System.out.println("error in closing");}
}
}
}
Ex 29: Write a program in Java to read a statement from console, convert it into upper case and again print on console.
import java.io.*;
class s08_04
{
public static void main(String a[]) throws IOException
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter file Statement:");
String s1=in.readLine();
System.out.println(s1.toUpperCase());
}
}
class s08_04
{
public static void main(String a[]) throws IOException
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter file Statement:");
String s1=in.readLine();
System.out.println(s1.toUpperCase());
}
}
Ex 28: Write a program for searching strings for the first occurrence of a character or substring and for the last occurrence of a character or substring.
import java.io.*;
class s08_03
{
public static void main(String[]args) throws Exception
{
int len1,len2,last=0;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the string:");
String s1=in.readLine();
System.out.println("Enter searching string:");
String s2=in.readLine();
len1=s1.length();
len2=s2.length();
for(int i=0;i<=(len1-len2);i++)
{
if(s1.substring(i,len2+i).equals(s2))
{
if(last==0)
System.out.println("first occurance is at possition :"+(i+1));
last=i+1;
}
}
if(last!=0)
System.out.println("last occurance is at possition :"+last);
else
System.out.println("the string is not found");
}
}
class s08_03
{
public static void main(String[]args) throws Exception
{
int len1,len2,last=0;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the string:");
String s1=in.readLine();
System.out.println("Enter searching string:");
String s2=in.readLine();
len1=s1.length();
len2=s2.length();
for(int i=0;i<=(len1-len2);i++)
{
if(s1.substring(i,len2+i).equals(s2))
{
if(last==0)
System.out.println("first occurance is at possition :"+(i+1));
last=i+1;
}
}
if(last!=0)
System.out.println("last occurance is at possition :"+last);
else
System.out.println("the string is not found");
}
}
Ex 26: Writ a program in Java to create a String object. Initialize this object with your name. Find the length of your name using the appropriate String method. Find whether the character „a‟ is in your name or not; if yes find the number of times „a‟ appears in your name. Print locations of occurrences of „a‟ .Try the same for different String objects.
class data
{
String name;
data(String n){ name=n; }
void disp()
{
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Name :"+name);
int c=0;
int len=name.length();
for(int i=0;i<len;i++)
if(name.charAt(i)=='A'||name.charAt(i)=='a')
{
c++;
System.out.println("number of occurance :"+c);
System.out.println("Possition :"+(i+1));
}
if(c==0)
System.out.println("there is no 'A' available in the string");
}
}
class s08_01
{
public static void main(String ar[])
{
data d1=new data("anil kumar");
d1.disp();
data d2=new data("biju");
d2.disp();
}
}
{
String name;
data(String n){ name=n; }
void disp()
{
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Name :"+name);
int c=0;
int len=name.length();
for(int i=0;i<len;i++)
if(name.charAt(i)=='A'||name.charAt(i)=='a')
{
c++;
System.out.println("number of occurance :"+c);
System.out.println("Possition :"+(i+1));
}
if(c==0)
System.out.println("there is no 'A' available in the string");
}
}
class s08_01
{
public static void main(String ar[])
{
data d1=new data("anil kumar");
d1.disp();
data d2=new data("biju");
d2.disp();
}
}
Ex 24: Write a program for generating 2 threads, one for printing even numbers and the other for printing odd numbers.
class even extends Thread
{
Thread t=null;
even()
{
t=new Thread(this);
start();
}
public void run()
{
try
{
for(int i=2;i<50;i+=2)
System.out.print(i+" ");
Thread.sleep(100);
}
catch(Exception e)
{System.out.println("thread interepted");}
}
}
class odd extends Thread
{
Thread t=null;
odd()
{
t=new Thread(this);
start();
}
public void run()
{
try
{
for(int i=1;i<50;i+=2)
System.out.print(i+" ");
Thread.sleep(100);
}
catch(Exception e)
{System.out.println("thread interepted");}
}
}
class s07_03
{
public static void main(String arg[])
{
even e=new even();
odd o=new odd();
}
}
{
Thread t=null;
even()
{
t=new Thread(this);
start();
}
public void run()
{
try
{
for(int i=2;i<50;i+=2)
System.out.print(i+" ");
Thread.sleep(100);
}
catch(Exception e)
{System.out.println("thread interepted");}
}
}
class odd extends Thread
{
Thread t=null;
odd()
{
t=new Thread(this);
start();
}
public void run()
{
try
{
for(int i=1;i<50;i+=2)
System.out.print(i+" ");
Thread.sleep(100);
}
catch(Exception e)
{System.out.println("thread interepted");}
}
}
class s07_03
{
public static void main(String arg[])
{
even e=new even();
odd o=new odd();
}
}
Ex 23: Write a program to launch 10 threads. Each thread increments a counter variable. Run the program with synchronization.
class s07_02
{
public static void main(String arg[])throws Exception
{
data d1=new data();
data d2=new data();
data d3=new data();
data d4=new data();
data d5=new data();
data d6=new data();
data d7=new data();
data d8=new data();
data d9=new data();
data d10=new data();
System.out.println(d10.count);
}
}
//---------------------------
class item { static int count=0; }
class data extends item implements Runnable
{
item d=this;
Thread t;
data()
{
t=new Thread(this);
t.start();
}
public void run()
{ d=syn.increment(d); }
}
//==============================
class syn
{
synchronized static item increment(item i)
{
i.count++;
return(i);
}
}
{
public static void main(String arg[])throws Exception
{
data d1=new data();
data d2=new data();
data d3=new data();
data d4=new data();
data d5=new data();
data d6=new data();
data d7=new data();
data d8=new data();
data d9=new data();
data d10=new data();
System.out.println(d10.count);
}
}
//---------------------------
class item { static int count=0; }
class data extends item implements Runnable
{
item d=this;
Thread t;
data()
{
t=new Thread(this);
t.start();
}
public void run()
{ d=syn.increment(d); }
}
//==============================
class syn
{
synchronized static item increment(item i)
{
i.count++;
return(i);
}
}
Subscribe to:
Posts (Atom)