Problem: Arrays and Sorting

• 01-29-2010, 04:50 AM
Rhez
Problem: Arrays and Sorting
Code:
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
//area

//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
}
}
}

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.
I know something like
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
}
}
}

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.
So what do I do?
• 01-29-2010, 06:36 AM
collin389
Why dont you ask how many ints, then create an array, then return that array?
• 01-29-2010, 07:20 AM
Rhez
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;
}
}

Is this what you're telling me to do?
• 01-29-2010, 08:01 AM
collin389
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:
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
}

}
}

It's up to you how you want your code to work.
• 01-30-2010, 03:25 AM
Rhez
I tried what you suggested, but
Code:

this.numArray2[] = new int[z];
was highlighted as "illegal start of expression" when I was compiling
• 01-30-2010, 07:13 AM
collin389
Sorry, it would be: this.numArray2 = new int[z];
• 02-01-2010, 11:06 AM
Rhez
Ok, so I tried this (underlined were the changes):
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]
}
}

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?
• 02-03-2010, 03:18 PM
Rhez
bump; sorry, but I still need help here.