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