Skip to main content

Posts

Showing posts from August, 2016

Difference between Abstract Class and Interface

Difference between Abstract Class and Interface
Difference between abstract class and interface ?

Abstract Class:-

1) Can have abstract and non-abstract method both.
2) One class can extend at most one class.
3) can declare non abstract method and also can define body for it.


Interface:-
1) can have only abstract method.
2) One class can implement multiple interfaces.
3) can't delare body to a method.

Please look below example for more understanding:-



package Basics;publicabstractclassAnimal{int head;//no body (unimplemented function need to override in child class as a//compulsionabstractvoidsetLegs();//having body//may override or may notvoidsetHead(){ head=1;}}package Basics;// can have only methods and by default all are abstract need to be override//by implementing class, unlike abstract no method can havedefinitionpublicinterfacePet{voidtellWhereAreYouFrom(String place); String tellYourWeight(String weight);}package Basics;// can extend one class but can implement multiple interf…

basics of java in layman term for beginner

Basics of java in layman term for beginner
What is an object ?

object is a real world entity.

Object is an instance of the class.It means something that is alive. Let us take an example of CAR:-

concept of a car is class. I mean we all know that it is having four tyres , one engine , four doors , a seat e.t.c. but we can't do anything just by the concept we need real Car to perform operations on it . This real car is what we call is an object.


Explain about main()method in java ?

main() method is an entry point of a program.
public static void main(String[] args)

Why main()method is public, static and void in java ?

public: It can be accessed from anywhere.
static: no class object need to call this method.
void: it is not returning anything in back.

What is JIT compiler ?

JIT is just in time compiler.JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it's called) into a form that's usu…

Shortest Path from source to Vertex :- Dijkstra Algorithm

Shortest Path from source to Vertex :- Dijkstra Algorithm:-

Dijkstra Algorithms is an algorithm use to find the shortest path from source vertex to a given vertex.



package Graph;importjava.util.HashMap;abstractpublicclassDirectedGraph{ Vertex[] vertexlist=new Vertex[10]; HashMap<Character,HashMap<Character,Integer>> edgelist=new HashMap<>(); Vertex vertex;//count of vertex and edgestaticint vertexcount=0;int edgecount=0;/* * This function takes a label and insert in the vertex list as well as edge list since it is new vertex it will add * null to its adjoining vertices */intaddVertex(char label){ vertex=new Vertex(label); vertexlist[vertexcount]=vertex; System.out.println(vertexlist[vertexcount].label); edgelist.put(vertex.label,null); vertexcount++;return vertexcount;}intaddEdge(char label,char[] labels,int[] distances){ HashMap<Character,Integer> vertexlist=new HashMap<>();for(int i=0;i<labels.length;i++){ vertexlist.put(labels[i],…

Breadth First Search Implementation in Java

Breadth First Search Implementation in Java
For Graph Class Design and DFS implementation please refer to below link:-

depth-first-search-implementation-in-java


/*1. Pick a vertex.2. Get Adjacent Vertex.3. Insert in queue ( at tail)4. Remove vertex from queue( head)5. Do until no vertex left in a queue.*/ voidsearchBFS(){//this is how we define queue using LinkedList Queue<Character> queue=new LinkedList<Character>(); queue.add(vertexlist[0].label);while(!queue.isEmpty()){ //element at head (Function in main graph class please use above link) ArrayList adjvertex=getNeighbours(queue.peek()); System.out.println(queue.peek()); //element removed at head queue.remove();if(adjvertex!=null){for(int i=0;i<adjvertex.size();i++){ //element added at tail queue.add((Character) adjvertex.get(i));}}}}

Depth First Search Implementation in Graph

Depth First Search Implementation in Graph


--Graph //This is a class to define the vertex in a graphpublicclassVertex{char label;boolean visited;publicVertex(char label){this.label=label;this.visited=false;}package Graph;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;importjava.util.Map.Entry;// this class is abstract because we won't allow you make instance of it , it is of no use until not extendedabstractpublicclassGraphBase{//declaring instance of vertex Vertex vertex;//declaring an array of vertex to hold all the vertexstatic Vertex[] vertexlist=new Vertex[10];//declaring edgelist to hold all edges key is vertex value is neighbouring vertexstatic HashMap<Character,ArrayList<Character>> edgelist=new HashMap<>();//count of vertex and edgestaticint vertexcount=0;int edgecount=0;/* * This function takes a label and insert in the vertex list as well as edge list since it is new vertex it will add * null to its ad…

.