Friday, 13 September 2013

Static members in java

class staticdemo{
int objno;
static int objcnt;

void hitcount(){
objno = ++objcnt;
}

void showhits(){
System.out.println("This page has been hit for :"+objcnt);
}

void showposition(){
System.out.println("client"+objno +"/" +objcnt);
}

}

public class Static {

    public static void main(String[] args) {
    staticdemo s = new staticdemo();
    staticdemo t = new staticdemo();
    staticdemo v = new staticdemo();
  
    s.hitcount();
    t.hitcount();
    v.hitcount();
  
    v.showhits();
  
    s.showposition();
    t.showposition();
    v.showposition();
  
    }
}


Method Overloading in java program

class Test{
int area(int i){
return i*i;
}

int area(int a, int b){
return a*b;
}

}


public class MethodOverloading {

        public static void main(String[] args) {
   
            Test t = new Test();
            int area;
            area = t.area(5); 
          
            System.err.println("Area of a square is :"+area);
          
            area = t.area(5,6);
            System.out.println("Area of a rectangle is :"+area);
    }
}


multiple if else statement using java


import java.util.*;

public class Condition {

    public static void main(String[] args) {
      int a[]= new int[1] ;
            Scanner sc=new Scanner(System.in);
            System.out.println("Please enter elements between 0 and 6");
            for(int j=0;j<1;j++)
                a[j] = sc.nextInt();

            for(int j=0;j<1;j++)
                if(a[j]==0)
                {
                System.out.println("Sunday!");
                 break;}
                else if(a[j]==1)
                {
                    System.out.println("Monday!");
                 break;}
                else if(a[j]==2)
                {
                System.out.println("Tuesday!");
                 break;}
                else if(a[j]==3)
                {
                    System.out.println("Wednesday!");
                 break;}
                else if(a[j]==4)
                {
                System.out.println("Thursday!");
                break; }
                 else if(a[j]==5)
                {
                    System.out.println("Friday!");
                 break;}
                else if(a[j]==6)
                {
                System.out.println("Saturday!");
                 break;}
                else
                {
                System.out.println("Sorry Invallid option! Enter the number between the range 0 to 6");
              break;  }
          
                          
    }
}