Results 1 to 8 of 8
Thread: Problem: Arrays and Sorting
- 01-29-2010, 03:50 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
Problem: Arrays and Sorting
Code:
Problem: So far there is no syntax error when compiled. What I want to do is create a method that asks the user how many integers will be in an array, and then the method will create this many random valued integers, store them in the array and output the random integers, the method made, on a list. When the user presses a sorting button (eg BubbleSort) that method should apply with the array given from RandomNums.Java Code:import java.io.*; //imports java input output import java.awt.*; //imports java abstract window toolkit import java.util.Random; //imports java Random from utilities public class SortAssignment extends EasyApp { public static void main(String[] args) { new SortAssignment(); } int numArray[] = new int[20]; //new integer array with 20 blank elements Button bListNums = addButton("List Nums",30,50,100,30,this); //adds a new //button Button bBubbleSort = addButton("Bubble Sort",30,100,100,30,this); Button bSelectSort = addButton("Selection Sort",30,150,100,30,this); Button bRandomNums = addButton("Random Nums",30,200,100,30,this); List lNumList = addList("",150,30,200,350,this); //adds a new list or text //area List lSortList = addList("",380,30,200,350,this); //constructor for objects of class SortAssignment //when button "List Nums" is pressed, method ListNums runs //when button "Bubble Sort" is pressed, method BubbleSort runs //when button "Selection Sort" is pressed, method SelectSort runs //when button "Random Nums" is press, method RandomNums runs public void actions(Object source,String command) { if(source == bListNums) { ListNums(numArray); } else if(source == bBubbleSort) { BubbleSort(numArray); } else if(source == bSelectSort) { SelectSort(numArray); } else if(source == bRandomNums) { RandomNums(); } } //First method public void ListNums(int numArray[]) { lNumList.removeAll(); //clear NumList lSortList.removeAll(); //clear SortList for(int n = 0; n < 20; n++) //loops 20 times { numArray[n] = inputInt("Type in an integer."); //Asks user for an //int value as n //increases String theNum = Integer.toString(numArray[n]); //converts int //to string lNumList.add(theNum); //list outputs what user inputted } } //BubbleSort method public void BubbleSort(int a[]) { ... } //SelectionSort method public void SelectSort(int a[]) { ... } public void RandomNums() { lNumList.removeAll(); lSortList.removeAll(); int z = inputInt("How many integers?"); //asks user for the number of //integers int numArray2[] = new int[z]; for(int n = 0; n < z; n++) //loops z times { double x = Math.random(); //method random runs numArray2[n] = (int)Math.round(x*z); String theNum = Integer.toString(numArray2[n]); //converts int //to string lNumList.add(theNum); //list outputs what the random integer was } } }
I know something like
won't work because z is not verified blah blah etc and if I made a null array I can't modify a final variable blah blah etc.Java Code:... public class SortAssignment extends EasyApp { public static void main(String[] args) { new SortAssignment(); } int numArray[] = new int[20]; //new integer array with 20 blank elements int numArray2[] = new int[z]; ... public void actions(Object source,String command) { if(source == bListNums) { ListNums(numArray); } else if(source == bBubbleSort) { BubbleSort(numArray); } else if(source == bSelectSort) { SelectSort(numArray); } else if(source == bRandomNums) { RandomNums(numArray2); } } ... public void RandomNums(int numArray2[]) { lNumList.removeAll(); lSortList.removeAll(); int z = inputInt("How many integers?"); //asks user for the number of //integers for(int n = 0; n < z; n++) //loops z times { double x = Math.random(); //method random runs numArray2[n] = (int)Math.round(x*z); String theNum = Integer.toString(numArray2[n]); //converts int //to string lNumList.add(theNum); //list outputs what the random integer was } } }
So what do I do?
- 01-29-2010, 05:36 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Why dont you ask how many ints, then create an array, then return that array?
- 01-29-2010, 06:20 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
Is this what you're telling me to do?Java Code:... public class SortAssignment extends EasyApp { public static void main(String[] args) { new SortAssignment(); } int numArray[] = new int[20]; //new integer array with 20 blank elements int numArray2[]; ... public void actions(Object source,String command) { if(source == bListNums) { ListNums(numArray); } else if(source == bBubbleSort) { BubbleSort(numArray); } else if(source == bSelectSort) { SelectSort(numArray); } else if(source == bRandomNums) { RandomNums(); } } ... public int[] RandomNums() { lNumList.removeAll(); lSortList.removeAll(); int z = inputInt("How many integers?"); //asks user for the number of //integers int numArray2[] = new int[z]; for(int n = 0; n < z; n++) //loops z times { double x = Math.random(); //method random runs ranged 0<x<1 numArray2[n] = (int)Math.round(x*z); //rounds x*z to the nearest //whole number String theNum = Integer.toString(numArray2[n]); //converts int //to string lNumList.add(theNum); //list outputs what the random integer was } return numArray2; } }
- 01-29-2010, 07:01 AM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
That could work if you set the object's array to the array getting returned, or if you wanted you could just set the object's array in RandomNums() like so:
It's up to you how you want your code to work.Java Code:... public class SortAssignment extends EasyApp { public static void main(String[] args) { new SortAssignment(); } int numArray[] = new int[20]; //new integer array with 20 blank elements int numArray2[]; ... public void actions(Object source,String command) { if(source == bListNums) { ListNums(numArray); } else if(source == bBubbleSort) { BubbleSort(numArray); } else if(source == bSelectSort) { SelectSort(numArray); } else if(source == bRandomNums) { RandomNums(); } } ... public void RandomNums() { lNumList.removeAll(); lSortList.removeAll(); int z = inputInt("How many integers?"); //asks user for the number of //integers this.numArray2[] = new int[z]; for(int n = 0; n < z; n++) //loops z times { double x = Math.random(); //method random runs ranged 0<x<1 this.numArray2[n] = (int)Math.round(x*z); //rounds x*z to the nearest //whole number String theNum = Integer.toString(this.numArray2[n]); //converts int //to string lNumList.add(theNum); //list outputs what the random integer was } } }
- 01-30-2010, 02:25 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
I tried what you suggested, but
was highlighted as "illegal start of expression" when I was compilingJava Code:this.numArray2[] = new int[z];
- 01-30-2010, 06:13 AM #6
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Sorry, it would be: this.numArray2 = new int[z];
- 02-01-2010, 10:06 AM #7
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
Ok, so I tried this (underlined were the changes):
No syntax errors were found when compiled, but when running after I clicked "Random Nums", entered a number (let's say 5) and selected "Bubble/Selection Sort", the output was 5 zeros. The amount needed to be sorted was correct but now there seems to be something missing for the correct output. What is needed to be done?Java Code:public class SortAssignment extends EasyApp { public static void main(String[] args) { new SortAssignment(); } int numArray[] = new int[20]; //new integer array with 20 blank elements [U]//(deleted numArray2[])[/U] ... public void actions(Object source,String command) { if(source == bListNums) { ListNums(numArray); } else if(source == bBubbleSort) { BubbleSort(numArray); } else if(source == bSelectSort) { SelectSort(numArray); } else if(source == bRandomNums) { [U]RandomNums(numArray);[/U] } } ... [U]public int[] RandomNums(int numArray[])[/U] { lNumList.removeAll(); lSortList.removeAll(); int z = inputInt("How many integers?"); //asks user for the number of //integers [U]this.numArray = new int[z];[/U] for(int n = 0; n < z; n++) //loops z times { double x = Math.random(); //method random runs ranged 0<x<1 numArray[n] = (int)Math.round(x*z); //rounds x*z to the nearest //whole number String theNum = Integer.toString(numArray[n]); //converts int //to string lNumList.add(theNum); //list outputs what the random integer was } [U]return numArray;[/U] } }Last edited by Rhez; 02-01-2010 at 10:09 AM.
- 02-03-2010, 02:18 PM #8
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
Scanner, while loop and sorting arrays/string?
By RSYR in forum New To JavaReplies: 10Last Post: 04-20-2011, 06:13 PM -
Need help with sorting 2d arrays (insertion and selection)
By mjunit in forum New To JavaReplies: 0Last Post: 01-17-2010, 06:09 AM -
Sorting Arrays by enum
By sahity1a@yahoo.com in forum New To JavaReplies: 3Last Post: 11-26-2009, 09:08 AM -
sorting problem...
By mark-mlt in forum New To JavaReplies: 4Last Post: 04-17-2008, 02:15 PM -
sorting problem
By mcal in forum New To JavaReplies: 1Last Post: 02-14-2008, 08:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks