What delivers a Parameter to a method like ...(myClass.FINALOBJECT)?

Viewed 58

I'm a student from Germany so sorry for bad language maybe.

I got an exercise from my lecturer in java basics and have a problem in understanding. He already teached us the basics about OOP so I know how to produce an object of a desired class for example with ClassX myObject = new ClassX (); and simple stuff like that.

But now in this new exercise we have to create a tool which visualizes sorting algorithms und analyses the complexity. The lecturer gave us some help to realise this by telling us:

A possible way a user could use the tool could be:

SortingTool sTool = new SortingTool();
sTool.setInitData(10,false);
sTool.performExperiment(Algorithms.INSERTIONSORT);

I'm pretty sure he meant us to make a class called Algorithms and in it to construct a final object of it like this:

public class Algorithms {

      public final static Algorithms INSERTIONSORT = new Algorithms ();

Could someone pls explain how this works in general? And especially what the performExperiment -class of sTool should get from the object INSTERTIONSORT.

Thank you all in advance!

1 Answers

It may be something like this

interface Algorithms {

  public static Algorithms INSERTION_SORT = new InsertionSort();         

  void sort(List<Integer> data);
}

public class InsertionSort implements Algorithms {
    public void sort(List<Integer> data) {
        // do insertion sort 
    }
}

And sTool.performExperiment() should look like this

performExperiment(Algorithms algo) {
    algo.sort(data);
}
Related