Results 1 to 13 of 13
- 01-31-2009, 09:06 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
Help with a long sorting program.
The assignment asks for a program that sorts a 1D int array, 2D int array, 1D double array, 2D double array, 1D char array, 2D char array, 1D string array and 2D string array using selection sort, bubble sort and another sort of your choice.
The program also asks for a nice menu to keep things organized and clean. I already know how to make the menu for this: 2 layers of menu, first have them pick what kind of sorting they want, then choose which kind of array to sort, after the second selection has been made, the array is made based on the selection. However, the things I am baffled by are:
1. Code efficient way of telling the sorting function what kind of sort and array the user has chosen.
2. How to sort chars and strings.
3. Another method of sorting other than bubble and selection.
I don't know exactly how to do bubble and selection sorting but I think i can look that up myself. Any help would be greatly appreciated.
- 02-01-2009, 01:19 AM #2
don't really know this subject too well, but i can give you a few suggestions for your third question.
quick sort, merge sort, heap sort, insertion-sort (recommended).
you can find out more about them in wikipedia.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-01-2009, 01:31 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
Thanks for the ideas angryboy. I'm mainly trying to get past number 1 because without it, I'm reluctant to move on.
- 02-01-2009, 09:06 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
Umm... I tried this code for the menu of the program. However, I can't seem to pass the array variable.
The bolded part is where its giving me a "no field name found" error. I think that the variable array is not recognized when it is created in a if statement.Java Code:static Console c; public static void main (String[] args) { c=new Console(); int choice, choice2; do { c.print("MENU"); c.print("1. 1D Int Array"); c.print("2. 2D Int Array"); c.print("3. 1D Double Array"); c.print("4. 2D Double Array"); c.print("5. 1D Char Array"); c.print("6. 2D Char Array"); c.print("7. 1D String Array"); c.print("8. 2D String Array"); do { c.println("Make a selection: "); choice=c.readInt(); if ( (choice < 1) || (choice > 8) ) c.println("Invalid choice please enter 1 - 8."); } while ( (choice2 < 1) || (choice2 > 8) ); if (choice==1){int[] array = new int[5];break;} else if (choice==2){int[][] array = new int[5][2];break;} else if (choice==3){double[] array = new double[5];break;} else if (choice==4){double[][] array = new double[5][2];break;} else if (choice==5){char[] array = new char[5];break;} else if (choice==6){char[][] array = new char[5][2];break;} else if (choice==7){String[] array = new String[5];break;} else if (choice==8){String[][] array = new String[5][2];break;} do { c.clear(); c.println("MENU"); c.println("1. Selection Sort"); c.println("2. Bubble Sort"); c.println("3. Exit"); c.println("4. Exit"); do { choice2=c.readInt(); if ( (choice2 < 1) || (choice2 > 4) ) c.println("Invalid choice please enter 1 - 4."); } while ( (choice2 < 1) || (choice2 > 3) ); if (choice2==1){c.clear();bubble([b]array[/b]);} else if (choice2==2){c.clear();selection([b]array[/b]);} else if (choice2==3){c.clear();bubble([b]array[/b]);} } while (choice2!=4); } while (choice !=4); c.println("Program Closed."); }
- 02-01-2009, 10:50 PM #5
that because you declared the array inside the if statements. i suggest using intArray, charArray, etc...
also look into switch-case statements.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-02-2009, 03:47 AM #6
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
I've decided to do the sorting and method selection within separate classes and call the appropriate classes within the main function's menu. Problem is I'm not familiar with the code to call new classes.
- 02-02-2009, 04:03 AM #7
just make a field and use the class as is. e.g. public MySortClass c = new MySortClass();
then just use: c.selection(array);USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-02-2009, 04:37 AM #8
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
I'm having trouble naming my classes I know I would have a base class that does the sorting and the method selection. Then i would have subclasses off of that base class that is used for the different types of arrays. But what would the code look like?
But how would I connect the subclasses with the base class? What would be the most code efficient way of doing this?Java Code:public class sortbase { public int selection { //Code for inputting method choice. } //Code for sorting } public class intsort: sortbase { //Code for sorting integers. } public class dintsort: sortbase { //Code for sorting 2D integers. } public class charsort: sortbase { //Code for sorting chars. } etc...Last edited by leiferouis; 02-02-2009 at 04:44 AM.
- 02-02-2009, 04:44 AM #9
do some research on relationships (has-a, is-a, etc...). in other words, you can extend the class, or use it as in the above post #7.
i suggest making a sort class, and a main class that ask for user inputs, then pass params to the sort class methods.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-02-2009, 11:43 PM #10
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
Okay. Thanks for the help. I've pretty much figured out exactly what this would look like. Now I just have to code. One question about the code itself.
I have something like this:
The error is that its saying sortclass has to return a value thats compatible with String[][], which doesn't make sense because I am only returning array and array is a String[][].Java Code://In main function static sortclass s = new sortclass(); //random code String[][] array=new string[5][2]; //random code array=s.sort(array, 5,2); //output code //In sortclass class public class SortClass2 { static public String[][] sort (String array[][],int x,int y) //sorting code return array; }Last edited by leiferouis; 02-03-2009 at 12:01 AM.
- 02-03-2009, 02:08 AM #11
typo's in bold:
Java Code://In main function static [B]sortclass[/B] s = new [B]sortclass[/B](); //random code String[][] array=new [B][I]string[/I][/B][5][2]; //random code array=s.sort(array, 5,2); //output code //In sortclass class public class [B]SortClass2[/B] { static public String[][] sort (String array[][],int x,int y) //sorting code return array; }USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-03-2009, 02:12 AM #12
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
nevermind I figured out myself a while ago.
- 02-04-2009, 04:46 AM #13
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
Long Cannot Be Dereferenced?
By caps_lock in forum New To JavaReplies: 1Last Post: 01-18-2009, 01:49 AM -
Calculate sum of long integer!
By Julingo in forum New To JavaReplies: 2Last Post: 09-09-2008, 11:50 PM -
It takes very long time.....
By iresha in forum Advanced JavaReplies: 6Last Post: 05-11-2008, 02:31 AM -
Help with Sorting Program
By rhm54 in forum New To JavaReplies: 3Last Post: 01-25-2008, 10:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks