Reputation: 3247
I am trying to pass a class to a method as a parameter but don`t know if it is possible.
Instead of calling Insertion class
in the void runTest(String[] text, int[] number, String url)
method, can i pass it as a parameter so that i can add other sorting algorithms.
This is what I have so far:
Insertion insertion;
void setup() {
String url = sketchPath("numbers/512/");
insertion = new Insertion();
String[] stringData = null;
int[] intData = null;
runTest(stringData, intData, url);
}
void runTest(String[] text, int[] number, String url) {
File directory = new File(url);
File[] listOfFiles = directory.listFiles();
for (File file : listOfFiles) {
//println(file.getName());
text = loadStrings(file);
number = int(text);
insertion.insertionSort(number);
}
}
class Insertion {
Insertion() {
}
int[] insertionSort(int[] input) {
int temp;
for (int i = 1; i < input.length; i++) {
for (int j = i; j > 0; j--) {
if (input[j] < input[j-1]) {
temp = input[j];
input[j] = input[j-1];
input[j-1] = temp;
}
}
}
println(input);
return input;
}
}
Upvotes: 1
Views: 86
Reputation: 42176
What you probably want to do is define a Sort
interface which contains a sort()
function:
interface Sort{
public int[] sort(int[] input);
}
Then have your Insertion
class implement your Sort
interface and its sort()
function:
class Insertion implements Sort{
Insertion() {
}
int[] sort(int[] input) {
//...
}
}
Do the same for any other sorting classes, and then pass a Sort
variable into your runTest
function:
void runTest(String[] text, int[] number, String url, Sort sortFunction) {
File directory = new File(url);
File[] listOfFiles = directory.listFiles();
for (File file : listOfFiles) {
//println(file.getName());
text = loadStrings(file);
number = int(text);
sortFunction.sort(number);
}
}
Since your Insertion
class (and any other sorting class you've created) implements the Sort
interface, you can pass instances of them in to your runTest()
function.
Here is the Processing reference for implementing an interface.
Upvotes: 5
Reputation: 37584
You could use an Interface for this.
public interface SortInterface{
void sortStuff(int[] input);
}
Than you can have a lot of different Sorting classes which implement this Interface e.g.
class Insertion implements SortInterface
class BubbleSort implements SortInterface
All of them have the same method sortStuff(int[] input)
implemented which you can call in
runTest(String[] text, int[] number, String url, SortInterface sortObject)
{
sortObject.sortStuff(number);
}
Upvotes: 4
Reputation: 8487
Create an interface
called SortingAlgorithm
and then use it to create multiple implementations of sorting. Something like this:
interface SortingAlgorithm {
public int[] sort(int[]);
}
class BubbleSort implements SortingAlgorithm {
int[] sort(int[] input) {
// bubble sort code here
}
}
class InsertionSort implements SortingAlgorithm {
InsertionSort() {
}
int[] sort(int[] input) {
return insertionSort(input);
}
int[] insertionSort(int[] input) {
int temp;
for (int i = 1; i < input.length; i++) {
for (int j = i; j > 0; j--) {
if (input[j] < input[j-1]) {
temp = input[j];
input[j] = input[j-1];
input[j-1] = temp;
}
}
}
println(input);
return input;
}
}
Now use the algorithms:
InsertionSort insertionSort;
BubbleSort bubbleSort;
void setup() {
String url = sketchPath("numbers/512/");
insertion = new Insertion();
String[] stringData = null;
int[] intData = null;
runTest(stringData, intData, url, insertionSort);
runTest(stringData, intData, url, bubbleSort);
}
void runTest(String[] text, int[] number, String url, SortingAlgorithm algorithm) {
File directory = new File(url);
File[] listOfFiles = directory.listFiles();
for (File file : listOfFiles) {
//println(file.getName());
text = loadStrings(file);
number = int(text);
algorithm.sort(number);
}
}
Upvotes: 4