tags:

views:

120

answers:

2
+1  Q: 

Java Bucket Sort

I am trying to create my own style of Bucket Sort and I am getting an error with my code and I don't know how to fix it. Any help would be great. I have two classes, Node and BucketSort:

public class Node {

    protected int element;
    protected Node next;

    public Node()
    {
        element = 0;
        next = null;
    }
    public Node getNext(Node n)
    {
        return next;
    }
    public void setNext(Node n)
    {
        n = next;
    }
    public void setElement(int e)
    {
        e = element;
    }
    public int getElement()
    {
        return element;
    }
}

and

public class BucketSort extends Node{


    public static void bucketSort(int v, int[] b) //b = the array of integers and v = number of buckets wanted. 
    {
        Node[] bucket = new Node[v];  //Creating an array of type Node called bucket with v buckets. 
        for (int i=0; i<=b.length; i++){
            bucket[i].setElement(b[i]);   //Loop through array of integers b, and set each element to the corresponding index of array bucket.
        return;
        }

        for (int i=0; i<=bucket.length; i++){
             //Getting the element at the indexed Node of array "bucket" and assigning it to the array "b" at location element-1. For example if the element is 3, then 3 is assigned to index 2, since it is the 3rd spot.
            b[getElement()-1] = bucket[i].getElement(); 
            return; 
        }
        System.out.println(b);
    }

}

I am getting the following Error at line 16 of BucketSort, the is code: b[getElement()-1]

The Error is:

Cannot make a static reference to the non-static method getElement() from the type Node.

Can you please tell me how to fix this.

Thanks

How can I Test This Program?

+3  A: 

The problem is your misuse of the static keyword. I'm not sure if you're familiar with the semantics of static methods in Java (if you're not, you should read something comprehensive about it, maybe starting here), but the basic idea is that a method or field that is declared static belongs to the class and not any particular instance (object) of that class. In particular, static methods don't have any notion of a this pointer.

So the problem is that when your code calls getElement() by itself, Java is implicitly interpreting that as this.getElement(). However, bucketSort() is static while getElement() belongs to an instance of Node, so this doesn't make any sense -- what exactly is getElement() being called on?

Call it on a particular Node object, like bucket[i], and it'll compile (haven't really seen if it'll work though).

Adrian Petrescu
Could I just make the function not static?
user258875
That would resolve that compiler error, but I don't think it's a good "Big-Picture" thing to do. For one, it would mean you would have to first create a BucketSort object before you could call the actual sorting method. The root of your problem is that BucketSort subclasses Node, which is weird. Re-design that and the proper patterns should fall into place.
Adrian Petrescu
A: 

See the error. It tells the mistake you have done.

You can call the getElement() for a particular object.

And why bucketSort(int v, int[] b) need to be static? Any reasons?

Dheeraj Joshi