Wednesday, January 27, 2016

Use of Static keyword

Static keyword in java is mainly used for memory management.this is mainly used for variable,methods and blocks.
  • Java static variable : It is used to refer common property of all the objects. As name of city which is common for all the entity.
  • Static variable gets memory at time of class load.

This explain you how static variable works , if you have some more time just see it.

For videos in hindi click here 

Simple Static example

public class studentdetails {

int age ;
String name;
static String city="fremont";
studentdetails(int age,String name)
{
this.age=age;
this.name=name;
}

public static void hello(){ //this is a static method
     city = "foster";
System.out.println("My city is "+city);// Static method can use only static variable
}

public void test(){
System.out.println("My Name is -->"+name);
System.out.println("My age is -->"+age);
System.out.println("City is -->"+city);

}
// static method can use only static variable 

public static void main(String[] args) {
studentdetails obj1 = new studentdetails(22,"Ram");
studentdetails obj2 = new studentdetails(19,"krishna");  
obj1.test();//variable city is loaded only once in class so memory
obj2.test();//second time also when object is called static variables memory remains same
hello();
}
}

Output:-

My Name is -->Ram
My age is -->22
City is -->fremont
My Name is -->krishna
My age is -->19
City is -->fremont
My city is foster

Static method is used by putting a static keyword after access modifier  in method as seen in above example.
  • Static method uses static variable and can also change value of it.
  • Static method can be call without creating instance of class,as it belongs to class
Static method is used in counter of website as when object is created the value is add rather creating instance of object.

Example : this program shows difference of creating object with static and non-static variable, expense of  room mates total and personal for different students.
program

public class Room {
static double totalExpense;
double localexpence=0;

Room(){
}

public void addExpense(double amount){
totalExpense=totalExpense+amount; // method use static variable to add total expense 
       System.out.println("total is "+totalExpense);
}
public void addnewexpence(double amount){
localexpence=localexpence+amount; // it uses non-static variable to add personal expense
System.out.println("personal expence is "+localexpence);
}
//every time when object is created new memory is allocated to non-static variable 
       // static variable shares common memory , so not invoked when instance is created

public static void main(String[] args) {
 
      Room obj1=new Room();
obj1.addExpense(20d);
obj1.addnewexpence(40d);
Room obj2=new Room();
obj2.addExpense(50d);
obj2.addnewexpence(60d);
Room obj3=new Room();
obj3.addExpense(40d);
obj3.addnewexpence(70d);
}

}

Output:-
total is 20.0
personal expence is 40.0
total is 70.0
personal expence is 60.0
total is 110.0
personal expence is 70.0





Thursday, January 21, 2016

Object oriented programming

Object oriented programming is method by which you can program using classes and objects.Object is entity existing in real world.Some concepts of object oriented programming are as follows:
  1. Object
  2. Class
  3. Inheritance
  4. Encapsulation
  5. Abstraction
  6. polymorphism 
Object: Object is entity in existing real world with some properties. 

Class : In real world a class is an entity from which the object are created.Every car was constructed from same set of defined procedures,so contains same components. A class is an entity from where object are created.Class is a collection of object of similar type.

Inheritance: It is quality of class to acquire all the qualities and properties of parent class.It provides code re-usability.

Encapsulation: Process of binding code and data together in a single unit is know as encapsulation.Is is used to hide the implementation from the end user.

Abstraction: It refer to quality of hiding internal details and showing functionality.In is used to implement interface in java.

Polymorphism: It refers to the objects and methods having same name but different behavior.It also refer to methods having different parameters. 



Sunday, January 17, 2016

Arrays in Java

An array is collection of values of same datatype.Its length is decided when it is created.It has index by which its element are accessed.
Index of array is always from 0 to n-1 ( for n size of array).

  • How to declare array?
datatype[ ]  array_name; Example : ( int[] array_name; ) or (float[] array_name;)
  • How to allocate memory to array?

array-name = new datatype[size];
 Example( array_name= new int[10];) or (array_name=new float[15]). 
size is number of elements you can store array.
  • How you can initialize array?
array_name[0]= value;
array_name[1]=value1;
Example
array_name[0]=1;
array_name[2]=4;
Above example state that '1' value is stored at position 0 and '4' at position 2.
  • How to access element of array at some position ?

System.out.println("Elements at position 1"+array_name[1]);

  • How to do initialize and declare array in same step?
int array_name[] ={3,70,47,87,34,55};

Program Example in Eclipse

import java.util.Scanner;
public class arrayexp {

public static void main(String[] args) {
int i;
Scanner sc = new Scanner(System.in);
System.out.println("enter how much element u want in array ");
i=sc.nextInt();
int Arry[] = new int[i];
System.out.println("enter element s");
for(int a=0;a Arry[a]=sc.nextInt();
}
System.out.println("Elements in array are ");
for(int a=0;a System.out.print(" "+Arry[a]);
}

}


}

