To know more about java tutorials.... Click here
More Javascript tutorials you can found here

Merge Sort Example in Java

April 21, 2014 4 comments

package in.malliktalksjava;

/**
* @author malliktalksjava
*
* Sorts the given array using merge sort algorithm
*/
public class MergeSortExample {

private int[] array;
private int[] tempMergArr;
private int length;

/**
* @param args
*
* main method.
* Pass the array to sort method and prints the output array into console.
*/
public static void main(String args[]){

int[] inputArr = {12,25,36,95,86,21,14,52,85,32};
MergeSortExample mergeSorter = new MergeSortExample();
mergeSorter.sort(inputArr);
for(int sortedValue:inputArr){
System.out.print(sortedValue);
System.out.print(” “);
}
}

/**
* @param inputArr
*
* Checks given array is null of not, then pass it to mergeSort Method.
*/
public void sort(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {
System.out.println(“Given Array is null or does not contain any elements”);
return;
}

this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
mergeSort(0, length – 1);
}

/**
* @param lowerIndex
* @param higherIndex
*
* Sorts the array using mergesort algorithm.
*/
private void mergeSort(int lowerIndex, int higherIndex) {

if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex – lowerIndex) / 2;
// sort the left side of the array
mergeSort(lowerIndex, middle);
// sort the right side of the array
mergeSort(middle + 1, higherIndex);
// merge both sides
mergeParts(lowerIndex, middle, higherIndex);
}
}

/**
* @param lowerIndex
* @param middle
* @param higherIndex
*
*
*/
private void mergeParts(int lowerIndex, int middle, int higherIndex) {

for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}
}
}

 

Other Useful Links:

Quick Sort Example Program in Java

Insertion Sort Example program in JAVA

Selection Sort Example Program in Java

Bubble Sort Example in JAVA

About these ads

Quick Sort Example Program in Java

April 21, 2014 4 comments

package in.malliktalksjava;

/**
* @author malliktalksjava
*
* Sort the given array using QuickSort Algorithm
*/
public class QuickSortExample {

private int array[];
private int length;

/**
* @param args
*
* Main Method. Calls the sort method and print the output into
* console.
*/
public static void main(String args[]) {
QuickSortExample quickSorter = new QuickSortExample();
int[] input = { 15, 42, 14, 100, 85, 1, 13, 6, 9 };
quickSorter.sortArray(input);
for (int count : input) {
System.out.print(count);
System.out.print(” “);
}
}

/**
* @param inputArr
*
* Checks given array is empty or null, if not calls quickSort
* method.
*/
public void sortArray(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length – 1);
}

/**
* @param lowerIndex
* @param higherIndex
*
* Sorts the given array
*/
private void quickSort(int lowerIndex, int higherIndex) {

int temp1 = lowerIndex;
int temp2 = higherIndex;
// calculate pivot number, here pivot is middle index number
int pivot = array[lowerIndex + (higherIndex - lowerIndex) / 2];
// Divide into two arrays
while (temp1 <= temp2) {
/**
* In each iteration, we will identify a number from left side which
* is greater then the pivot value, and also we will identify a
* number from right side which is less then the pivot value. Once
* the search is done, then we exchange both numbers.
*/
while (array[temp1] < pivot) {
temp1++;
}
while (array[temp2] > pivot) {
temp2–;
}
if (temp1 <= temp2) {
exchangeNumbers(temp1, temp2);
// move index to next position on both sides
temp1++;
temp2–;
}
}
// call quickSort() method recursively
if (lowerIndex < temp2)
quickSort(lowerIndex, temp2);
if (temp1 < higherIndex)
quickSort(temp1, higherIndex);
}

/**
* @param param1
* @param param2
*
* Exchange the numbers and set to instance array variable
*/
private void exchangeNumbers(int param1, int param2) {
int temp = array[param1];
array[param1] = array[param2];
array[param2] = temp;
}
}

 

Other Useful Links:

Bubble Sort Example in JAVA

Selection Sort Example Program in JAVA

Insertion Sort Example program in JAVA

Merge Sort Example in Java

Insertion Sort Example program in JAVA

April 20, 2014 4 comments

package in.malliktalksjava;

/**
* @author malliktalksjava
* This class sort an array using Insertion sort algorithm.
*
*/
public class InsertionSortExample {

/**
* @param args
*/
public static void main(String args[]){
int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doInsertionSort(arr1);
for(int counts:arr2){
System.out.print(counts);
System.out.print(“, “);
}
}

/**
* @param input
* @return
*/
/**
* @param input
* @return
*/
public static int[] doInsertionSort(int[] input){

int temp;
for (int count = 1; count < input.length; count++) {
for(int count2 = count ; count2 > 0 ; count2–){
if(input[count2] < input[count2-1]){
temp = input[count2];
input[count2] = input[count2-1];
input[count2-1] = temp;
}
}
}
return input;
}
}

 

