简体   繁体   中英

Java: Implementation of Interfaces and abstract classes

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.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM