Wednesday 28 March 2018

Encapsulation in java







package javaapplication23;



class employee

{

private int emp_id ;

private String emp_name ;



public void setid(int value)

{

    emp_id=value;

}

public int getid()

{

 return emp_id; 

}





public void setname(String value)

{

    emp_name=value;

}

public String getname()

{

 return emp_name; 

}









}







public class JavaApplication23 {



   

    public static void main(String[] args)   

    {

        employee e=new employee();

       e.setid(1);

       e.setname("ali");

     

        System.out.println("EMPLOYEE ID "+e.getid());

        System.out.println("EMPLOYEE Name "+e.getname());

       

       

     

     

    }

   

}



Access modifier in java







package javaapplication21;





class A

{

  protected  String name="abcd"; 

private void msg1()

{

    System.out.println("hello this is msg1 and private");

}



protected void msg2()

{

    System.out.println("hello this is msg2 and protected");

}



}



class B extends A

{

public void showname()

{

 System.out.println("name is "+name);

}

}

public class JavaApplication21

{

    public static void main(String[] args)

    {

    B a=new B();

    a.msg2();

    a.showname();



     

     

       

    }

   

}


Wednesday 21 March 2018

Super Keyword in java





package superkeyword;



class mamal

{

public String color="blue";



public void eat()

{

    System.out.println("It eats.....");

}

public void reproduces()

{

    System.out.println("It gives birth to a new living being.....");

}





}



class blueWhale extends mamal

{

 

public String Color=super.color;   

public void swim()

{

    System.out.println("It swims in sea.......");

}

   

public void allactions()

{

    super.eat();

    super.reproduces();

    swim();



}





}













public class Superkeyword {



 

    public static void main(String[] args)

   

    {

       

        blueWhale bw=new blueWhale();

        System.out.println("color is "+ bw.Color);

        bw.allactions();

   

    }

   

}


Tuesday 20 March 2018

Method Overriding in java with practical Example





package polymorph;



class bank

{



 public double getinterest(int principle,int time)

 {



     return 0;

 }

   

   

}



class HabibMetro extends bank

{

public double getinterest(int principle,int time)

 {

     return principle* time* 0.1;

 }





}



class Alfalah extends bank

{

public double getinterest(int principle,int time)

 {

     return principle* time* 0.15;

 }





}



public class Polymorph

{



    public static void main(String[] args)

    {

     

        int p=10000,year=2;

        double interest;

      HabibMetro hb=new HabibMetro();

      interest=  hb.getinterest(p, year);

     System.out.println("Simple Interest from bank habib metro is "+interest);

 

   

     Alfalah al=new Alfalah();

   interest=  al.getinterest(p, year);

       System.out.println("Simple Interest from bank Alfalah  is "+interest);

 

     

   

   

       

    }

   

}


Wednesday 14 March 2018

METHOD OVERLOADING IN JAVA


package javaapplication16;

class calculator
{

    public int sum(int x,int y)
    {
    return x+y;
    }

   public int sum(int x,int y,int z)
    {
    return x+y+z;
    }
 
 
   public double sum(double x,double y,double z)
    {
    return x+y+z;
    }
 

}




public class JavaApplication16
{

   
    public static void main(String[] args)
    {
        calculator c=new calculator();
        double r =c.sum(3, 4, 10);
       
        System.out.println("sum is "+r);
   
    }
   
}

Aggregation in java


package aggregate;

class Address
{

 int zipcode; 
String city,street;

public Address(int zc,String cit,String st) // parametrized constructor.........
{
this.zipcode=zc;
this.street=st;
this.city=cit;
   
}
   
   
}

class employee
{
int emp_id ;
String name;
double salary;
Address a;

public employee(int id,String name,double salary,Address ad)
{
this.emp_id=id;
this.name=name;
this.salary=salary;
this.a=ad;

}


void display()
{
    System.out.println("Emp id= "+emp_id);
    System.out.println("Emp Name= "+name);
    System.out.println("Emp Salary= "+salary);
    System.out.println("Emp Address= "+a.zipcode+" - "+a.street+" - "+a.city);
   
   
}


}





public class Aggregate {

 
    public static void main(String[] args)
    {
        Address ad=new Address(78650,"Karachi","Street-2");
        employee emp=new employee(541,"salman",76000,ad);
        emp.display();

       
    }
   
}

Monday 12 March 2018

Hierarchical inheritance in java





package javaapplication14;



class employee

{

double salary;

}

class developer extends  employee

{

int dvbonus=5000;

String dv_name;



}



class designer extends  employee

{

int debonus=4000;

String de_name;



}







public class JavaApplication14 {



   

    public static void main(String[] args)

    {

        designer de=new designer();

       double salary= de.salary=35000;

      String name= de.de_name="ali";

       System.out.println("Name="+name+"\ntotal salary= "+(salary+de.debonus));

           

       developer d=new developer();

       salary=d.salary=50000;

       name="immad";

         System.out.println("Name="+name+"\ntotal salary= "+(salary+d.dvbonus));



    }

   

}


Types of Inheritance Single & multilevel inheritance in java





package javaapplication13;



class calculator1

{

   

public int sum(int a,int b)

{

return a+b;

}





}





class calculator2 extends calculator1

{

   

public int sub(int a,int b)

{

return a-b;

}

   

   

}





class calculator3 extends calculator2

{

   

public int mul(int a,int b)

{

return a*b;

}

}











public class JavaApplication13 {



   

    public static void main(String[] args)

    {

   int r;

    calculator2 c=new calculator3();

   r= c.sum(9, 9);

   System.out.println(r);

   r=c.sub(9,9);

   System.out.println(r);

 

   

    }

   

}



