Saturday, March 12, 2016

How to create LinkList in java

Linked list is data Structure which contain linear collection of data elements as Node in such way that every node refer to next node in List.Here in java let us create class node which contain integer data and reference to the next node in Link list.

This is class Node which is in file Node.java

public class Node
{
protected int data;
    protected Node link;

    //  Constructor to class Node with initially refer to null and set data to 0 by default
    public Node()
    {
        link = null;
        data = 0;
    }  
    //  Constructor to class Node which has both data and reference to next node
    public Node(int d,Node n)
    {
        data = d;
        link = n;
    }  
    //  Function that set link to next Node (which refer to next node
    public void setLink(Node n)
    {
        link = n;
    }  
    //  Function which assign integer  data to current Node
    public void setData(int d)
    {
        data = d;
    }  
    /*  Function to get link to next node  */
    public Node getLink()
    {
        return link;
    }  
    /*  Function to get data from current Node  */
    public int getData()
    {
        return data;
    }
}


Class LinkedList  in which  methods involved in it. Linkedlist.java file

import java.util.Scanner;

public class Linkedlist
{
protected Node start;
   protected Node end ;
   public int size ;
public Scanner sc;

   /*  Constructor  */
   public Linkedlist()
   {
       start = null;
       end = null;
       size = 0;
   }
   /*  Function to check if list is empty  */
   public boolean isEmpty()
   {
       return start == null;
   }
   /*  Function to get size of list  */
   public int getSize()
   {
       return size;
   }  
   /*  Function to insert an element at begining  */
   public void insertAtStart(int val)
   {
       Node nptr = new Node(val, null);  
       size++ ;  
       if(start == null)
       {
           start = nptr;
           end = start;
       }
       else
       {
           nptr.setLink(start);
           start = nptr;
       }
   }
   /*  Function to insert an element at end  */
   public void insertAtEnd(int val)
   {
       Node nptr = new Node(val,null);  
       size++ ;  
       if(start == null)
       {
           start = nptr;
           end = start;
       }
       else
       {
           end.setLink(nptr);
           end = nptr;
       }
   }
   /*  Function to insert an element at position  */
   public void insertAtPos(int val , int pos)
   {
       Node nptr = new Node(val, null);              
       Node ptr = start;
       pos = pos - 1 ;
       for (int i = 1; i < size; i++)
       {
           if (i == pos)
           {
               Node tmp = ptr.getLink() ;
               ptr.setLink(nptr);
               nptr.setLink(tmp);
               break;
           }
           ptr = ptr.getLink();
       }
       size++ ;
   }
   /*  Function to delete an element at position  */
   public void deleteAtPos(int pos)
   {      
       if (pos == 1)
       {
           start = start.getLink();
           size--;
           return ;
       }
       if (pos == size)
       {
           Node s = start;
           Node t = start;
           while (s != end)
           {
               t = s;
               s = s.getLink();
           }
           end = t;
           end.setLink(null);
           size --;
           return;
       }
       Node ptr = start;
       pos = pos - 1 ;
       for (int i = 1; i < size - 1; i++)
       {
           if (i == pos)
           {
               Node tmp = ptr.getLink();
               tmp = tmp.getLink();
               ptr.setLink(tmp);
               break;
           }
           ptr = ptr.getLink();
       }
       size-- ;
}
public void reverse(){

Linkedlist newList = new Linkedlist();
Node node = start;
for(int i=0; i newList.insertAtStart(node.getData());
node = node.getLink();
}
//Update the start and end of the original list
start = newList.start;
end = newList.end;
}

public void findelement(){

System.out.println("enter a element u wanted to know position");
Linkedlist l = new Linkedlist();
Node n = start;
sc = new Scanner(System.in);
int i = sc.nextInt();
int a=1;

while(i!=n.getData()){
a++;
if(n.getLink() == null)
{
System.out.println("Count not find element " + i + " in the list");
return;
}
n=n.getLink();
}
System.out.println("index is " +a);
}

public void display(){
System.out.println("single linked list is +  ");
if(size==0){
System.out.println("empty linklist");
}

if(start.getLink()==null){
System.out.println(start.getData() );
        return;
}
Node ptr = start;
    System.out.print(start.getData()+ "->");
    ptr = start.getLink();
    while (ptr.getLink() != null)
    {
        System.out.print(ptr.getData()+ "->");
        ptr = ptr.getLink();
    }
    System.out.print(ptr.getData()+ "\n");
}
}

This is a main program which involve  execution of main method and calls the method of Linkedlist class.

