>

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.....

SCJP Questions with Answers Part -1

1. String compare using == operator


public void stringConvert(){
    if("java".toUpperCase()=="java".toUpperCase())
    {
        System.out.println(true);
    }
    else
        System.out.println(false);
}


2. String compare using equals :

public void stringEquals(){
    if("java".toLowerCase().equals("JAVA".toLowerCase()))
    {
        System.out.println("true");
    }
    else
        System.out.println(false);
           
}

3.


public void stringSubstring(){
    String str="java programming";
    str.substring(0,4);
    str.replace('a', 'j');
    String stra="welcome to "+str;
    System.out.println(stra);
   
}

4.
public void zero(){
        if(-00.0==+0.000)
        {
            System.out.println(true);
        }
        else
            System.out.println(false);



ANSWERS:

1.false -> == operator check objects not contents...
2.true  -> equals modifier checks only content...
3.welcome to java programming-> value of string replaced but not stored in anywhere.... So sting original contents not replaced...
4.true...-> there is nothing called negative and positive zero in java.. all zeros are treated equally..

Java program using If-Else Condition



WAP :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. 


public class NegativePositive {

    public static void posNeg(int a,int b, boolean neg)
    {
        if((a<0&b>0&neg==false)|(a>0&b<0&neg==false))
        {

            System.out.println(true);

        }
        else if(a<0&b<0&neg==true)
            System.out.println(true);

        else
            System.out.println(false);

    }    public static void main(String[] args) {
     
            posNeg(-5,-1,true);
    }

}

Travelling Guide -ROUTES


ROUTES-AN NGO
Every day numerous people travelling towards Chennai. But most of them were not aware of Chennai Bus routes. For those who new to Chennai or People do not have idea about Chennai bus routes can utilize this facility provided by an NGO called ROUTES.
Just call this number from Chennai. You will get help about all bus routes in Chennai. This service is provided at free of cost...
8695959595

...

Also do not forgot to post your comments...

Java String Program

Given a string, take the first 2 chars and return the string with the 2 
chars added at both the front and back, so "kitten" yields"kikittenki". 
If the string length is less than 2, use whatever chars are there.

My Own program follows:

public class FrontTwo {


    public static void frontT(String str)
    {
        if(str.length()>=2){
          
          
            String temp=str.substring(0, 2);
            System.out.println(temp+""+str+""+temp);
            }
            else if(str.length()<2)
            {
                String temp=str.substring(0);
                System.out.println(temp+""+str+""+temp);
            }
            else
            {
                System.out.println(str);
            }
      
    }

         public static void main(String[] args) {

        frontT("hai");

    }

}

Java program using Constructor

What is the output?

public class SaravananK {

    static int x=4;

    public SaravananK() {

        System.out.print(this.x);

          }   
public static void main(String[] args) {
     
         System.out.println(new SaravananK().x);
    }

}

Java program using Dot Dot Dot (String ... args)

public class DotDotDot {

    public static void main(String... args) {
       

        System.out.println("this will work? or not");

    }

}

Java program using While loop

What is the out put?
public class ThisNumber {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

       
        int the_number=4;
        int that_number;
        while(the_number<10)
        {
            that_number=the_number;
            the_number=the_number+that_number/2;
        }
        System.out.println(the_number);
    }
   

}

An Java program using inheritance

An Java program using inheritance :

class BaseA
{
    public BaseA(){
       
        System.out.println("Base A constructor");
    }
   
    public void methodOne(){
        System.out.println("Method one in base A");
    }
}

class BaseB extends BaseA{

    public BaseB(){
        super();
        System.out.println("Base b constructor");
    }
    public void methodOne(){

        BaseA ba=new BaseA();
        ba.methodOne();
        System.out.println("Method one in base B");
    }
}

public class Base{
   
    public static void main(String args[])
    {
        BaseA ba=new BaseB();
        BaseB bb=new BaseB();
        ba.methodOne();
        bb.methodOne();
        Base base=new Base();
       
       
    }
    public static void main(String args[][])
    {
        BaseA ba=new BaseA();
    }
}

