0

I am trying to build a program where I have a visual representation of an int array while they get sorted. There will be two different search algorithms of choice. All the functionality is split up into classes, interfaces and abstract classes. My main problem is getting the data from one thing to the next.

My main class implements basic window functionality. Paints the window, a few buttons to choose the search algorithm, provides a textfield to input an array and displays in its center a bar graph visualisation of the array. Drawing the bars is done in a class that extends JComponent. This is also where I transform the String of numbers into an int array. I can already draw the graphs, change the array and it gets also drawn.

Now I have an interface called Sorter which provides the following methods.

public void setUpTo( int i ); // to limit the number of swaps during the search
public void setNumbers( int[] numbers );
public void sort();
public String getName();
public int getSwaps();

Then I have the abstract class CountingSort that implements Sorter and the class CountingBubbleSort which extends CountingSort.
This is all pretty confusing to me.
Within my main class I listen to a button to pass on the content of TextField and start the sorting. What do I need to do to get the int array through CountingSort into CountingBubbleSort? I have already implemented CountingBubbleSort.

Let me know what additional information I need to provide.

5
  • CountingBubbleSort#setNumbers ? Commented Jan 16, 2014 at 14:30
  • Not sure if I understood, but, why don't you just create an instance of CountingBubbleSort? Then call setNumbers(...) (and all required methods) for that instance. Commented Jan 16, 2014 at 14:33
  • Should be just a matter of creating a CountingBubbleSort object and calling the setNumbers() method. The idea of the abstract class, CountingSort, is to provide a standard interface to the sorting functionality allowing you to have several different types of sorting objects derived from that abstract class. Polymorphism allows you to treat all of the various derived types as just a CountingSort object and each of the derived types will do their own specific implementation of sorting. Commented Jan 16, 2014 at 14:37
  • How and where do I do this? I'm sorry but this is my first task using Interfaces and abstract classes. Commented Jan 16, 2014 at 14:41
  • Take a look at some of the Related links such as this one stackoverflow.com/questions/11389137/… Commented Jan 16, 2014 at 14:49

1 Answer 1

2

If you add another method to your Sorter interface (getNumbers()), it will guarantee, that all the Sorter implementations will have a getter and setter to the internal int array.

interface Sorter {
    public int[] getNumbers();
    public void setNumbers(int [] numbers);
    //... other methods....
}

Then if you implement it like this in your abstract CountingSort class, then you will be able to use these methods without the need to implement them in all subclasses.

abstract class CountingSort implements Sorter{
    private int [] mNumbers;

    @Override
    public int[] getNumbers() {
        return mNumbers;
    }

    @Override
    public void setNumbers(int[] numbers) {
        mNumbers = numbers;
    }
}

Then you can access the numbers in the concrete implementation any time, if you have them set. I would advise to use a constructor in the CountingBubbleSort, which takes the int array (or generates it randomly, however you want to create it). This constructor could maybe moved up to the abstract class:

class CountingBubbleSort extends CountingSort{
    public CountingBubbleSort(int [] numbers){
        setNumbers(numbers);
    }

    public void someOtherMethod(){
        int [] numbers = getNumbers();
    }
}

In your main class you can use what inheritance has to offer, something like this:

class MainClass {
    private Sorter mSorter;

    public void doSort(){
        //Create sorter objects
        if(you want CountingBubbleSort){
            mSorter = new CountingBubbleSort();
        }else{
            mSorter = new BubbleSort();
        }
        //get the numbers
        int [] numbers = mSorter.getNumbers();

        // do the sorting
        mSorter.sort();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok. I've done that. In CountingBubbleSort I put `public void sort(int[] numbers){...} under your suggested code. But how do I call the BubbleSort from my main class?
Updated my answer. I advise to look at java inheritance in more depth before you continue your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.