Sunday, April 23, 2006 

java.lang.UnsupportedClassVersionError:

java.lang.UnsupportedClassVersionError:

When I try to compile the simple Hello program there was no problem but when trying to compile it but its giving error when tyring to run the class file

class Hello
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Exception in thread "main" java.lang.UnsupportedClassVersionError:
HelloWorld (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

is the error I am getting.......


Solution:

Set the path using:

C:\oracle\ora92\bin;C:\Program Files\Oracle\jre\Change it to the latest version you have installed(1.5.0 in my case)\bin;

The error has occured becuase you have compiled the program with a latest version and trying to run it using a version which is older version.... so make it to a new version.

Saturday, April 22, 2006 

Merge Sort


/*
The merge sort splits the list to be sorted into two equal halves, and places them in
separate arrays. Each array is recursively sorted, and then merged back together to
form the final sorted list. Like most recursive sorts, the merge sort has an algorithmic
has an complexity of O(n log n).
Elementary implementations of the merge sort make use of three arrays - one for each half
of the data set and one to store the sorted list in. The below algorithm merges the arrays
in-place, so only two arrays are required. There are non-recursive versions of the merge sort,
but they don't yield any significant performance enhancement over the recursive algorithm on
most machines.


Pros: Marginally faster than the heap sort for larger sets.

Cons: At least twice the memory requirements of the other sorts; recursive.
*/

#include <stdlib.h>
#include <stdio.h>




void mergeSort(int numbers[], int temp[], int array_size);
void m_sort(int numbers[], int temp[], int left, int right);
void merge(int numbers[], int temp[], int left, int mid, int right);

int numbers[10];
int temp[10];


int main()
{
int i;


printf("Enter the numbers you want to sort");
for(i=0;i<10;i++)
scanf("%d",&numbers[i]);

//perform merge sort on array
mergeSort(numbers, temp, NUM_ITEMS);

printf("Done with sort.\n");

for (i = 0; i < NUM_ITEMS; i++)
printf("%i\n", numbers[i]);
}


void mergeSort(int numbers[], int temp[], int array_size)
{
m_sort(numbers, temp, 0, array_size - 1);
}



void m_sort(int numbers[], int temp[], int left, int right)
{
int mid;

if (right > left)
{
mid = (right + left) / 2;
m_sort(numbers, temp, left, mid);
m_sort(numbers, temp, mid+1, right);

merge(numbers, temp, left, mid+1, right);
}
}


void merge(int numbers[], int temp[], int left, int mid, int right)
{
int i, left_end, num_elements, tmp_pos;

left_end = mid - 1;
tmp_pos = left;
num_elements = right - left + 1;

while ((left <= left_end) && (mid <= right))
{
if (numbers[left] <= numbers[mid])
{
temp[tmp_pos] = numbers[left];
tmp_pos = tmp_pos + 1;
left = left +1;
}
else
{
temp[tmp_pos] = numbers[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}

while (left <= left_end)
{
temp[tmp_pos] = numbers[left];
left = left + 1;
tmp_pos = tmp_pos + 1;
}
while (mid <= right)
{
temp[tmp_pos] = numbers[mid];
mid = mid + 1;
tmp_pos = tmp_pos + 1;
}

for (i=0; i <= num_elements; i++)
{
numbers[right] = temp[right];
right = right - 1;
}
}

 

Insertion Sort

/** this is a program to implement Insertion sort
V.S.Phanindra
*/

/**
Algorithm Analysis
The insertion sort works just like its name suggests - it inserts each item into its proper place in the final
list. The simplest implementation of this requires two list structures - the source list and the list into
which sorted items are inserted. To save memory, most implementations use an in-place sort that works by
moving the current item past the already sorted items and repeatedly swapping it with the preceding item
until it is in place.
Like the bubble sort, the insertion sort has a complexity of O(n2). Although it has the same complexity,
the insertion sort is a little over twice as efficient as the bubble sort.

Pros: Relatively simple and easy to implement.
Cons: Inefficient for large lists.
*/
class InsertionSort
{
public static void main(String[] args)
{
int a[]={2,1,4,6,8,9,-1,4,3,10};
int temp,j;

for(int i=0;i<a.length;i++)
{
int index=a[i];
j=i;
while((j>0)&&(a[j-1]>index))
{
a[j]=a[j-1];
j=j-1;
}
a[j]=index;
}
for(int j1=0;j1<a.length;j1++)
System.out.println(a[j1]);
}

}

 

Selection Sort

/**
The selection sort works by selecting the smallest unsorted item remaining in the list, and then swapping
it with the item in the next position to be filled. The selection sort has a complexity of O(n2).

It yields a 60% performance improvement over the bubble sort, but the insertion sort is over twice as fast as
the bubble sort and is just as easy to implement as the selection sort. In short, there really isn't any reason
to use the selection sort - use the insertion sort instead.

If you really want to use the selection sort for some reason, try to avoid sorting lists of more than a 1000 items
with it or repetitively sorting lists of more than a couple hundred items.

Pros: Simple and easy to implement.
Cons: Inefficient for large lists, so similar to the more efficient
insertion sort that the insertion sort should be used in its place.

*/
class SelectionSort
{
public static void main(String[] args)
{
int a[]={2,1,4,6,-1};
int j=0,temp=0;
for(int i=0;i<a.length-1;i++)
{
int min=i;
for(j=i+1;j<a.length;j++)
{
if(a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}

Monday, April 10, 2006 

Program to Implement Quick Sort

/**
The quick sort is an in-place, divide-and-conquer, massively recursive sort. The efficiency of the algorithm is majorly impacted by which element is choosen as the pivot point.The worst-case efficiency of the quick sort, O(n2), occurs when the list is sorted and the left-most element is chosen.*/

Pros: Extremely fast.
Cons: Very complex algorithm, massively recursive.

#include
#include



void quickSort(int numbers[], int array_size);
void q_sort(int numbers[], int left, int right);

int numbers[5];


int main()
{
int i;

printf("Enter the numbers to be sorted");
for (i = 0; i <5; i++)
scanf("%d",&numbers[i]);

//perform quick sort on array
quickSort(numbers, 5);

printf("List of Elements after Sorting.\n");
for (i = 0; i < 5; i++)
printf("%i\n", numbers[i]);
}


void quickSort(int numbers[], int array_size)
{
q_sort(numbers, 0, array_size - 1);
}



void q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
static int count=0;
count++;
l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot-1);
if (right > pivot)
q_sort(numbers, pivot+1, right);
}

 

Bubble Sort

/** This is a program to implement bubble sort
Author : V.S.Phanindra
*/

The bubble sort is the oldest and simplest sort in use. Unfortunately, it's also the slowest, it sort works by comparing each item in the list with the item next to it, and swapping them if required.

Pros: Simplicity and ease of implementation.
Cons: Horribly inefficient.

class BubbleSort
{
public static void main(String[] args)
{
int a[]={2,1,4,6,8,9,-1,4,3,2};
int temp;

System.out.println("This is the List of elements before Sorting:");
for(int k=0;k<a.length;k++)
System.out.println(a[k]);
for(int i=0;i<a.length;i++)
{
for(int j=i+1;j<a.length-1;j++)
{
if (a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
} }
System.out.println("This is the sorted List of Elements:");
for(int j=0;j<a.length;j++)
System.out.println(a[j]);
}

}

Saturday, April 08, 2006 

Program for print Ramanjan Numbers

/*Taking input as command line arguments and checks whether the numbers
form Ramanjan number.*/

/*Author:SivaPhanindra.Vemuri*/
#include
main(int argc,char *argv[])
{
int l,m,n,p;
if(argc!=5)
{
printf("Wrong Number of arguments\n");
exit(0);
}
l=atoi(argv[1]); //loading the values of l,m,n,p from the
// comman line arguments
m=atoi(argv[2]);
n=atoi(argv[3]);
p=atoi(argv[4]);
printf("%d\n%d\n%d\n%d\n",l,m,n,p);
if(l!=m!=p!=n)
{
if(cube(l)+cube(m)==cube(n)+cube(p))
printf("GIVEN NUMBERS FORM RAMANJAN SERIES\n");
else
printf("NUMBERS DOES'NT FORM RAMANJAN NUMBER\n");
}
else
printf("NUMBERS DOES'NT FORM RAMANJAN NUMBER\n");
}
int cube(int x) //defining cube
{
return(x*x*x);
}

Thursday, April 06, 2006 

Overriding in java

when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.

sample program on overriding:

class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}

void show() {
System.out.println("i and j: " + i + " " + j);
}
}

class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}

void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B class
}
}

What if I want to call show method in super class.....


sample program on overriding:

class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}

void show() {
System.out.println("i and j: " + i + " " + j);
}
}

class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}

 

What is overloading in java

In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case,the methods are said to be overloaded, and the process is referred to as method
overloading. Method overloading is one of the ways that Java implements polymorphism.

Sample program of overloading:

class Overloading {
void test() {
System.out.println("No parameters");
}

void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}

void test(float a) {
System.out.println("Inside test(double) a: " + a);
}
}
class Overload {
public static void main(String args[]) {
Overloading ob = new Overloading();
int i = 88;
ob.test();
ob.test(10, 20);
ob.test(i);
ob.test(123.2);
}
}

About me

  • I'm phanindra
  • From Hyderabad, Andhra Pradesh, India
  • An Alumini of JNTU Hyd into happening IT industry...
My profile

Visitors
Bloggeries Blog Directory BlogRankings.com singapore blog directory blog search directory
Google
 
Web conceptoftheday.blospot.com