import java.util.Scanner;
public class singlelist {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Linkedlist list = new Linkedlist();
System.out.println("Single linkedlist display");
char ch;
do
       {
           System.out.println("\nSingly Linked List Operations\n");
           System.out.println("1. insert at begining");
           System.out.println("2. insert at end");
           System.out.println("3. insert at position");
           System.out.println("4. delete at position");
           System.out.println("5. check empty");
           System.out.println("6. get size");
           System.out.println("7. reverse list");
           System.out.println("8. tacke element");
           int choice = sc.nextInt();            
           switch (choice)
           {
           case 1 : 
               System.out.println("Enter integer element to insert");
               list.insertAtStart( sc.nextInt() );                     
               break;                          
           case 2 : 
               System.out.println("Enter integer element to insert");
               list.insertAtEnd( sc.nextInt() );                     
               break;                         
           case 3 : 
               System.out.println("Enter integer element to insert");
               int num = sc.nextInt() ;
               System.out.println("Enter position");
               int pos = sc.nextInt() ;
               if (pos <= 1 || pos > list.getSize() )
                   System.out.println("Invalid position\n");
               else
                   list.insertAtPos(num,pos);
               break;                                          
           case 4 : 
               System.out.println("Enter position");
               int p = sc.nextInt() ;
               if (p < 1 || p > list.getSize() )
                   System.out.println("Invalid position\n");
               else
                   list.deleteAtPos(p);
               break;
           case 5 : 
               System.out.println("Empty status = "+ list.isEmpty());
               break;                   
           case 6 : 
               System.out.println("Size = "+ list.getSize() +" \n");
               break;                         
           case 7 :
            list.reverse();
            System.out.println("reverse linked list = " + list);
            break;
           case 8:
            list.findelement();
            break;
           default : 
               System.out.println("Wrong Entry \n ");
               break;   
           }
           /*  Display List  */ 
           list.display();
           System.out.println("\nDo you want to continue (Type y or n) \n");
           ch = sc.next().charAt(0);                        
       } while (ch == 'Y'|| ch == 'y'); 
}







Sorting an array with N2 (insertion sort Algorithm)

Here is program which allow you to sort an array with  complexity of (n square) in ascending order Here is the program: here it uses 2 for loop to compare
Example:- Program

import java.util.Scanner;
public class inertsort {

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 intsort[] = new int[i];
System.out.println("enter element s");
for(int a=0;a intsort[a]=sc.nextInt();
}
System.out.println("Elements in array are ");
for(int a=0;a System.out.print(" "+intsort[a]);
}

for(int m =1;m int temp;
for(int n=m;n>0;n--){
{
if(intsort[n] temp=intsort[n];
intsort[n]=intsort[n-1];
intsort[n-1]=temp;
}
}
}
}
System.out.println("Elements in array are ");
for(int a=0;a System.out.print(" "+intsort[a]);
}

}
}

Output:
enter how much element u want in array 
5
enter element s
100
77
55
85
91
Elements in array are 
 100 77 55 85 91Elements in array are 
 55 77 85 91 100

how to define class and object in Ruby

        Hello friends as Ruby is pure object oriented language , How to define class in ruby and how to access the attributes is very import to learn.
Example lets consider Car class in ruby and speed, color and model as its attributes:
Example : code

irb(main):001:0> class Car
irb(main):002:1> attr_accessor :model,:color,:speed
irb(main):003:1> end

Here you can see using class keyword in ruby we can make class and all contain of class lies between class and end key word.
Car is name of class, and you can notice it nil  keyword shown by ruby interpreter. when class do not return anything nil is returned by it.








How to create object of class in ruby and how to use inheritance with ruby is very simple 
Example :code

irb(main):001:0> class Car
irb(main):002:1> attr_accessor :model,:company,:color
irb(main):003:1> end
=> nil
irb(main):004:0> c1=Car.new
=> #
irb(main):005:0> c1.model="swift"
=> "swift"
irb(main):006:0> c1.color="red"
=> "red"
irb(main):007:0> c1.company="maruti"
=> "maruti"
irb(main):008:0>
Object in ruby

here you can notice that c1 is object of class Car, so new keyword after class keyword with use of .(dot) 
c1 is object of Car class here is how we access the attributes of class.
attr_accessor is keyword used to declare the access to the attributes of class.







     Let see if we inherit property of Car class in Sport_car class: code

irb(main):008:0> class Sport_car < Car
irb(main):009:1> attr_accessor :topspeed
irb(main):010:1> end
=> nil
irb(main):011:0> s1=Sport_car.new
=> #
irb(main):012:0> s1.color="black"
=> "black"
irb(main):013:0> s1.topspeed=500
=> 500
irb(main):014:0>
OOps concept in Ruby

Here we can see with just use "<" and parent class Car we can inherit attributes of Car class in Sport_car class, and also we can add new attributes such as topspeed in sport_car class