Output

enter how much element u want in array
4
enter element s
2
4
6
8
Elements in array are
 2 4 6 8


Tuesday, October 6, 2015

Reverse number program in java

Reverse number Program is program which reverse the number given by user and if both the numbers are same reverse and given reverse .For example user enters 123 the reverse number is 321.and if user enters number 121 and if its reverse number is same 121, then the number is called palidrona.This program have static  reverse() method which invoke and return reverse intiger.

Example:-

Program

import java.util.Scanner; // this package util and Class Scanner is declared to take input from user
/*
*@ scanner class
* @author khant vyas*/
public class reverse // name of class
{

public static boolean Ispalidrona(int rev)// its method of boolean type
{
if (rev==reverse.reverse(rev))
{
System.out.println("number is palidrona ");
return true;
}
else

System.out.println("number is not  palidrona");
return false;


}
public static int reverse(int rev) /* method reverse of integer type with intiger argument and return integer value*/
       {
int result=0;
while(rev>0)
{
int num=rev%10;
rev=rev/10;

result= result*10+num;
}
return result;
}

public static void main(String[] args)
{
int a;// variable a takes user input in program
Scanner sc = new Scanner(System.in); // sc is object to the Scanner class
System.out.println("Enter the number you wanted to reverse ");
a=sc.nextInt();// nextInt() method is used accept intiger value from user

reverse(a); // passing a variable in reverse() method
Ispalidrona(reverse(a));// this method takes return value from reverse function

System.out.println(" reverse string is"+reverse(a));

}

}

Monday, October 5, 2015

linear search program in java

Linear search in java program is used to find a particular element in array with n number of values

Example 

Write a program to find a particular position of element in given dynamic array

Program 

import java.util.Scanner; / this package util and Class Scanner is declared to take input from user
/*
*@ scanner class
* @author khant vyas
*/public class linearsearch // name of class
{

public static void main(String[] args) {
int value,sizea,searchele, list[]; // list variable to array
Scanner sc = new Scanner(System.in); // sc is object to the Scanner class
System.out.println("enter number of elements you need to enter");
value=sc.nextInt(); // nextInt() method is used accept intiger value from user
list=new int[value];// array initilization
System.out.println("enter "+value+" elements ");

for(sizea=0;sizea
list[sizea]=sc.nextInt(); // adding value by user in array of size sizea-1
}
if(sizea==value)
System.out.println("enter the element you wanted to find");
searchele=sc.nextInt();// user enter element to be searched

for(sizea=0;sizea if(list[sizea]==searchele)// comparing  values in array with user entered element 
     {
System.out.println("this" +searchele+" is at position"+(sizea+1));
break;
}
}
if(sizea==value)
       {
System.out.println(searchele+" not in this array");
}
}

}

Output

java linear search output not found
linear search search

switch case in java

How to use switch case statement in java with example, switch case statement is used when there are multiple condition in java

Syntax

switch(Variable){
 case value:
     code statement;
  break;
 case value:
     code statement;
  break;
default:
     delault value not in above cases;
}

Example 
Write a program which allows user to enter a integer from todays day, and user get to know which day of week it will be after certain days (consider today as sunday,0 for sunday )Today is day 0.

Program

import java.util.Scanner;// this package util and Class Scanner is declared to take input from user
/*
*@ scanner class
* @author khant vyas
*/
public class weekdays // name of class
{

public static void main(String args[])
{
int a,b;// a is variable to store summation of given numbers

System.out.println("Enter the number of days from today and will tell you which day will be after thpse days");
System.out.println("(consider today as sunday)");
Scanner sc = new Scanner(System.in);// sc is object to the Scanner class
a=sc.nextInt();// nextInt() method is used accept intiger value from user
b=a%7; // b stores value by dividing a by 7 to sort day

   switch(b)
     {
case 0:
System.out.println("Sunday");
break;
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;

   }
 }
}

Output:

java switch case
Switch case java



do while in java

Do-while loop in jave is used to run a specific code statement for first time when you run the program and then next time will ask for condition.
syntex

do{
statement(s);
-----
}while(expression);

Example 
Write a program to get summation of values entered by programmer untill user enter's 0.(0 will terminate program)

import java.util.Scanner; // this package util and Class Scanner is declared to take input from user
/*
*@ scanner class
* @author khant vyas
*/
public class conditional // name of class
 {

public static void main(String[] args) 
{
float number,sum=0;// sum is variable to store summation of given numbers
System.out.println("enter the numbers you wan to sum");
System.out.println("enter 0 to terminate");
Scanner sc = new Scanner(System.in);
do {

number= sc.nextFloat();//number stores value from user
 sum =number+sum; // sum add numbers and return summation of all given numbers
}while(number!=0); // when user enters 0 , program termiates loop
 
System.out.println("Summation is "+sum);
}


}

Output
do while in java image not found
do while in java