Other Useful Links:

Selection Sort Example Program in JAVA

Bubble Sort Example in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java

Selection Sort Example Program in Java

April 19, 2014 4 comments

package in.malliktalksjava;

/**
* @author malliktalksjava
* @version 1.0
*
* Below class sorts the given array using SelectionSort
* mechanism and prints the output into console.
*/
public class SelectionSortExample {

/**
* @param args
*/
public static void main(String args[]){

int[] arr1 = {12,434,2,23,7,44,66,42};
int[] arr2 = doSelectionSort(arr1);
for(int var:arr2){
System.out.print(var);
System.out.print(“, “);
}
}

/**
* @param arr
* @return sorted array
*/
public static int[] doSelectionSort(int[] arr){

for (int count = 0; count < arr.length – 1; count++)
{
int index = count;
for (int count2 = count + 1; count2 < arr.length; count2++){
if (arr[count2] < arr[index]){
index = count2;
}
}

int smallerNumber = arr[index];
arr[index] = arr[count];
arr[count] = smallerNumber;
}
return arr;
}
}

 

Other Useful Links:

Bubble Sort Example in JAVA

Insertion Sort Example program in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java

Bubble Sort Example in JAVA

April 18, 2014 4 comments

package in.malliktalksjava;

/**
* @author malliktalksjava
*
*/
public class BubbleSortExample {

private static int[] input = { 4, 2, 9, 6, 23, 11, 44, 0 };

/**
* @param args
* main method to run the logic.
*/
public static void main(String[] args) {
bubbleSort(input);

}
/**
* @param array – input array which required to sort.
*
* logic to sort the elements
*/
public static void bubbleSort(int array[]) {
int count = array.length;
int var;
for (int temp = count; temp >= 0; temp–) {
for (int temp2 = 0; temp2 < count – 1; temp2++) {
var = temp2 + 1;
if (array[temp2] > array[var]) {
swapNumbers(temp2, var, array);
}
}
printNumbers(array);
}
}

/**
* @param var1
* @param var2
* @param array
* Swap two numbers
*/
private static void swapNumbers(int var1, int var2, int[] array) {

int temp;
temp = array[var1];
array[var1] = array[var2];
array[var2] = temp;
}

/**
* @param input – Sorted array
*
* Prints the sorted array into console.
*/
private static void printNumbers(int[] input) {

for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + “, “);
}
System.out.println(“\n”);
}
}

Other Useful Links:

Selection Sort Example Program in JAVA

Insertion Sort Example program in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java

Number series example program using JAVA

April 18, 2014 Leave a comment

package in.malliktalksjava;

/**
* @author malliktalksjava.in
*
* This program prints the numbers in below pattern
* 1
* 123
* 12345
* 1234567
* 12345
* 123
* 1
*/
class PrintingNumberPatterns{

public static void main(String[] args){
printNumberSeries();
}

/**
* Print the numbers in assending and decending order by iterating it.
*/
private static void printNumberSeries() {
for (int i = 1; i <= 7; i += 2) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}

for (int i = 5; i >= 1; i -= 2) {
for (int j = 1; j < i + 1; j++) {
System.out.print(j);
}
System.out.println();
}
}
}

javax.servlet.ServletException: BeanUtils.populate

February 2, 2014 Leave a comment

javax.servlet.ServletException: BeanUtils.populate
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:497)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:798)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:205)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Root Cause:

java.lang.IllegalArgumentException: argument type mismatch
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:1789)
org.apache.commons.beanutils.PropertyUtils.setNestedProperty(PropertyUtils.java:1684)
org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.java:1713)
org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:1019)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:808)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:495)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:798)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:205)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Solution:

Fallowings are may be the reason for the above exception
1) The action attribute of an tag must match exactly the path attribute of the action definition in the struts-config.xml file. This is how Struts associates the ActionForm bean with the action.

2) This error usually occurs when you have specified a form name that does not exist in your tag. For example, you specific and ‘myForm’ is not the name of a form associated with myAction in the struts-config file

3) You get this message when Struts is unable to map the data in the HTML form to the properties in your ActionForm bean. Make sure each of the properties on your bean is either a String or a Boolean. Do you have any properties of type java.util.Date or other objects? That might cause this error. Also check to see that you have public getters and setter for each of your properties.

Follow

Get every new post delivered to your Inbox.

Join 210 other followers