Tuesday, April 24, 2012
Test Object state after serialization and transient variables state.
import java.io.*;
public class TestSerializationVersion {
public static void main(String [] args)
{
Dog d=new Dog(45,"Ramesh");
try
{
System.out.println(d.getName()+" "+d.getAge());
//FileOutputStream fos=new FileOutputStream("Anand.txt");
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("Anand.txt"));
oos.writeObject(d);
d.setAge(56);
d.setName("Sravani");
System.out.println(d.getName()+" "+d.getAge());
oos.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
try
{
//FileOutputStream fos=new FileOutputStream("Anand.txt");
ObjectInputStream ios=new ObjectInputStream(new FileInputStream("Anand.txt"));
Dog d1=(Dog)ios.readObject();
ios.close();
System.out.println(d1.getName()+" "+d1.getAge());
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
class Dog implements Serializable
{
private int age;
private transient String name;
public Dog(int age, String name)
{
this.age=age;
this.name=name;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String setName(String name1)
{
this.name=name1;
return name1;
}
public int setAge(int age1)
{
this.age=age1;
return age1;
}
}
Thursday, April 19, 2012
Sequential run of threads
Thread t1 = new Thread( new MyRunnable( ) );
Thread t2 = new Thread( new MyRunnable( t1 ) );
Thread t3 = new Thread( new MyRunnable( t2 ) );
t1.start();
t2.start();
t3.start();
...
class MyRunnable()
{
private Thread mustFinishFirst;
public MyRunnable()
{}
public MyRunnable( Thread mustFinishFirst )
{
this.mustFinishFirst = mustFinishFirst;
}
public void run()
{
if( mustFinishFirst != null )
mustFinishFirst.join()
...
}
}
Thread t2 = new Thread( new MyRunnable( t1 ) );
Thread t3 = new Thread( new MyRunnable( t2 ) );
t1.start();
t2.start();
t3.start();
...
class MyRunnable()
{
private Thread mustFinishFirst;
public MyRunnable()
{}
public MyRunnable( Thread mustFinishFirst )
{
this.mustFinishFirst = mustFinishFirst;
}
public void run()
{
if( mustFinishFirst != null )
mustFinishFirst.join()
...
}
}
Wednesday, April 18, 2012
To find nth Fibonacci number
//Using recursion
public class Febo
{
public static void main(String[] args)
{
for ( int i = 0; i < 10; i++ ) {
System.out.print ( fib(i) + " " );
}
//System.out.println ( fib(10) );
}
//to find nth Fibonacci number
static long fib(int n) {
return n <= 1 ? n : fib(n-1) + fib(n-2);
}
}
//Simple program written by me
import java.io.*;
public class Febo {
public static void main(String []args) throws IOException
{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a number");
String input=reader.readLine();
int i=Integer.parseInt(input);
int []ret=Fibo(i);
for(int j=0;j<ret.length;j++)
{
System.out.print(" "+ret[j]);
}
}
public static int[] Fibo(int n)
{
int a[]=new int[n];
if (n==1)
a[0]=0;
else
{a[0]=0;
a[1]=1;
}
if(n>2)
{
for(int i=2;i<n;i++)
{
a[i]=a[i-1]+a[i-2];
}
}
return a;
}
}
public class Febo
{
public static void main(String[] args)
{
for ( int i = 0; i < 10; i++ ) {
System.out.print ( fib(i) + " " );
}
//System.out.println ( fib(10) );
}
//to find nth Fibonacci number
static long fib(int n) {
return n <= 1 ? n : fib(n-1) + fib(n-2);
}
}
//Simple program written by me
import java.io.*;
public class Febo {
public static void main(String []args) throws IOException
{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a number");
String input=reader.readLine();
int i=Integer.parseInt(input);
int []ret=Fibo(i);
for(int j=0;j<ret.length;j++)
{
System.out.print(" "+ret[j]);
}
}
public static int[] Fibo(int n)
{
int a[]=new int[n];
if (n==1)
a[0]=0;
else
{a[0]=0;
a[1]=1;
}
if(n>2)
{
for(int i=2;i<n;i++)
{
a[i]=a[i-1]+a[i-2];
}
}
return a;
}
}
Efficient program to reverse a string
public class Reverse {
public static void main(String [] args)
{
String s=reverse("Anand Prakash Yadav");
System.out.println(s);
}
public static String reverse ( String s ) {
int length = s.length(), last = length - 1;
char[] chars = s.toCharArray();
for ( int i = 0; i < length/2; i++ ) {
char c = chars[i];
chars[i] = chars[last - i];
chars[last - i] = c;
}
return new String(chars);
}
}
Subscribe to:
Comments (Atom)