Results 1 to 6 of 6
  1. #1
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Modifying Given Code

    One of my assignments this week is to modify a given program that sorts an array of integers from lowest to highest so that it will sort names from last in the alphabet to first (for example: Input: Crowe, Clooney, Pitt, Gibson; Returns: Pitt, Gibson, Crowe, Clooney)

    I thought I was doing okay, but there are 2 places in the code that are looking for int variables. The teacher said the code changes would be minimal so I'm at a lost as to what to do next.

    Here's my updated code:

    Java Code:
    public class SelectionSortTester_JC {
           public static void main(String[] args) {
          //declare vars
             String[] data = {"Crowe", "Clooney", "Pitt", "Gibson"};
          
           //print array (unsorted)
             System.out.println("Unsorted: ");
             printArray(data);
          
          //call sort method
             selectionSort(data);
          
          //print array (sorted)
             System.out.println("Sorted: ");
             printArray(data);      
          } // eo main
       
       
        //METHODS-> 
        //---------
        
       	//prints out array of int all on 1 line.
       	//parameter passed: address of int array
    		//no return statement needed since change is made to actual array
    		public static void printArray(String[] inData) {
             for(int i = 0; i < inData.length; i++){
                System.out.print(inData[i] + "  ");}
             System.out.println();
          
          }
       	
       	//swaps two values in an array of int
       	//parameters passed: address of int array, indices of two elements to swap
    		//no return statement needed since change is made to actual array
          public static void swap(String[] inData, int current, int min) {
             int temp = inData[current];
             inData[current] = inData[min];
             inData[min] = temp;
          }//eo swap
       
          //Finds index of smallest entry in array
    		//parameters passed: address of int array, index of first element to be processed
          //returns index of smallest element in array  
    		public static int findSmallest(String[] inData, int start) {
             int smallestIdx = start;
             for (int i = (start + 1); i < inData.length; i++) {
                if(inData[i] < inData[smallestIdx]) {
                   smallestIdx = i;
                }//eo if
             }//eo for
             return smallestIdx;
          }//eo findSmmalest
       	
       	
       	//selection sort method
       	//parameter passed: address of int array
    		//no return statement needed since change is made to actual array
           public static void selectionSort(String[] inData) {
             int indexOfCurrentSmallest;
             for(int i = 0; i<(inData.length - 1); i++) {
                indexOfCurrentSmallest = findSmallest(inData,i);  //finds smallest
                swap(inData,i,indexOfCurrentSmallest); //swaps smallest with current
             }//eo for			     
          }//eo selectionSort method
       } // eo class

    I changed:
    Line 4 to take in strings.
    Line 25 to use a String array.
    Line 35 to use a String array.
    Line 44 to use a String array.
    Line 58 to use a String array.

    The incompatible type errors are showing up on lines 36 and 38 (temp, current and min are set to integers but I don't know how to make them work for strings).

    I'm also getting a bad operand type on line 47 since you can't do a < on Strings, but again, I don't know how to change it.

    Not too mention, the changes should be "minimal"....

    HELP!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Modifying Given Code

    Quote Originally Posted by jmscarlet9 View Post
    The incompatible type errors are showing up on lines 36 and 38 (temp, current and min are set to integers but I don't know how to make them work for strings).
    You do. You've done it already.

    A variable declaration and initialization follows the form
    Java Code:
    Type variableName = variableValue;
    where the variable value has a type that is assignment compatible with the declared type. In your present line 36, you have
    Java Code:
    int temp = inData[current];
    But. inData is an array of String -- each element, like inData[0], inData[1] ... inData[current] is a String. So, what should be the Type of the variable temp?

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

  3. #3
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Modifying Given Code

    Ok, that was me being stupid after 8 hours of trying to figure stuff out. And it may still be continuing, how do I fix the < error (error: bad operand types for binary operator '<') from line 7 below? I know that inData are Strings, but aren't i and smallestIdx integers? How can I get this to work?

    Java Code:
          //Finds index of smallest entry in array
    		//parameters passed: address of int array, index of first element to be processed
          //returns index of smallest element in array  
    		public static int findSmallest(String[] inData, int start) {
             int smallestIdx = start;
             for (int i = (start + 1); i < inData.length; i++) {
                if(inData[i] < inData[smallestIdx]) {
                   smallestIdx = i;
                }//eo if
             }//eo for

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

    Default Re: Modifying Given Code

    Questions to ask yourself:
    • What is inData an array of?
    • Does the < operator make sense for this data type?

  5. #5
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Modifying Given Code

    It doesn't make sense because this is comparing #s, but I want it to compare the letters and then print it in reverse alphabet. So, I need it to compare letters. I not sure how to switch it from one to the other.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Modifying Given Code

    Quote Originally Posted by jmscarlet9 View Post
    It doesn't make sense because this is comparing #s, but I want it to compare the letters and then print it in reverse alphabet. So, I need it to compare letters. I not sure how to switch it from one to the other.
    If all else fails read the API documentation; it's all in there.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Modifying this app.
    By seaisle68 in forum Java Applets
    Replies: 5
    Last Post: 04-07-2011, 04:12 PM
  2. Modifying output
    By sqlsql0 in forum New To Java
    Replies: 1
    Last Post: 02-28-2011, 12:33 PM
  3. Modifying Automato Code
    By whateverme in forum New To Java
    Replies: 0
    Last Post: 02-06-2011, 03:36 PM
  4. Need help modifying code for hex conversion
    By gamer765 in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 07:59 AM
  5. One Jspinner modifying the value of the other
    By cotarelo in forum AWT / Swing
    Replies: 1
    Last Post: 06-11-2010, 03:48 PM

Posting Permissions

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