Wednesday 7 March 2018

inheritance in java









package calculator;



import java.util.Scanner;



class simpleCalculator

{



public float add(float a,float b)

{

return a+b;

}



public float sub(float a,float b)

{

return a-b;

}

public float mul(float a,float b)

{

return a*b;

}

public float div(float a,float b)

{

return a/b;

}





}







class simpleCalculator2 extends simpleCalculator

{



    public int modulus(int a ,int b)

    {

    return a%b;

    }

           



}







public class Calculator

{



 

    public static void main(String[] args)

   

    {

        Scanner sc=new Scanner(System.in);

        int a,b,choice;

        float x=0;

        simpleCalculator2 c=new simpleCalculator2();

        System.out.println("Enter the 1st Number: ");

        a=sc.nextInt();

        System.out.println("Enter the 2nd Number: ");

        b=sc.nextInt();

        System.out.println("Press 1 for Addtion\nPress 2 for Subtraction\nPress 3 for Mulitplication\nPress 4 for Division\nPress 5 for Modulus");

        System.out.println("Enter a number to Perform Airthmatic Operation:  ");

        choice=sc.nextInt();

        switch(choice)

        {

            case 1:

               x= c.add(a,b);

                break;

           

                case 2:

               

                  x=  c.sub(a, b);

                break;

                case 3:

                  x=  c.mul(a, b);

                break;

               

                case 4:

                  x=  c.div(a, b);

                break;

                case 5:

                  x=  c.modulus(a, b);

                break;

           

        }

       

        System.out.println("Answer is "+x);

       

       

 

   

   

    }

   

}


Sunday 4 March 2018

Array in java , Replace Array Element by Index program





package javaapplication10;



import java.util.Scanner;





public class JavaApplication10 {



   

    public static void main(String[] args)

    {

        int n,index;

        Scanner obj=new Scanner( System.in);

        int []x=new int[]{10,20,40,30};

        System.out.println("Enter the number that you want to Replace in array: ");

        n=obj.nextInt();

        System.out.println("Enter the index on which you want to replace the number "+n);

        index=obj.nextInt();

   

        if (index>-1 && index<x.length)

        {

          x[index]=n; 

       

         

        }

        else

        {

            System.out.println("Invalid input.......");

        }       

     

          for(int c:x)

        {

       

            System.out.print(c+" ");

        }

       

       

           

       

       

       

    }

   

}


Arrays in java User input,Display Array using foreach loop & sum of array





package javaapplication9;



import java.util.Scanner;





public class JavaApplication9 {



 

    public static void main(String[] args)

    {

        Scanner obj=new Scanner(System.in);

            int sum=0,a=0;   

        int []x=new int[5];

       

        for (int i = 0; i < x.length; i++)

        {

            System.out.println("Enter the number at index "+i);

            x[i]=obj.nextInt();

           

           

        }

         System.out.println("-------Printing the values--------");     

        for(int c:x)

        {

            System.out.println(c);

        }

  while(a<x.length)

  {

  sum=sum+x[a];

  a++;

 

  }

     System.out.println("-------Sum of the Array--------");     

        System.out.println("sum = "+sum);

       

       

    }

   

}


Thursday 1 March 2018

Do while and Foreach Loop in Java









Do While


package javaapplication7;


public class JavaApplication7 {

   
    public static void main(String[] args)
    {
        int i=10;
        do 
        {
            System.out.println(i);
            i--;
        } while (i>0);
        
       
    }
    
}

Foreach 


package javaapplication8;


public class JavaApplication8 {

    
    public static void main(String[] args) 
    {
        String[] fruits = new String[] { "Orange", "Apple", "Pear", "Strawberry" };

     for (String x : fruits)
     {
         System.out.println(x);
    
     }
    
}
}

While Loop in Java with Factorial Program example







package javaapplication6;





public class JavaApplication6 {



   

    public static void main(String[] args)

    {

      int fac=1;

        int i=25,temp;

        temp=i;

        while(i>=1)

        {

        fac=fac*i;

        i--;   

        }

       

        System.out.println(temp+"!="+fac);

    }

   

}



for loop in javaNested for loop Star Pattern, Sum of even and odd number...





package javaapplication5;





public class JavaApplication5 {



   

    public static void main(String[] args)

    {

        int esum=0,osum=0,tsum=0;

       

        for (int i = 1; i <= 100; i++)

        {

       

            if (i%2==0)

            {

            esum=esum+i;

               

            }

            else if (i%2!=0)

            {

                osum=osum+i;

            }

           

            tsum=tsum+i;

           

           

        }

       

        System.out.println("Sum of even no is "+esum);

        System.out.println("Sum of odd no is "+osum);

        System.out.println("Total from addition "+(esum+osum));

       

        System.out.println("Total from loop "+tsum);

       

       

       

       

       

       

       

       

       

       

       

       

       

    }

   

}



for loop in javaNested for loop Star Pattern, Sum of even and odd number...





package javaapplication5;





public class JavaApplication5 {



   

    public static void main(String[] args)

    {

        int esum=0,osum=0,tsum=0;

       

        for (int i = 1; i <= 100; i++)

        {

       

            if (i%2==0)

            {

            esum=esum+i;

               

            }

            else if (i%2!=0)

            {

                osum=osum+i;

            }

           

            tsum=tsum+i;

           

           

        }

       

        System.out.println("Sum of even no is "+esum);

        System.out.println("Sum of odd no is "+osum);

        System.out.println("Total from addition "+(esum+osum));

       

        System.out.println("Total from loop "+tsum);

       

       

       

       

       

       

       

       

       

       

       

       

       

    }

   

}



Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...