Basics Operation with Ruby

Hello friends Ruby is very easy and  intelligent language when you do all operation easily. All thing in Ruby is considered as Class and object. Ruby is pure object oriented language. As you write addition operation in ruby it gives you result directly.
 Example:
write 1+100 you will get 101 on next line

irb(main):001:0> 1+100
=> 101

Here in above example 1 and 100 are treated as object and + is a method which is used to deal with this two objects.
1 and 100 are treated as an object of class Fixnum in ruby. there is a .class method in ruby which return class of an object so you can see it. 
Interactive Ruby

As you can see this example in Interactive Ruby tutorial.and find out class of ruby.













         Now do you know how ruby deals with string and how it is responding when you put +  between two  strings.Example

irb(main):001:0> "shree" + "ram"
=> "shreeram"
String concatenation in ruby
Here you can see adding two sting "shree" and "ram"  you get concatenation of this two strings. but when you add a string and number ruby interpreter gives you error for implicit conversion.
        This clearly state that you can either add two numbers or concatenate  2 strings.








Friday, March 11, 2016

Downloading Ruby and Install

http://www.ruby-lang.org/en/downloads/  just click here and you will be directed to page then download version of ruby you wanted to install.
Install Ruby











Click this to Download
Install ruby


















Click here to Install and give CMD of Ruby 


















This is interactive ruby where you get way to program 



Big Data overview and keywords

What is Big Data ?
Big Data is collection of large data-sets which cannot be processed using traditional data handling techniques.
What big data includes? 
It includes data from Black box data, Stock exchange data, Social media data, Power grid data, Transport data, Search engine data, etc.
What are big data challenges?
Big data challenges include, capturing data, transfer of data, storage, searching of data, sharing, analysis and presentation.
What is Mongo DB?
This is 4th most used database and its NoSQL, it is massively parallel processing database system.
MapReduce :- it divides task and assign it to computer connected.
Hadoop:- Belong to Apache where data is processed in parallel o different CPU nodes. It is capable of running Application on cluster of computers.
HDFS(Hadoop Distributed File System):-It uses Master Slave architecture, NameNode manages file system metadata. DataNode stores actual data.

There are few Big data provider in market which offers big data services :


Services offered by AWS  are many but few of then are as follows:

  1. Amazon public elastic compute cloud(EC2)
  2. Amazon Elastic Map Reduce(Hadoop)
  3. Amazon DynomoDB
  4. Amazon simple storage service(S3)
  5. Amazon High Performance computing
  6. Amazon Redshift
Services offered by google big data service:

  1. Google compute engine 
  2. Google Big Query 
  3. Google Prediction API


Thursday, March 10, 2016

How to add new repository to GitHub


  • Step 1 open Github and the login with you username and password https://github.com/ click this web site. then you will see following home page 
    GitHub page












  • Step 2 Click on create repository as highlighted in above image, now you will be directed towards new page.
    Repository in GitHub












  • Step 3 Give name to you repository and select the access modifier (private or public) and then click on ADD button gitignore and ADD button License to select it then click on create repository.
    Github not displayed












Github
  • Finally repository will be created and you will see following page 

how to get start with GitHub and How to install it

         Hello friends its very useful to work with Github and post project on it, it provide you single time update for small components in your project from PC to Server.
Go to you web-browser and open Github Click here.then to start with git hub Click on Signup->

       Choose username and your email then click signup and you account will be created, choose
free if you want to submit public projects.

      So you will be directed to this page so now click finish sign up and select plan if you prefer
 private project. then click to http://git-scm.com/download and you will see download page so now download Github.
Select your operating system and click there where marked

      As setup is download the run installation by clicking the next button


          This was you will have  2 icons Git CMD and Git GUI you need, GitCMD is used to run and maintain repository on GitHub.
 
         This is Git CMD so can execute your command in this window.I use microsoft windows so I get this GIT CMD but you may get different GIT CMD as your Operating System.

Saturday, February 27, 2016

Learn Java in hindi very easy way via Videos

Basic in java Writing a hello world program in java 


 

Java String in Hindi 


Static variable in java in hindi 


How to prepare Resume for Usa in Computer Science

Facing a coding interview in USA is lot different than compared to other country.It include few skills sets and easy flexible procedure for every individual.

Few key procedures to keep in mind while facing an interview:
  • Ask Your interviewer a question regarding problem.
  • Find an optimum way to solve the problem and with lesser complexity(time and space).
  • Write a pseudo code for your program before writing it.
  • Try to fix bugs in your program systematically and correct the bugs.
  • it requires coding skills, aptitude and good personality.
  • Use Situation action Result module to answer question. 
  •  Most of people makes mistake in interview so don’t worry on any of mistake you make 
