2016-07-26

Java restful web services with Spring, jersey, JpaRepository, maven, angularjs and oracle

Create Web Project from Maven:

  Open CMD > Type the commend

mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} - DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false 

example :
 mvn archetype:generate -DgroupId=com.Webapp -DartifactId=WebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

2016-07-22

Hashmap capacity, load factor and fail-fast

Capacity :

  •     Capacity is the Number of element HashMap can contain. 
  •     initial capacity is the capacity at the time the hash table is created 
  •     default initial capacity (16) 
  •     capacity increased (2 power n) , where n- number of elements
load factor :

  • Defines threshold of HashMap. When re-sizing will occur of HashMap.
  • The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
  • default load factor (.75)
fail-fast :


What is the difference between hashmap, hashtable and hashset

Hashmap:

  • Hash table implements Map interface
  • Hash table permits null values and the null key
  • HashMap class is equivalent to Hashtable, except that it is unsynchronized and permits nulls
  • It does not guarantee that the order will remain constant over time
  • constant-time performance for the operations (get, put and remove)
  •  Time complexity - O(1) for get, put and remove
                (Learn About capacity , load factor and fail-fast)
  • It is not synchronized
  • How to make Hashmap Synchronize
                 Map m = Collections.synchronizedMap(new HashMap(...));
  • Exapmle:
                   import java.util.*;
                   public class HashMapEx{

2016-07-19

Understanding final in java

Java has a final keyword that we can use it in three places.
Final variable
When you use final with a variable, it creates a constant whose value can’t be changed once it has been initialized. Similarly final can be used to  create final methods and final classes.
For Example:
final int i=10;
final File f=new File();
final ClassName;

Final methods
A final method is a method that can’t be overridden by a subclass. To  create a final method, you simply add the keyword final to the method declaration.
For example:
public class SpaceShip
{
public final int getVelocity()
{
return this.velocity;
}
}

Merge Sort Java Program


  1. import java.util.Scanner;
  2.  

  3. public class MergeSort 
  4. {
  5.     
  6.     public static void sort(int[] a, int low, int high) 
  7.     {
  8.         int N = high - low;         
  9.         if (N <= 1) 
  10.             return; 
  11.         int mid = low + N/2; 
  12.         // recursively sort 
  13.         sort(a, low, mid); 
  14.         sort(a, mid, high); 
  15.         // merging two sorted sub-arrays
  16.         int[] temp = new int[N];
  17.         int i = low, j = mid;
  18.         for (int k = 0; k < N; k++) 
  19.         {
  20.             if (i == mid)  
  21.                 temp[k] = a[j++];
  22.             else if (j == high) 
  23.                 temp[k] = a[i++];
  24.             else if (a[j]<a[i]) 
  25.                 temp[k] = a[j++];
  26.             else 
  27.                 temp[k] = a[i++];
  28.         }    
  29.         for (int k = 0; k < N; k++) 
  30.             a[low + k] = temp[k];         
  31.     }

2016-07-18

write a program to find sum of integers from a string


  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;


  3. public class GetSum {

  4. public static void main(String[] args) {
  5. String s = "1ima7 j1 1hh1  0ttr1dfg10";
  6. Pattern p = Pattern.compile("[0-9]");
  7. Matcher m = p.matcher(s);
  8. int sum = 0;

  9. while (m.find()) {
  10. sum += Integer.parseInt(m.group());
  11. }
  12. System.out.println(sum);
  13. }

Inheritance in java

Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object.
Benefits of inheritance

  • reuse of methods 
  • reuse of fields of parent class 
  • We can add new methods and fields
Syntax

  1. public class className extends ParentclassName  
  2. {  
  3.    //methods and fields  
  4. }  

Polymorphism in Java

Polymorphism (from the Greek meaning "having multiple forms")
Polymorphism means one thing in many form. an object to have more than one form. Polymorphism is capability of one object to behave in multiple ways.

There are following types of polymorphism :

Static polymorphism (compile time) function overloading :-
     Method Overloading is a feature that allows more than two methods having same name in a class with different parameters.

Example:

  1. public class Calculator{  
  2.   void add(int a,int b)
  3.   {
  4.   System.out.println(a+b);
  5.   }  
  6.   void add(int a,int b,int c)
  7.   {
  8.   System.out.println(a+b+c);
  9.   }  
  10.   

How to convert .class file to .java file

Step 1: Download Jad Link here JAD (UnZip it)

Step 2: Set environment variable.
           

Data abstraction

Abstraction is the process is used to hide certain details and only show the essential features of the object.
It is class level design that hiding the complexity of the implementation and offered by an API / design / system, in a sense simplifying the 'interface' to access the underlying implementation.


Example:


  1. public class Employee
  2. {
  3.    private String name;
  4.    private String address;
  5.    private int sal;
  6.    public Employee(String name, String address, int sal)
  7.    {
  8.       this.name = name;
  9.       this.address = address;
  10.       this.sal = sal;
  11.    }

Java Custom Exceptions

Creating Custom Exceptions
Here we are going to learn how to create your own exception.

The Throwable hierarchy
As you know, you use the try/catch statement to catch exceptions, and
the throw statement to throw exceptions. Each type of exception that can
be caught or thrown is represented by a different exception class. What you
might not have realized is that those exception classes use a fairly complex
inheritance chain, as shown in Figure :

Java Encapsulation

Encapsulation
       Encapsulation is a mechanism of binding the data and function into a single unit. Encapsulation essentially has both information hiding and implementation hiding. Encapsulation provides a way for abstraction.

Example:

class Encapsulation
{
    //Restrict direct access, hiding Data
    private int a;
    private int b;
    
    public void setA(int a){
     this.a=a;
   }
     public void setB(int b){
       this.b=b;
   }
    //hiding implementation
    public int sum(){
        int total=a+b;
        //  Other Implementation
        return total;
    }
}

Java Class concept

Class
       A class is a blueprint or prototype from which objects are created. Class that contains data type, Object, function methods, properties, Classes and constructor.
Example:
              For example our college, Our college contain different different classes and classes contain student.
Programming Language Example:

class MyClass{
    String name= "";
    String address= "";
    int age= 0;

    void setName(String name) {
         this.name = name;
    }

    void setAddress(String address) {
         this.address = address;
    }

    void setAge(int age) {
         this.age = age;
    }

    void print() {
         System.out.println(name +" "+Address+" "+age);
    }
}


Java Object Concept

 Object
       Object is representative of the class and is responsible for memory allocation of its data members and member functions.An object is a real world entity having attributes (State / data type) and behaviors (functions). An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object behavior.
Example:
              In our class room, All students are an individual Object. They have different
              name and color.
Programming Language Example:  All are the Different different object
int a=10;
float b1=20.10f;
String s1="Name";
MyClass x;

2016-07-11

JavaScript Data Types

 JavaScript Data Types

Following are the javascript data type :
            String, Number, Boolean, Array, Object.

Example:

Number
var length = 10;            

String                                      
var Name = "Soma";    

Array                                    
var country = ["India", "China", "USA"];    

 
Object / JSON   
var name = {firstName:"Nikita", lastName:"Camon"};  

Object oriented programming concepts (OOPs)

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects". Re-usability is the main concept of Oops. There are few major principles of object-oriented paradigm. If any programming Language supports this principles that programming language called Object-oriented programming.

Below are object oriented programming concepts :

1. Object
       Read More about Object  
 http://theprogrammersfirst.blogspot.in/2016/07/java-object-concept.html

2016-07-08

Java pagination Using webservices, Spring, JPARepository, jersey, Oracle and Angularjs Grid

Dijkstra's algorithm : graph Shortest Path implementation using java

Input Format

The first line contains T, denoting the number of test cases.
First line of each test case has two integers N, denoting the number of nodes in the graph and M, denoting the number of edges in the graph.

The next M  lines each consist of three space-separated integers x,y,r , where x and y denote the two nodes between which the undirected edge exists, r denotes the length of edge between these corresponding nodes.

The last line has an integer , S denoting the starting position.


Example:
1
4 4
1 2 24
1 4 20
3 1 3
4 3 12
1

2016-07-07

How to change jdk version in intellij

Open Iintellij


Goto >>   File > Project Structure

Binary Search Tree : Lowest Common Ancestor


  1.  /* Node is defined as :
  2.  class Node 
  3.     int data;
  4.     Node left;
  5.     Node right;
  6.     
  7.     */

  8. static Node lca(Node root,int v1,int v2)
  9.     {

  10.        //Decide if you have to call rekursively
  11.     //Samller than both
  12.     if(root.data < v1 && root.data < v2){
  13.         return lca(root.right,v1,v2);
  14.     }
  15.     //Bigger than both
  16.     if(root.data > v1 && root.data > v2){
  17.         return lca(root.left,v1,v2);
  18.     }

Swapping Nodes algorithm Implementation using java (binary tree)

Swapping subtrees of a node means that if initially node has left subtree L and right subtree R, then after swapping left subtree will be R and right subtree L.



  1. import java.io.*;
  2. import java.util.*;

  3. public class Solution {