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
}
}
Sorterclass. The Sorter has one responsibility which is to sort (implemented in different ways, but still). The menu should be part of the Driver class.