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 ; public abstract class Animal { int head ; //no body (unimplemented function need to override in child class as a //compulsion abstract void setLegs (); //having body //may override or may not void setHead () { 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 havedefinition public interface Pet { void tellWhereAreYouFr...

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-...

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 ; import java.util.HashMap ; abstract public class DirectedGraph { Vertex [] vertexlist = new Vertex [ 10 ]; HashMap < Character , HashMap < Character , Integer >> edgelist = new HashMap <>(); Vertex vertex ; //count of vertex and edge static int 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 */ int addVertex ( char label ) { vertex = new Vertex ( label ); vertexlist [ vertexcount ]= vertex ; System . out . println ( vertexlist [ vertexcount ]. label ); edgelist . put ( vertex . label , null ); vertexcount ++; return vertexcount ; } int addEdge ( char label , char ...

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. */ void searchBFS () { //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 ...

Depth First Search Implementation in Graph

Depth First Search Implementation in Graph -- Graph //This is a class to define the vertex in a graph public class Vertex { char label ; boolean visited ; public Vertex ( char label ){ this . label = label ; this . visited = false ; } package Graph ; import java.util.ArrayList ; import java.util.HashMap ; import java.util.Iterator ; import java.util.Map ; import java.util.Map.Entry ; // this class is abstract because we won't allow you make instance of it , it is of no use until not extended abstract public class GraphBase { //declaring instance of vertex Vertex vertex ; //declaring an array of vertex to hold all the vertex static Vertex [] vertexlist = new Vertex [ 10 ]; //declaring edgelist to hold all edges key is vertex value is neighbouring vertex static HashMap < Character , ArrayList < Character >> edgelist = new HashMap <>(); //count of vertex and edge static int ...

.