Results 1 to 5 of 5
  1. #1
    Z-slasher is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default Last Java assignment for the semester.

    here is the description for my last assignment:

    --------------------------------------------------------------------------

    Given the following array
    {23,54,32,56,78,987,5,4,21,12,845,999,888,333,222, 111,444,555}

    Create a class, SortAlgorithms that contain this array.

    Within this class develop two methods:

    int[]selection(int[]arrayin,char updown){...}

    int[]insertion(int[]arrayin.char updown){...}

    The selection method implements a selection sort on the integer array passed to it and returns the sorted array. The second parameter updown is a character which has one of two values. An "a" to sort the array in ascending order and a "d" to sort the array in descending order.

    The insertion method implements a insertion sort on the integer array passed to it and returns the sorted array. The second parameter updown is a character which has one of two values. An "a" to sort the array in ascending order and a "d" to sort the array in descending order.

    The class should contain a method that calls each of these methods twice. Once with the flag set for ascending order, once with the flag set for descending order.

    The class should print out the sorted arrays each time they are sorted and indicate in the output whether they were sorted in ascending or descending order.

    --------------------------------------------------------------------------
    I did try to make one method for a selection sort but i'm having some difficulty making the code.

    Here is my code I tried so far:

    public class SortAlgorithms { public static void main(String[] args) { - Pastebin.com

    It's probably all wrong, so I will need to know which is right.

  2. #2
    danborgir is offline Member
    Join Date
    Jan 2011
    Posts
    26
    Rep Power
    0

    Default

    Check out the Java API for sorting arrays.

    Arrays (Java Platform SE 6)

    You can use sort() to arrange the array.

    for example:

    Java Code:
    Arrays.sort(nameOfArray)
    Okay that last code is not very informative... This will print out the array in acending order:

    Check out the Java API for sorting arrays.

    Arrays (Java Platform SE 6)

    You can use sort() to arrange the array.

    for example:

    Java Code:
    Arrays.sort(nameOfArray)
    Okay that last code is not very informative... This will print out the array in acending order:

    Java Code:
    import java.util.Arrays;
    
    public class sortArray {
    	
    	int[] number={23,54,32,56,78,987,5,4,21,12,845,999,888,333,222,111,444,555};
    
    		public sortArray() {
    		
    		Arrays.sort(number);
    		
    		System.out.println(Arrays.toString(number));
    		}
    		
    	public static void main(String[] args) {
    			new sortArray();
    		}
    
    }
    Last edited by danborgir; 04-27-2011 at 08:42 PM.

  3. #3
    Z-slasher is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    as this is my last java assignment, I have to say I am still a beginner. in other words it was poorly teached.

    with that said I don't know where that code even goes. do I replace the code with the one you posted or do I replace the method I created with the code?

  4. #4
    danborgir is offline Member
    Join Date
    Jan 2011
    Posts
    26
    Rep Power
    0

    Default

    Make a new java file called sortArray and paste the second post I made in to it. Then you can compile and run the program

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default

    Four ways to make people think you're lazy and therefore not want to help you:

    1- Post your homework assignment without showing what you've tried or what went wrong.
    2- Complain about a deadline- there are hundreds of posts here, each with an urgent user who might have managed his/her time better.
    3- Complain that your lack of understanding is all your educator's fault- many people here are self-taught, without the benefit of a classroom, classmates, or an educator at all.
    4- Post all of your code, in an external link no less.

    In short, stop being lazy and post an SSCCE, and we'll go from there.
    Last edited by KevinWorkman; 04-28-2011 at 02:04 AM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Need Help with Java Assignment
    By smurf67 in forum New To Java
    Replies: 4
    Last Post: 03-26-2011, 10:25 AM
  2. My java assignment -- please help me !
    By java_beginner1 in forum New To Java
    Replies: 11
    Last Post: 05-20-2010, 04:00 PM
  3. My semester proj.
    By ramsrocker in forum New To Java
    Replies: 4
    Last Post: 03-08-2009, 05:25 AM
  4. Half a semester into java
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 10:05 AM
  5. Help with my assignment java
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:59 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
  •