0

I have an assignment I'm working on for my java course. We have to create a program that asks the user to enter several integers and then they make a choice of which type of sorting method they'd like to use (i.e. Selection, Bubble, and Insertion).

Directions state we should have a driver class and a class called Sorter. Class Sorter would have the methods for the different sorting options.

My issue is, I started with insertion sort and got that all up and going and thought the easy part would be separating it into different classes but I'm having a hard time doing that. I thought I'd use a switch for the "menu" and just add the different methods for the different sorting algorithms in methods below that. I can't figure out how to get it working properly though. Any help would be greatly appreciated.

Also, does my insertion sort look ok? I thought it would be better to use an array list rather than a fixed array, that proved to be harder than I thought too.

Here is my code...

Driver class

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;

public class DriverSort {

public static void main(String[] args) {

    Scanner scan =new Scanner(System.in);
    /* System.out.println("1-Insertion sort\n"
                    +"2-Selection sort\n"
                    + "3-Bubble sort\n"
                    + "0-quit\n"
                    + "Please enter the number for a sorting method or enter 0 to quit: ");
    option = scan.nextInt(); */

     //Instantiate and call Insertion sort. 

    String list="";

    ArrayList<Integer> arrlist=new ArrayList<Integer>();
    System.out.println(" ");
    System.out.println(" ");
    System.out.println("Welcome to the sorting application menu, the following sorting" 
            + "methods are available: ");
    System.out.println("Please enter the list of elements: ");
    System.out.println(" write 'STOP' when list is completed ");


    while(!(list=scan.nextLine()).equalsIgnoreCase("stop")){
        int intelement = Integer.parseInt(list);
        arrlist.add(intelement);           
    }

    int elementlist[]  = new int[arrlist.size()];
    Iterator<Integer> iter = arrlist.iterator();
    for (int j=0;iter.hasNext();j++) {
        elementlist[j] = iter.next();
    }

    elementlist = Sorter.insertionSort(elementlist);
    System.out.println(" ");
    System.out.println(" ");
    System.out.println(" ");
    System.out.println("Values after Insertion Sort : ");
    for (int j=0;j<elementlist.length;j++) {
        System.out.print(elementlist[j]+" ");
    }
  }
}

Sorter class

public class Sorter {

public int getMenu() {
    int option = 0;

    switch (option) {

    case 0:
        System.out.println("Goodbye!");
        System.exit(0);
        break;

    case 1: //Insertion Sort    

        //method for insertion algorithm
        public static int[] insertionSort(int[] list) {
            for (int i = 1; i < list.length; i++) {
                int next = list[i];
                // find the insertion location while moving all larger elements up
                int j = i;
                while (j > 0 && list[j - 1] > next) {
                    list[j] = list[j - 1];
                    j--;
                }//end while

                // insert the element
                list[j] = next;
            }//end for loop
            return list;
        }//end insertionSort



    }//end switch
  } 
} 
3
  • You can't define a method in a switch. Commented Aug 9, 2014 at 5:46
  • Yeah I should've clarified that...That's unfinished code. I wasn't sure what to add to the switch to work with the insertionSort method that would then work with the driver class. I just got lost. Commented Aug 9, 2014 at 5:52
  • The menu should not be in the Sorter class. The Sorter has one responsibility which is to sort (implemented in different ways, but still). The menu should be part of the Driver class. Commented Aug 9, 2014 at 6:01

2 Answers 2

2

Well, I think you are more confused about the design rather than about the code. Anyways, you can easily find the sorting implementations on the web, so I'll just try to help you out the design.

public class DriverSort {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        private static Sorter sorter = new Sorter();

        // blah blah blah
        // take the input in an array or alist

        int choice;  // variable which says which sorting algorithm to use
        int[] arr;   // list of inputs to be sorted

        switch (choice) 
        {
            case 0: sorter.InsertionSort(are);
                    sorter.print(arr);
                    break;

            case 1: sorter.QuickSort(arr);
                    sorter.print(arr);
                    break;

            // and so on…

            default:
                    break;
        }
        }
    }
}

Keep the Driver class independent of any sorting and printing logic. Handle that piece of code in the Sorter class.

public class Sorter {

    public int[] InsertionSort(int[] arr)
    {
        // implement specific sorting logic
        return arr;
    }

    public int[] SelectionSort(int[] arr)
    {
        // implement specific sorting logic
        return arr;
    }

    public int[] QuickSort(int[] arr)
    {
        // implement specific sorting logic
        return arr;
    }

    public int[] ShellSort(int[] arr)
    {
        // implement specific sorting logic
        return arr;
    }

    public void print(int[] arr)
    { 
    }
} 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes confused by the design. I get royal confused with methods, classes and parameters. I understand your code though, thanks a lot! One question, you said use either array or arraylist, is there a better choice?
Use Arrays in this case, because ArrayList is internally implemented using `Arrays'.
Quick, change the method names according to the usual form. Novices shouldn't have to unlearn.
Sorry, What do you mean ?
1

The description isn't quite clear, but this is one way of doing it:

What should be written is an abstract class (or interface) Sorter with a single abstract method 'void sort( int[] list )`. Then you write three classes extending (or implementing) Sorter, one for of the sorting methods; here method sort actually implements the algorithm.

The "driver" does the dialogue, creates a Sorter subclass instance and calls sort.

public abstract class Sorter {
     public abstract void sort( int[] list );
}

public class InsertionSorter extends Sorter {
    public void sort( int[] list ){
         // implement insertion sort
    }
}

public class Driver {
    public void sortUsing(){
         int iSort = ...;
         int[] list = ...; 
         Sorter sorter = null;
         switch( iSort ){
         case 1:
             sorter = new InsertionSorter();
             break;
         // cases 2, 3,...
         default:
             throw ...;
         }
         sorter.sort( list );
    }        
}

2 Comments

Thanks Laune! What is abstract? And also int iSort = ...; What's the ... again? Sorry I'm super new.
"abstract" indicates that a method is "promised to be implemented by non-abstract subclasses". An abstract class (typically) contains one or more abstract methods.

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.