A Developer's Diary

May 19, 2012

A simple java web application in minutes

You can set up your simple java web application in no time using maven. The application can be downloaded from maven's site.

Maven comes up with a single line command to create Hello World web application for you. The value to the argument archetypeArtifactId identifies that the project to be created is of type web application. In case you are running this command for the first time, it will take some time for the maven to download the dependent plugins first and then create the sample application

Create Web Application Project
mvn archetype:create -DgroupId=com.simple.web -DartifactId=webapp -DarchetypeArtifactId=maven-archetype-webapp
Your sample application is created and ready to be packaged and deployed

Read more ...

May 18, 2012

Maven cheat sheet for the beginners

Maven is a build automation tool used by the java developers. Some of the commands which I use on daily basis are:

Create a Java Project
mvn archetype:create -DgroupId=com.devfaqs.project -DartifactId=MyProject
Above is a single line command to create hello world java project. The command creates the project in a well defined directory structure along with the provision to write and execute junit test cases for the project created

Read more ...

May 1, 2012

Array Multiplication Problem - II

The Problem
Given an array A[n] of n numbers. You have to modify A[n] such that A[i] will be equal to multiplication of all the elements of A[n] except A[i] e.g.
A[0] = A[1] * A[2] * ... * A[n-1] and
A[1] = A[0] * A[2] * ... * A[n-1]
You have to solve the problem without using the division operator and in O(n). You cannot make use of another array

Read more ...

Array Multiplication Problem

The Problem

Given an array A[n] of n numbers. You have to compose an array O[N] such that O[i] will be equal to multiplication of all the elements of A[n] except A[i] e.g.
O[0] = A[1] * A[2] * ... * A[n-1] and
O[1] = A[0] * A[2] * ... * A[n-1]
You have to solve the problem without using the division operator and in O(n).
C++ Program
#include <iostream>
#define MAX 5

int main()
{
  int arr[MAX] = { 4, 3, 5, 1, 2 };
  int product;

  int product_of_elems_before[MAX] = {0};
  product = 1;
  for(int i = 0; i < MAX; ++i)
  {
    product_of_elems_before[i] = product;
    product *= arr[i];
  }

  int product_of_elems_after[MAX] = {0};
  product = 1;
  for(int i = MAX - 1; i >= 0; --i)
  {
    product_of_elems_after[i] = product;
    product *= arr[i];
  }

  for(int i = 0; i < MAX; ++i)
  {
    arr[i] = product_of_elems_before[i] * product_of_elems_after[i];
    std::cout << arr[i] << " ";
  }
  std::cout << std::endl;
}
Java Program
public class ArrayMultiplication
{
  public static void main(String args[])
  {
    int[] arr = new int[]{ 3, 2, 1, 4, 5 };
    int product;
    
    int productOfElemsBefore[] = new int[arr.length];
    product = 1;
    for(int i = 0; i < arr.length; ++i)
    {
      productOfElemsBefore[i] = product;
      product *= arr[i];
    }

    int productOfElemsAfter[] = new int[arr.length];
    product = 1;
    for(int i = arr.length - 1; i >= 0; --i)
    {
      productOfElemsAfter[i] = product;
      product *= arr[i];
    }

    for(int i = 0; i < arr.length; ++i)
    {
      arr[i] = productOfElemsBefore[i] * productOfElemsAfter[i];
      System.out.print(arr[i] + " ");
    }
  }
}

Read more ...

Apr 28, 2012

Tower of Hanoi Problem

The Problem

There are 3 pegs source ,auxillary and target. n disks of different sizes are given which can slide onto any peg . In the beginning all of the disks are in the source peg in the order of size with largest disk at the bottom and smallest disk at the top. We have to move all the disks from source peg to target peg such that in the end the target peg will have all the disks in the same order of size.

Rules:
1. Only one disk can be moved from one peg to another peg at a time
2. A larger disk cannot be placed on top of the smaller disk
C++ Program
#include <iostream>
#include <cstdlib>

static int moveCounter = 0;
void towerOfHanoi(int ndisk, char source, char auxillary, char target)
{
  if(ndisk == 1)
  {
    std::cout << "Move [" << ndisk << "]   [" << source << "] to [" <<
      target << "]" << std::endl;
    ++moveCounter;
    return;
  }

  //place ndisk - 1 disks from source to auxillary peg
  towerOfHanoi(ndisk - 1, source, target, auxillary);
  
  //place ndisk to target peg
  std::cout << "Move [" << ndisk << "]   [" << source << "] to [" <<
      target << "]" << std::endl;
  ++moveCounter;

  //place ndisk - 1 disks from auxillary to target peg
  towerOfHanoi(ndisk - 1, auxillary, source, target);
}

int main(int args, char *argv[])
{
  if(argv[1] == NULL)
  {
    std::cout << "ERROR: Insufficient Arguments\n";
    std::cout << "Usage: ./a.out number_of_disks\n";
    exit(-1);
  }
  int disks = atoi(argv[1]);
  
  char peg1 = 'A', peg2 = 'B', peg3 = 'C';
  towerOfHanoi(disks, peg1, peg2, peg3);
  std::cout << "Total Moves = " << moveCounter << std::endl;
}

Read more ...