Friday, May 13, 2016

Java Introduction

Introduction of Java

Java is a powerful tool for internet development projects. It is a simplified version of C++. According to Sun Microsystems, Java is a simple, object-oriented, statically typed, complied, architecture neutral, multi-threaded, garbage collected, robust, secure, and extensible.
Java can be used to create two types of programs:
-applets applications
-standalone applications

Below shown are some of the important things to keep in mind about Java program:
• Case Sensitivity - Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.
• Class Names - For all class names, the first letter should be in Upper Case.   If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.  Example class MyFirstJavaClass
 • Method Names - All method names should start with a Lower Case letter.   If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.  Example public void myMethodName()
• Program File Name - Name of the program file should exactly match the class name.   When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to the end of the name (if the file name and the class name do not match your program will not compile).  Example : Assume 'MyFirstJavaProgram' is the class name, then the file should be saved as'MyFirstJavaProgram.java'
public static void main(String args[]) - Java program processing starts from the main() method, which is a mandatory part of every Java program.


OOP(Object Oriented Programming)

It enables us to think of program elements as objects. It is just a way to hide the details of program. The main concepts that are the backbones of OOP should  be understood to learn about OOP. They are
1.       Encapsulation
2.       Inheritance
3.       Polymorphism

Java Development Kit(JDK)

We needs to get a recent copy of the Java JDK in order to get started in Java programming. This can be obtained for free by downloading it from the Sun Microsystems website
In order to program for java, there are certain IDEs that helps us to work easily for programming. Some of the IDEs are as follows:
You can use any one of these IDEs.


First Java Program
public class Example1 {
public static void main(String args []) {
System.out.println("Hello this is my first java program");
}
}

Run the above program and the output will be :


OUTPUT:
Hello this is my first java program


example for getset array

Here i am going to show you the use of get set array in java.


package setgetarray;
import java.util.Scanner;
public class SetGetArray {

    int i;

    Scanner sc= new Scanner(toString());
    int rollNo[]=new int[2];
    float marks[] = new float[2];
    String info[]=new String[2];

    public void setInfo(){

        System.out.println("Enter the student no:");
        //this.n= sc.nextInt();
        for (i=0;i<2;i++){
            System.out.println("student roll:");
            sc.nextInt();
            System.out.println("student marks");
            sc.nextFloat();
            System.out.println("student info");
            sc.nextLine();
        }

    }
    public void getInfo(){
        System.out.println("student info:");
        for(i=0;i<2;i++){

            System.out.println("student roll:"+rollNo);
            System.out.println("student marks"+marks);
            System.out.println("student info"+info);
        }

    }
}

Thursday, May 12, 2016

Example of array

Simple example of array is shown below:


package monthactual;

import java.util.Scanner;

public class MonthActual {
    int n;
    String[] month= { "january","february","march","april","may","june","july","august","september","october","november","december"};
    Scanner sc=new Scanner();
    public void actualMonth(){
        for (int i=0;i<month.length; i++){
            System.out.println("Enter the no of month:");
            this.n=sc.nextInt();
            System.out.println("the actual month is" +month[n]);

        }
    }
}

Tuesday, May 10, 2016

java functions

java Functions are most strong to handle the operations with global variables and arguments passed to it. the variables defined inside the java functions are always local and  are only available inside function only. Function can return any value of the class type. We can consider data type int, float, String etc as a Class type. In function one can return Object typed java.
Here is one simple function illustrated for concept of java array.
 public void setStudentArray(){
        for(int i=0;i<num;i++){

            HasStudent student=new HasStudent();
            System.out.println("======***********========");
            student.name=setName();
            student.Address=setAddress();
            student.rollNo=setRoll();
            stdArray[i]=student;
            System.out.println("======***********========");

        }
    }
In this function there is no return type and no argument. only local and global variables available inside the functions are processed with this function and are available when callng the function

we can declare function with the return type as follows:

(Access_Specifier) (ClassName) functionName(arguments.
class definintions;
processing of local and global datas.
return(Object of class);
}


Thursday, March 26, 2015

LinkedHashMap

LinkedHashMap is a Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

Example:

import java.util.LinkedHashMap;
import java.util.Set;
import java.util.Iterator;
import java.util.Map;
public class LinkedHashMapDemo {
    public static void main(String args[]) {
         // HashMap Declaration
         LinkedHashMap<Integer, String> lhmap = 
                 new LinkedHashMap<Integer, String>();

         //Adding elements to LinkedHashMap
         lhmap.put(22, "Abey");
         lhmap.put(33, "Dawn");
         lhmap.put(1, "Sherry");
         lhmap.put(2, "Karon");
         lhmap.put(100, "Jim");

         // Generating a Set of entries
         Set set = lhmap.entrySet();

         // Displaying elements of LinkedHashMap
         Iterator iterator = set.iterator();
         while(iterator.hasNext()) {
            Map.Entry me = (Map.Entry)iterator.next();
            System.out.print("Key is: "+ me.getKey() + 
                    "& Value is: "+me.getValue()+"\n");
         }
    }
}
Output:
Key is: 22& Value is: Abey
Key is: 33& Value is: Dawn
Key is: 1& Value is: Sherry
Key is: 2& Value is: Karon
Key is: 100& Value is: Jim

