Saturday, 15 June 2013

java program for use break key word

public class Test {

   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};

      for(int x : numbers ) {
         if( x == 30 ) {
       break;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

java for loop program

public class Test {

   public static void main(String args[]) {

      for(int x = 10; x < 20; x = x+1) {
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}

java do while loop

public class Test {

   public static void main(String args[]){
      int x = 10;

      do{
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 20 );
   }
}

java while loop

public class Test {

   public static void main(String args[]) {
      int x = 10;

      while( x < 20 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

java inheritance program

class Employee
{
    private int eno;
    private String name;
    private float bs;
    public Employee()
    {
        eno=0;
        name="Empty";
        bs=0;
    }
    public Employee(int eno,String name, float bs)
    {
        this.eno=eno;
        this.name=name;
        this.bs=bs;
    }
    public String toString()
    {
        return "Eno - "+eno+"\n Name - "+name+"\n Salary - "+bs;
    }
}
class WageEmp extends Employee
{
    private int hours,rate;
    public WageEmp()
    {
        hours=0;
        rate=0;
    }
    public WageEmp(int eno,String name,float bs, int hours, int rate)
    {
        super(eno,name,bs);
        this.hours=hours;
        this.rate=rate;
    }
    public String toString()
    {
        return super.toString()+"\n Housr -"+hours+"\n Rate - "+rate;
    }
}
class EmpDemo
{
    public static void main(String []args)
    {
        WageEmp we=new WageEmp(123,"Amit",3000.0f,10,5);
        System.out.println("Wage Emp is - "+we);
    }
}       

simple reverse program in java inner class

import java.io.*;
class outter
{
    int no=512,rem,rev=0,a;
    public void print()
    {
        inner i=new inner();
        a=i.cal(rev);
        System.out.println("reverse of given number is = "+a);
    }
    class inner
    {
        public int cal(int rev)
        {
            while(no!=0)
            {
                rem=no%10;
                rev=rev*10+rem;
                no=no/10;
            }
            return(rev);
        }
    }
}
class innerclass_simple_reverse
{
    public static void main(String args[])
    {
        outter o=new outter();
        o.print();
    }
}

Use java string function valueOf()

import java.io.*;

public class valueOf
{
   public static void main(String args[])
   {
      double d = 102939939.939;
      boolean b = true;
      long l = 1232874;
      char[] arr = {'a', 'b', 'c', 'd', 'e', 'f','g' };

      System.out.println("Return Value : " + String.valueOf(d) );
      System.out.println("Return Value : " + String.valueOf(b) );
      System.out.println("Return Value : " + String.valueOf(l) );
      System.out.println("Return Value : " + String.valueOf(arr) );
   }
}