Types of Question generally asked in freshers interview are as follows:
  • Matrix problem, Reverse Linked list, Array manipulation.
  • Algorithm like , merge sort, insertion sort, recursion, bit manipulation. 
  •  How to find time and space complexity.
  • question related to other data structure and other mathematical problem.
If you wish to practice for interview then practice some question from CareerCup.

Some more type of logical Question for interview for experienced individual.
  • Product design question need to be handled with more detail as asking of user.
  • problem solving skill, how break problem in components.
  • How to reduce ambiguity? Sanity check.
  • How to solve scalablity issue?
  • Everyone makes mistake in interview but they compare with other candidates.
How to make a good resume to get interview call? 
  • Make your resume short to just 1 page or maximum 2.
  • Put your important projects on your resume, and job role in experience.
  • Mention details of your projects, be specific about what was your role in project.
  • Mention algorithm you implemented,how you optimize it?
  • Certification are attractive if you put on resume.
Resource to get resume demo
If you really have time then please watch this video

String In java

What is String Class in Java ?

  • String class represent character string. All string in  java implemented as instance of class.
  • String class is an Immutable java class It can not be modified and it uses string table.
  • String class in java is one which is used most.
There are various methods in string class which are used to manipulate string. String Class possess eleven constructor which allow you to manipulate string.

Simple way to create string in java.

String s="Welcome to my Blog"; //String s contains Welcome to my Blog 

Few methods of String Class are as follows:-
  • Char CharAt(int index) :- Returns a character at specified index.
  • int indexOf(char ch):- Returns index of first occurrence of  specified character.
  • int compareTo(object o):- compare string with another object.
  • int length():- Returns length of string.
  • String toLowerCase(): Converts all the character of string to lower case.
String example: Some methods of string class:

If you have More time there is a video  you can look and learn from it 

For  video in Hind  language click here

Program:
package xyz;
public class stringexp {

public static void main(String[] args) {
String s1="Welcome to Java";
  String s2 = s1;
String s3 = new String("Welcome to Java");
String s4 = "Welcome to Java";
System.out.println("this are four strings"+"\n"+s1+"\n"+s2+"\n"+s3+"\n"+s4);
System.out.println("s1==s2:"+s1==s2);
 System.out.println("s2==s3:"+s2==s3);
System.out.println("s1.equals(s2):"+s1.equals(s2));
System.out.println("s2.equals(s3):"+s2.equals(s3));
System.out.println(" s1.compareTo(s2): " +s1.compareTo(s2));
System.out.println("s2.compareTo(s3):" +s2.compareTo(s3));
System.out.println("s1 == s4:"+s1 == s4 );
System.out.println("character at 0:"+s1.charAt(0));
System.out.println("index of string j:"+s1.indexOf('J'));
System.out.println("index of string to:"+s1.indexOf("to"));
System.out.println("last index of a:"+s1.lastIndexOf('a'));
System.out.println("last index of o:"+s1.lastIndexOf("o", 15));
System.out.println("lenth of string:"+s1.length());
System.out.println("from 5th character of string: "+s1.substring(5));
System.out.println("from 5th to 11th character:"+s1.substring(5, 11));
System.out.println("check if it start with wel :"+s1.startsWith("Wel"));
System.out.println("check if it ends with (java):"+s1.endsWith("Java"));
System.out.println("string to lowercase:"+s1.toLowerCase());
System.out.println("string to uppercase:"+s1.toUpperCase());
System.out.println("Welcome "+s1.trim());
System.out.println("replace o with T: "+s1.replace('o', 'T'));
System.out.println("replace all the 'o' by character 'T':"+s1.replaceAll("o", "T"));
System.out.println("replace only 1st 'o' with 'T':"+s1.replaceFirst("o", "T"));
char[] array = s1.toCharArray();
System.out.println(" convert sting to character : ");
for(int i=0;i {
     System.out.print(array[i]+" ");
}
  }

}


Output:

this are four strings
Welcome to Java
Welcome to Java
Welcome to Java
Welcome to Java
false
false
s1.equals(s2):true
s2.equals(s3):true
 s1.compareTo(s2): 0
s2.compareTo(s3):0
false
character at 0:W
index of string j:11
index of string to:8
last index of a:14
last index of o:9
lenth of string:15
from 5th character of string: me to Java
from 5th to 11th character:me to 
check if it start with wel :true
check if it ends with (java):true
string to lowercase:welcome to java
string to uppercase:WELCOME TO JAVA
Welcome Welcome to Java
replace o with T: WelcTme tT Java
replace all the 'o' by character 'T':WelcTme tT Java
replace only 1st 'o' with 'T':WelcTme to Java
 convert sting to character : 
W e l c o m e   t o   J a v a 





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