Reputation: 627
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
}
}
Upvotes: 0
Views: 971
Reputation: 7728
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)
{
}
}
Upvotes: 2
Reputation: 31290
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 );
}
}
Upvotes: 1