0

I have made a program where I'm trying to make a 'sorting shell' using eclipse. Some basic functions I can do are load which sorting algorithm to use, unload it, sort using the algorithm and unload the algorithm from the shell using a specific ID.

I have separate classes for all the sorting algorithms (Bubble, Insertion, Quick, etc), but I don't know how to load them into the shell. This is what I have so far:

public static void main(String[] args) {
    boolean exit_loop = true;

    while (exit_loop){

        String[] command;
        command = input();
        switch(command[0]){
            case "load":
                // ???
                }
                break;
            case "tag":
                break;
            case "sort":
                break;
            case "unload":
                break;
            case "quit":
                exit_loop = false;
                break;
            default: 
                System.out.println("Input a valid command!");
                break;
        }
    }
}

public static String[] input(){
    Scanner user_input = new Scanner( System.in );
    String command;
    System.out.print("sort:>");
    command = user_input.next();
    String[] first = command.split("\\s+");
    return first;
}

So even if I'm able to get the user-input, I'm not sure how to actually load my sorting algorithm classes, (which I have implemented as separate classes).

Here is an example of how I've implemented one of my sorting algorithms:

class Bubble implements SortingAlgorithms{

public int[] sort(int[] array){

    int temp;
    boolean swap = true;

    while(swap){
        swap = false;
        for (int i=0; i < array.length-1; i++){

            if (array[i] > array[i + 1]){
                temp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = temp;
                swap = true;
            }
        }
    }
    return array;
}

private String id;
public String name() {
    return id;
}

public void name(String name) {
    id = name;
}

Any help would be much appreciated!

  • What is "load" supposed to do? – BetaRide Oct 21 '15 at 05:22
  • load the sorting algorithm into the shell. For instance, if my class is named “Bubble", “sort:>load Bubble"should initialize the class in the environment. – Starscreen60 Oct 21 '15 at 05:25
  • And what is this initializiation doing? Your code just sorts something. All there is to do is call new of your sorting class and call the sort method. Just implement this two lines when the user enters "sort". – BetaRide Oct 21 '15 at 05:29
  • Since there are 5 sorting algorithms (I've only posted Bubble to stop the post from getting too long), if I load Bubble, then every time we want to sort something I'll sort using Bubble only, until unloaded. That's kinda what I'm trying to accomplish. I'll try your method as well! Thanks! – Starscreen60 Oct 21 '15 at 05:34
  • First of all, your question in fact seems to have *nothing* to do with Eclipse. Second, you haven't shown us what you've tried and why it fails => thus you're kind of missing the point of what exactly do you want to achieve here. If you indeed have only 5 fixed classes, simple `switch` statement will do. If you insist on loading external classes in runtime, it's possible too (simple example is http://stackoverflow.com/questions/9886266/is-there-a-way-to-instantiate-a-class-by-name-in-java), but your question lacks *a lot* of details why exactly these methods don't suit it. – GreyCat Oct 21 '15 at 13:43

1 Answers1

0

I assume you know how write the code to create and fill the array, and how to print the results so I won't bother with it here.

public static void main(String[] args) {
  boolean exit_loop = false;
  SortingAlgorithms selectedalgo = null;
  int[] array = null;

  while (!exit_loop) {
    String[] command;
    command = input();

    switch (command[0]) {
      case "load":
        SortingAlgorithms newalgo = null;
        String algoname = command[1]; // "Bubble" etc.
        try {
          Class c = Class.forName(algoname);
          newalgo = c.newInstance();
        }
        catch (Exception e) {
        }
        if (newalgo == null) {
          System.out.println("Unknown algorithm: " + algoname);
        }
        else {
          selectedalgo = newalgo;
        }
        break;

      case "sort":
        if (selectedalgo == null) {
          System.out.println("Load an algorithm first!");
        }
        else if (array == null) {
          System.out.println("Provide an array first!");
        }
        else {
          array = selectedalgo.sort(array);
        }
        break;

      case "unload":
        selectedalgo = null;
        break;

      case "quit":
        exit_loop = true;
        break;

      default: 
        System.out.println("Input a valid command!");
        break;
    }
  }
}
yacc
  • 2,915
  • 4
  • 19
  • 33