Saturday, December 15, 2012

Java Inner Class

What is out put?

// A program using in inner class and method over loading
class Outer{
   protected int a=6;
   public void methodOne(){
       a=a++;
       System.out.println("In methodOne outer class a "+a);
   }
       class Inner{
          
            public void methodOne(){
                int a=6;
                System.out.println("In method one value of a "+a);
            }
            public void methodTwo(){
                a=a--;
                System.out.println("In method two value of a "+a);
            }
       }
       public void methodTwo(){
           System.out.println("In outerclass method Two value of a "+a);
       }
      
}
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Outer out=new Outer();
        Outer.Inner in=out.new Inner();
        out.methodOne();
        in.methodOne();
        in.methodTwo();
        out.methodTwo();
       
    }

}

Friday, December 14, 2012

Threads Question And Answers

What is thread?
It is the unit of execution which doesn't have any existence.

What is Synchronization?
It is the controlling mechanism when multiple threads are trying to access same resource.

How can be a thread Created?
By extending thread class and implementing runnable interface.

What is the use of sleep method?
Sleep method is used to stop the execution of a thread temporarily for a user defined milliseconds.

What is Preemptive Scheduling?
When an lower priority thread is already running  that thread will be suspended and higher priority thread will get executed.

What is Time slicing?
It is the process where the time is sliced between equal priority threads.

What are the methods available in threads?
Thread t=new Thread();
t.getName()-To get the name of thread.
t.setName()-To set the name for thread.
t.getPriority()- To get the priority
t.setPriority()-To set the priority

constant properties of thread
MAX_PRIORITY=10
MIN_PRIORITY=1
NORM_PRIORITY=5

What are the states of an thread?
newborn()->runnable->running>dead.

What is Daemon Thread?

It is a thread which will be invoked by automatically without intervention of user. Garbage collector is example for Daemon Thread.

What is run Method?
Whether we use implement runnable or extending thread the public void run() method should be override. And using start() method we start running a thread.

Saturday, December 8, 2012

Java Find maximum value in an array

import java.util.Scanner;
public class ArrayMaximum {
 public static void main(String args[])
 {
     System.out.println("Enter the limit for array :");
     Scanner in=new Scanner(System.in);
     int limit=in.nextInt();
     int array[]=new int[limit];
     System.out.println("enter the values for array :");
     for(int i=0;i<array.length;i++)
     {
         array[i]=in.nextInt();
     }
         System.out.println("Values of array");
             for(int m:array)
             {
                 System.out.print(" "+m);
             }
             System.out.println();
             int max=array[0];
             for(int j=1;j<array.length;j++){
                 if((array[j])>max)
                  max=array[j];
                  else
                      max=array[0];
                
             }
             System.out.println("biggest Number in this array "+ max);
 }
}


OUTPUT
Enter the limit for array :
5
enter the values for array :
4
5
6
7
8
Values of array
 4 5 6 7 8
biggest Number in this array 8

Friday, December 7, 2012

String Replace in Java

Most of us know that replace function in  String is only works with characters. As work within single quotes.. But String replace function also works fine with double quotes.

public class StringManipulation {

    public static void main(String args[]){

        String name="commission";
        name=name.replace("i","j");
        System.out.println(name);
    }
}


OUTPUT

commjssjon

Sequence Labelling a New Technology

SEQUENCE LABELING:

Sequence labeling is a technology where appliances in our home will interactive with us. Means they will listen our commands and take action based on commands. In addition with predefined algorithms. And  reduce the workload for housewives, senior citizens, disabled persons. But in out of box it will be increase our laziness. Because now a days people are forgot to walk. Most people will not walk even 30 minutes per day.

Smart homes, or homes wherein appliances are turned on or off with direction from us — either pre-loaded cues, or through speech recognition — already exist. Apparently, these technologies are based on a process called sequence labelling, which classifies how tasks occur in a consequential sequence.

For more details visit the
http://www.thehindu.com/sci-tech/technology/towards-a-smart-home/article4156967.ece

An article from Hindu.,..