>

Wednesday, December 19, 2012

Thread Program four in Java


public class Thread_Demo extends Thread{

public void run(){
System.out.println("Some text");
}
public static void main(String[] args) throws InterruptedException{


Thread_Demo t=new Thread_Demo();
Thread_Demo td1=new Thread_Demo();

t.start();

System.out.println("ISALIVE :"+t.isAlive());
t.sleep(500);
System.out.println("ISALIVE :"+t.isAlive());
//t.destroy();
t.resume();
System.out.println("ISALIVE :"+t.isAlive());
}

}

Thread Program three in Java


What is out put?

public class Thread_Demo extends Thread{

public void run(){
System.out.println("Some text");
}

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

Thread_Demo t=new Thread_Demo();
Thread_Demo td1=new Thread_Demo();
t.run();
System.out.println("ISALIVE :"+t.isAlive());
t.sleep(500);
System.out.println("ISALIVE :"+t.isAlive());
t.destroy();
System.out.println("ISALIVE :"+t.isAlive());
}

}

Thread Program Two in Java

What is output?


public class Thread_Demo extends Thread{

public void run(){
System.out.println("Some text");
}

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


Thread_Demo t=new Thread_Demo();
Thread_Demo td1=new Thread_Demo();

t.start();

System.out.println("ISALIVE :"+t.isAlive());
t.sleep(500);
System.out.println("ISALIVE :"+t.isAlive());
t.destroy();
System.out.println("ISALIVE :"+t.isAlive());
}

}

Thread Program in Java

What is out put?


public class Thread_Demo extends Thread{

public void run(){
System.out.println("Some text");
}

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


Thread_Demo td=new Thread_Demo();
Thread_Demo td1=new Thread_Demo();
/*Thread t=new Thread(td);*/
td.start();
td.setPriority(10);
System.out.println(td.getName()+td.getPriority());
td1.run();
System.out.println(td1.getName()+td1.getPriority());
}
}

Sunday, December 16, 2012

File Creation in java

import java.io.File;
import java.io.IOException;

public class FileCreatin {

    public static void main(String args[]) throws IOException
    {
        File f=new File("F:/java.txt");
        if(!f.exists())
        {
            f.createNewFile();
            System.out.println("file created successfully");
        }
        else
            System.out.println("File already exists");
       
    }
}

Simple Java programs

1.
Question :
Given two non-negative int values, return true if they have the same last digit, such as with 27 and 57. Note that the % "mod" operator computes remainders, so 17 % 10 is 7.

Ans:
public boolean lastDigit(int a, int b) {
          int r=a%10;
        int r1=b%10;
        if(r==r1)
        {
            System.out.println(true);
        }  // These programs for educational purpose only. Dont copy and publish in your blog or website..
        else
            System.out.println(false);
}

2.

Question :Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative.

Ans:
public boolean posNeg(int a, int b, boolean negative) {
  if((a<0&b>0&negative==false)|(a>0&b<0&negative==false))
        {
            System.out.println(true);
        }
        else if(a<0&b<0&negative==true)
            System.out.println(true); 
        else
            System.out.println(false);
} // These programs for educational purpose only. Dont copy and publish in your blog or website..


3.
 Question :
Given two temperatures, return true if one is less than 0 and the other is greater than 100.

icyHot(120, -1) → true
icyHot(-1, 120) → true
icyHot(2, 120) → false

Answer:
public boolean icyHot(int temp1, int temp2) {
  if((temp1<0&temp2>100)|(temp1>100&temp2<0))
        {
            System.out.println(true);
        }
        else
            System.out.println(false);
} // These programs for educational purpose only. Dont copy and publish in your blog or website..

OCJP Constructor and method overloading....

1.An java program with  Constructor and method overloading

public class Workout {

    public void workout(){
        System.out.println("Constructor in Workout");
    }
    public Workout(){
        Workout.main("hi");
        System.out.println("Over loaded constructor in Workout");
    }    public static void main(String string) {

System.out.println(string);
new Workout();
       
    }
    public static void main(Integer args[])
    {
        Workout.main("hi");
    }
public static void main(String args[])
{
    new Workout();//An program with constructor and method overloading......by k.saravanan
// program for educational purposes only.. Dont copy and post in your blog or ur website ....
}
}


What is output?

Prints hi continuously and stack overflow error will occur.....