Results 1 to 4 of 4
  1. #1
    Shlr is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default Changing a code to include a main method

    How do you change this:

    Java Code:
    /**
       This class sorts an array, using the selection sort 
       algorithm
    */
    public class SelectionSorter
    {
       /**
          Constructs a selection sorter.
          @param anArray the array to sort
       */
       public SelectionSorter(int[] anArray)
       {
          a = anArray;
       }
    
       /**
          Sorts the array managed by this selection sorter.
       */
       public void sort()
       {  
          for (int i = 0; i < a.length - 1; i++)
          {  
             int minPos = minimumPosition(i);
             swap(minPos, i);
          }
       }
    
       /**
          Finds the smallest element in a tail range of the array.
          @param from the first position in a to compare
          @return the position of the smallest element in the
          range a[from] . . . a[a.length - 1]
       */
       private int minimumPosition(int from)
       {  
          int minPos = from;
          for (int i = from + 1; i < a.length; i++)
             if (a[i] < a[minPos]) minPos = i;
          return minPos;
       }
    
       /**
          Swaps two entries of the array.
          @param i the first position to swap
          @param j the second position to swap
       */
       private void swap(int i, int j)
       {
          int temp = a[i];
          a[i] = a[j];
          a[j] = temp;
       }
    
       private int[] a;
    }

    so that it has a main method? I need to call the sorting method from the main method, which I can't since there is no main method.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Changing a code to include a main method

    First thing I would try would be to simply add an empty main method. Have you tried this yet? Does it compile? If so, show the attempt please.

    Then I'd add some code to it including a SelectionSorter variable.

  3. #3
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default Re: Changing a code to include a main method

    Java Code:
    public static void main(String args[]) {
    		SelectionSorter myProgram = new SelectionSorter();
    	}

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Changing a code to include a main method

    Does that compile? If not, why not?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  2. Replies: 0
    Last Post: 03-02-2010, 08:28 AM
  3. Changing text colour in code
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-29-2009, 03:32 PM
  4. include-prelude/include-coda
    By devin in forum Java Servlet
    Replies: 0
    Last Post: 02-11-2009, 07:33 PM
  5. Changing plugins source code
    By yakupemre in forum Eclipse
    Replies: 1
    Last Post: 08-15-2007, 09:31 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
  •