Sonar Setup for Java and Groovy


Hola Folks,

SonarQube, it’s an required tool when you’re going though a quality work and want to maintain it with your team as well.

Below are the steps that will guide through setting up a sonar in local machine.

  1. Download SonarQube 5.1
  2. Extract the zip file

  3. Change file
    1. Go to sonarqube-5.1\conf\inextractractedfolder.
    2. find”sonar.jdbc.username” and change it to “root” (Database username you use to login)
    3. find “sonar.jdbc.password” and change it to “root” (Database password you use to login for root)
    4. go to the MySQL 5.x section of file and remove comment from “sonar.jdbc.url”
  4. Create a schema named “sonar” in mysql

  5. Run “StartSonar.bat” from folder in “sonarqube-5.1\bin\” based on your OS and architecture. i.e. “sonarqube-5.1\bin\windows-x86-64\StartSonar.bat”
  6. Login to Sonar
    a. Now click on Login from upper right corner of the page and login with username as “admin” and password as “admin”.
    b. Go to http://localhost:9000 and check if sonar is working or not. (Sometimes it may take around 2-5 minutes, after you run StartSonar.bat)
  7. Install Groovy plugin to Sonar (SKIP THIS STEP IF YOUR’RE ONLY WORKING WITH JAVA)

    1. Once logged in, click on settings from the upper menu.
    2. Select Update Center from System menu.
    3. Click on Available Plugins and search for Groovy and install it.
    4. Restart sonar to get the benefit of installation.
      1. Press Ctrl + C on cmd screen that is running the sonar server.
      2. Press Y when asked to terminate batch job.
      3. Again click on “sonarqube-5.1\bin\windows-x86-64\StartSonar.bat” to start the sonar server.
  8. Run Analysis on Project
    1. Download SonarQube Runner (v. 2.4) and extractzip file.
    2. Go to”sonar-runner-2.4\conf\”anduncomment “sonar.jdbc.url” in MySQL section.
    3. Now set  an Environement variable for “SONAR_RUNNER_HOME” and set the path upto root folder of extracted Sonar Runner.
    4. To test if the environment variable is set open cmd and run “echo %SONAR_RUNNER_HOME%”, if you don’t see the path to folder printed then restart your machine, it will be set then.
    5. Copy “sonar-runner-2.4\bin\sonar-runner.bat” to project’s root folder.
    6. Run the file “sonar-runner.bat” from root folder of project.
    7. So, now project is analyzed, you can view it on your sonar server.
  9. Associate your eclipse project with SonarQube

    1. Go to eclipse market place and search with “sonarqube”, and install SonarQube 3.4.0
    2. Go to Windows -> Preferences and search for “SonarQube” expand the section and go to “Servers“. Check if “http://localhost:9000” is available or not, if not then add.
    3. Right click on Project -> Configure -> Associate with SonarQube…
    4. New window for Associate with SonarQube will open. Please type project keyword against your projectthat will suggest the analyzed project on sonar server – select it and click on Finish.
    5. Eclipse is configured with SonarQube, to analyze your local project from eclipse rightclick on project and goto SonarQube -> Analyze.



One of the divide and conquer types of algo.

Merging: Combine two ordered arrays to make one larger ordered array.

To sort an array

  • Divide it into two halves
  • Sort the two halves (recursively)
  • Merge the result



Mergesort guarantees to sort an array of N items in time proportional to N log N, no matter what the input. Its prime disadvantage is that it uses extra space proportional to N.

Binary Search


Binary search, a simple but yet effective algorithm to search and still used at many places.

Having a complexity of (1+lg N)

Goal: Given a Sorted array and a Key, find the index of the key in an array?

Binary Search:

  • Compare key against middle entry
  • Too Small, Go left.
  • To Big, Go right.
  • Equal, found.

Java Code for the above algorithm is as:

public static int binarySearch(int[] a, int key){
		int lo = 0;
		int hi = a.length - 1;
		while(lo <= hi){
			int mid = lo + (hi-lo)/2;
			if(key < a[mid])		hi = mid - 1;
			else if(key > a[mid])	lo = mid + 1;
			else return mid;
		return -1;

Why java calls `toString()` on `println()`


I know that calling an println(Object o) actually works as println(o.toString()) thing before but, today Found an implementation of println() at GrepCode for openjdk.

Here is what i found there :

Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object’s string value, then behaves as though it invokes print(java.lang.String) and then println().
x The Object to be printed.
820 public void println(Object x) {
821 String s = String.valueOf(x);
822 synchronized (this) {
823 print(s);
824 newLine();
825 }
826 }

which lead to String.valueOf(x) :

Returns the string representation of the Object argument.
obj an Object.
if the argument is null, then a string equal to “null”; otherwise, the value of obj.toString() is returned.
See also:

2901 public static String valueOf(Object obj) {
2902 return (obj == null) ? "null" : obj.toString();
2903 }

Nth Root


Same as my Previous Post from SQRT Implementation, We can also find Nth Root Using the Newton-Raphson.

Here it Goes,

int n = Integer.parseInt(request.getParameter("times"));
double A = Double.parseDouble(request.getParameter("number"));
double p = .001;

if(A < 0){
      System.err.println("A < 0");// we handle only real positive numbers
}else if(A == 0){
      System.err.println("A = 0");// we handle only real positive numbers
double x_prev = A;
double x = A / n;  // starting "guessed" value...

while(Math.abs(x - x_prev) > p)
      x_prev = x;
      x = ((n - 1.0) * x + A / Math.pow(x, n - 1.0)) / n;

Made a High-Low Game of Cards today


Finally Yesterday, after learning Some Array and Some Card Algorithms, Made a High-Low Card Game, which I like most while Playing a Poker. Currently It’s NOT in a GUI form, but I’ll try to give it Some better looking GUI. Hope So… :)

Below is the code for the Game


public class CardGuess {
	String[] suit={"club","dimond","hearts","spades"};
	String[] rank={"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
	int SUITS=suit.length;
	int RANKS=rank.length;
	String[] deck;
	public CardGuess(){
		deck=new String[N];
		for(int i=0;i<RANKS;i++){
			for(int j=0;j<SUITS;j++){
				deck[SUITS*i+j]=rank[i]+" of "+suit[j];
	public void suffle(){
		for(int i=0;i<N;i++){
			int r= i+(int) Math.random()*(N-i);
			String t=deck[r];
	public void show(){
		for(int i=0;i<N;i++){
	public String giveRandomOne(){
		return deck[(int) (Math.random()*N)];
	public int compareCard(String firstCard,String secondCard){
		String[] st=firstCard.split(" ");
		st=secondCard.split(" ");
		CardGuess cg=new CardGuess();
			return 1;
		else if(cg.indexOf(firstCard)<cg.indexOf(secondCard)){
			return -1;
			return 0;
	public int indexOf(String str){
		for(int i=0;i<=RANKS;i++){
				return i;
		return 0;
	public static void main(String ar[]){
		int Point=0;
		CardGuess cardGuess=new CardGuess();
		String firstCard=cardGuess.giveRandomOne();
		String secondCard=cardGuess.giveRandomOne();
		System.out.println("firstCard: "+firstCard+"\nSecondCard: "+secondCard);
		BufferedReader br=new BufferedReader(new InputStreamReader(;
		String userInput = "";
			try {
				System.out.println("Next Card Will Be up OR down?");
			} catch (IOException e) {
			int i=cardGuess.compareCard(firstCard, secondCard);
//			System.out.println(i);
//				System.out.println("\n\n"+firstCard);
				System.out.println("Correct, Your Total Point is "+Point);
//				System.out.println(secondCard);
				System.out.println("GAME OVER"+"\nYour Total Point is "+Point);

SQRT Implementation


Newton’s method. uses a classic iterative technique that dates back to Heron of Alexandria in the first century. It is a special case of the more general Newton-Raphson method.Newton's method

To compute the square root of a positive number x: Start with an estimate t. If t is equal to x/t (up to machine precision), then t is equal to a square root of x, so the computation is complete. If not, refine the estimate by replacing t with the average of t and x/t. Each time we perform this update, we get closer to the desired answer.

public class SQRT {
  public static void main(String ar[]){
  BufferedReader read=new BufferedReader(new InputStreamReader(;
  try {
   //read the number from user
      System.out.println("Please Enter the Num: ");
      double c=Double.parseDouble(read.readLine());
      double epsilon=1e-15; //relative error tolerance
      double t=c; //estimate of the square root of c

      //Repeatedly apply Newton update step until desired precision is achieved
      while(Math.abs(t - c/t) > epsilon*t){
          t=(c/t+t) / 2.0;
   } catch (NumberFormatException e) {
   // TODO Auto-generated catch block
   } catch (IOException e) {
   // TODO Auto-generated catch block