Results 1 to 8 of 8
  1. #1
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default Problem: Arrays and Sorting

    Code:
    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
            }
        }
    }
    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
    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
            }
        }
    }
    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?

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    Why dont you ask how many ints, then create an array, then return that array?

  3. #3
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    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;
        }
    }
    Is this what you're telling me to do?

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    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:
    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
            }
    
        }
    }
    It's up to you how you want your code to work.

  5. #5
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    I tried what you suggested, but
    Java Code:
            this.numArray2[] = new int[z];
    was highlighted as "illegal start of expression" when I was compiling

  6. #6
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    Sorry, it would be: this.numArray2 = new int[z];

  7. #7
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Ok, so I tried this (underlined were the changes):
    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]
        }
    }
    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?
    Last edited by Rhez; 02-01-2010 at 11:09 AM.

  8. #8
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

Similar Threads

  1. Scanner, while loop and sorting arrays/string?
    By RSYR in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 07:13 PM
  2. Replies: 0
    Last Post: 01-17-2010, 07:09 AM
  3. Sorting Arrays by enum
    By sahity1a@yahoo.com in forum New To Java
    Replies: 3
    Last Post: 11-26-2009, 10:08 AM
  4. sorting problem...
    By mark-mlt in forum New To Java
    Replies: 4
    Last Post: 04-17-2008, 03:15 PM
  5. sorting problem
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •