Results 1 to 6 of 6
Thread: Modifying Given Code
- 04-01-2012, 03:47 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
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!
- 04-01-2012, 07:44 AM #2
Re: Modifying Given Code
You do. You've done it already.
A variable declaration and initialization follows the formJava Code:Type variableName = variableValue;
Java Code:int temp = inData[current];
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-01-2012, 04:25 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
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
-
Re: Modifying Given Code
Questions to ask yourself:
- What is inData an array of?
- Does the < operator make sense for this data type?
- 04-01-2012, 05:54 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
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.
- 04-01-2012, 06:13 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Modifying this app.
By seaisle68 in forum Java AppletsReplies: 5Last Post: 04-07-2011, 04:12 PM -
Modifying output
By sqlsql0 in forum New To JavaReplies: 1Last Post: 02-28-2011, 12:33 PM -
Modifying Automato Code
By whateverme in forum New To JavaReplies: 0Last Post: 02-06-2011, 03:36 PM -
Need help modifying code for hex conversion
By gamer765 in forum New To JavaReplies: 14Last Post: 10-25-2010, 07:59 AM -
One Jspinner modifying the value of the other
By cotarelo in forum AWT / SwingReplies: 1Last Post: 06-11-2010, 03:48 PM
Bookmarks