Skip to main content

Posts

Showing posts from 2016

How to do Effective Programming?

There is no secret to doing effective programming but following a sequence of steps and procedures can help in building great programs. Today I will take you on a tour to some of the best practices that are integrants of perfect programs. Algorithms are important: - You can’t deny the fact that algorithms are important and before start to write any program, Algorithms are must to write. For example, if there is a program about finding the sum of two numbers? What will you write ?The steps can be :- 1)   Get the first number. 2)   Get the second number. 3)   Add both numbers. This is what algorithm is writing about ,a list of statements .From the above three statements you can conclude boundary cases (at least two number should be there in input), mathematical function(Sum is needed here) , storage capacity(amount of memory to be assign to the variables), number of methods by analyzing repeat steps (reduce replete codes) and many other things. During algorithm only yo

Left Rotation in an Array using Java

Left Rotation in an Array using Java There are two methods implemented by me here :- 1) Using extra Array   Get new position of  a current element using formula.  Add the element in the new position in a new array. 2) Using same Array:- This is little bit complex hold variable at index i. find new index for variable. hold variable present at new index. place variable at index i to this new index. assign i to new index variable. repeat steps until count is not equal to lenght of an array which simply means all variable got covered. package Arrays ; /* * 12345 to 34512 */ /* * sample input * 5 4(d=4 that is 4 rotations) 1 2 3 4 5 sample output 51234 */ public class LeftRotation { public static void printArray ( int [] arr ) { for ( int i =0; i < arr . length ; i ++) { System . out . print ( " " + arr [ i ]); } System . out . println ( " " ); } //method which returns

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

.