TreeMap

TreeMap is Red-Black tree based NavigableMap implementation. It is sorted according to the natural ordering of its keys.
TreeMap class implements Map interface similar to HashMap class. The main difference between them is that HashMap is an unordered collection while TreeMap is sorted in the ascending order of its keys. TreeMap is unsynchronized collection class which means it is not suitable for thread-safe operations until unless synchronized explicitly.

Example:

import java.util.TreeMap;
import java.util.Set;
import java.util.Iterator;
import java.util.Map;

public class Details {

   public static void main(String args[]) {

      /* This is how to declare TreeMap */
      TreeMap<Integer, String> tmap = 
             new TreeMap<Integer, String>();

      /*Adding elements to TreeMap*/
      tmap.put(1, "Data1");
      tmap.put(23, "Data2");
      tmap.put(70, "Data3");
      tmap.put(4, "Data4");
      tmap.put(2, "Data5");

      /* Display content using Iterator*/
      Set set = tmap.entrySet();
      Iterator iterator = set.iterator();
      while(iterator.hasNext()) {
         Map.Entry mentry = (Map.Entry)iterator.next();
         System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
         System.out.println(mentry.getValue());
      }

   }
}
Output:
key is: 1 & Value is: Data1
key is: 2 & Value is: Data5
key is: 4 & Value is: Data4
key is: 23 & Value is: Data2
key is: 70 & Value is: Data3

TreeSet

TreeSet is similar to HashSet except that it sorts the elements in the ascending order while HashSet doesn’t maintain any order. TreeSet allows null element but like HashSet it doesn’t allow. Like most of the other collection classes this class is also not synchronized, however it can be synchronized explicitly like this: 
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));


Example:


import java.util.TreeSet;
public class TreeSetExample {
     public static void main(String args[]) {
         // TreeSet of String Type
         TreeSet<String> tset = new TreeSet<String>();

         // Adding elements to TreeSet<String>
         tset.add("ABC");
         tset.add("String");
         tset.add("Test");
         tset.add("Pen");
         tset.add("Ink");
         tset.add("Jack");

         //Displaying TreeSet
         System.out.println(tset);

         // TreeSet of Integer Type
         TreeSet<Integer> tset2 = new TreeSet<Integer>();

         // Adding elements to TreeSet<Integer>
         tset2.add(88);
         tset2.add(7);
         tset2.add(101);
         tset2.add(0);
         tset2.add(3);
         tset2.add(222);
         System.out.println(tset2);
    }
 }
Output: You can see both the TreeSet have been sorted in ascending order implicitly.

[ABC, Ink, Jack, Pen, String, Test]
[0, 3, 7, 88, 101, 222]

Hashmap

HashMap is a Map based collection class that is used for storing Key & value pairs. This class makes no guarantees as to the order of the map. It is similar to the Hashtable class except that it is unsynchronized and permits nulls(null values and null key).

HashMap maintains key and value pairs and often denoted as HashMap<Key, Value> or HashMap<K, V>. HashMap implements Map interface. HashMap is similar to Hashtable with two exceptions – HashMap methods are unsynchornized and it allows null key and null values unlike Hashtable. It is used for maintaining key and value mapping.

It is not an ordered collection which means it does not return the keys and values in the same order in which they have been inserted into the HashMap. It neither does any kind of sorting to the stored keys and Values. You must need to import java.util.HashMap or its super class in order to use the HashMap class and methods.

Example:

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class Details {

   public static void main(String args[]) {

      /* This is how to declare HashMap */
      HashMap<Integer, String> hmap = new HashMap<Integer, String>();

      /*Adding elements to HashMap*/
      hmap.put(12, "Nabin");
      hmap.put(2, "Rabin");
      hmap.put(7, "Sabin");
      hmap.put(49, "Rashmi");
      hmap.put(3, "Kripa");

      /* Display content using Iterator*/
      Set set = hmap.entrySet();
      Iterator iterator = set.iterator();
      while(iterator.hasNext()) {
         Map.Entry mentry = (Map.Entry)iterator.next();
         System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
         System.out.println(mentry.getValue());
      }

      /* Get values based on key*/
      String var= hmap.get(2);
      System.out.println("Value at index 2 is: "+var);

      /* Remove values based on key*/
      hmap.remove(3);
      System.out.println("Map key and values after removal:");
      Set set2 = hmap.entrySet();
      Iterator iterator2 = set2.iterator();
      while(iterator2.hasNext()) {
          Map.Entry mentry2 = (Map.Entry)iterator2.next();
          System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
          System.out.println(mentry2.getValue());
       }

   }
}
Output:
key is: 49 & Value is: Rashmi
key is: 2 & Value is: Rabin
key is: 3 & Value is: Kripa
key is: 7 & Value is: Sabin
key is: 12 & Value is: Nabin
Value at index 2 is: Rabin
Map key and values after removal:
Key is: 49 & Value is: Rashmi
Key is: 2 & Value is: Rabin
Key is: 7 & Value is: Sabin
Key is: 12 & Value is: Nabin