Comp 410, Program 2, isPalindrome method The method header omitted in all code examples. 1) Two sample solutions int left = 0; int right = str.length()-1; while( left < right ) { if( str.charAt(left) != str.charAt(right)) return false; left++; right--; } return true; The following is more concise, but less clear and I'm not sure about the mixing of 'int' and 'double' in determining the midpoint of the array for(int index = 0; index < 0.5*(str.length()); index++ ) if(str.charAt(index) != str.charAt(str.length()-1-index)) return false; return true; 2) There is a subtle but jarring asymmetry in the following int r = str.length()-1; for(int l = 0; l < r; l++ ) { if( str.charAt(l) != str.charAt(r)) return false; r--; } return true; Note that the indices l and r have exactly the same function but are delcared, initialized and updated differently. Code should be symmetrical. Compare to the symmetrical code int l = 0; int r = str.length()-1; while( l < r ) { if( str.charAt(l) != str.charAt(r)) return false; r--; l++; } Which was written by a master programmer? As an aside, we don't need an 'else' clause because of the return statement. However, writing this as if( str.charAt(l) != str.charAt(r)) return false; else { l++; r--; } is just fine. Its a matter of personal style. 3) This is correct BUT int l = 0; int r = str.length()-1; boolean palindrome = true; while( l < r ) { if( str.charAt(l) != str.charAt(r)) palindrome = false; else { l++; r--; } } // So far, so good but now comes if(palindrome) SOP("Is plaindrome"); else SOP("Is not a palindrome"); return palindrome; Who said anything about printing? Thats is not the responsibility of this method. Suppose (a big stretch) that WORD not only checked the spelling of each word in a document but used the above code to check if each word is a palindrome? 4) The following is correct but what needs to be done in the Finish step? for(int index = 0; index < str.length; index++ ) if( str.charAt(index) != str.charAt(str.length()-1-index) return false; return true; 5) Correct but unnecessary is boolean palindrome; .......... if(palindrome == true) return true; else return false; We could simply write if(palindrome) return true; else return false; but more to the point is return palindrome; All 3 of these do exactly the same thing. 6) Don't repeat yourself. Let your code speak for itself // if the characters are not the same then // it is not a palindrome if( str.charAt(index) != str.charAt(str.length()-1-index) return false; 7) I want everyone to write on the blackboard 500 times "I will use good names" "I will use good names" Compare the following boolean loopRunning = true; while( loopRunning && left < right ) { if( str.charAt(index) != str.charAt(str.length()-1-index) loopRunning = false; ..... boolean done = false; while( ! done && left < right ) { if( str.charAt(index) != str.charAt(str.length()-1-index) done = true; ..... boolean palindrome = true; while( palindrome && left < right ) { if( str.charAt(index) != str.charAt(str.length()-1-index) palindrome = false;; ..... and all these are pretty good code. There were lots of variables named: i1,i2,temp,x,y,........ Here is an attempt, but the code lies int firstChar = 0; int lastChar = str.length()-1; while( ... ) if(str.charAt(firstChar) != str.charAt(lastChar) ) ...... Two things wrong with this: i) At the time of the initialization, yes, they denote the first and last characters of str, but, after initialization, they never again are the first and last - only the one on the left and the one on the right. ii) More importantly, firstChar is not a character at all. It is an index into a String. str.charAt(firsChar) is a character. 8) Inexact is boolean palindrome = true; while( palindrome && left < right ) { if( str.charAt(index) != str.charAt(str.length()-1-index) palindrome = false;; left++; right--; } return palindrome; This will get the correct answer, but if we may know the answer to the question being asked after the very first comparison. Why keep looping? 9) What is wrong with the following? Hint, its not the indexing. boolean isPalindrome = false; for(int index = 0; index < str.length; index++) { if(str.charAt(index) == str.charAt(str.length()-1-index)) isPalindrome = true: else isPalindrome = false; } return isPalindrome;