// APS101, Winter 2009: Lecture 21 (Mar. 3) // // Review: last time we did some more work with while loops. // we wrote two versions of method "reverse" in WhileDemo.java // // what was the difference between the two? // (the same idea, but different ways to do it: // 1. start from last character and build new reversed String // 2. start from first character and build new reversed String) // // when working with indices (like index i or j), it's better to use // for loops, rather than while loops. // (as review of the for-loop syntax, we wrote method printSquares in ForDemo.java) // // NOTE: you don't have to use the index in a for loop! For example: for (int i = 0; i < 5; i++) { System.out.println("You don't have to use index i anywhere in the body!"); } // we also wrote the isPalindrome method in WhileDemo. We ran into 2 problems: // 1. dealing with case ("R" vs. "r"): use String's toLowerCase() method // 2. dealing with extra spaces ("racecar "): use String's trim() method // (recall testing principles: take a look at TesterFor.java from last lecture) // Algorithm: a sequence of steps (operations) that the computer needs to perform in order to complete the task it is given. // Computational Efficiency: completing the task using the least amount of resources possible - i.e. least amount of time and memory. // the current isPalindrome algorithm in WhileDemo is inefficient. Why? // (consider really long Strings of several hundred characters... // you have to reverse the whole String first, then compare it with the original) // A more efficient way to do it (general idea): // - compare the 1st character in the String with the last; see if they're the same. // - if they're not, stop and return false; if they are, keep going. // - compare the 2nd character in the String with the 2nd last... etc. // - once all characters have been compared (and if the method didn't return false), // then return true. // Algorithm 1 (using a while loop and String manipulation): // - start with a String: ex. "racecar" // - compare first character ("r") with last character ("r") // - if they're different, stop; // - if they're the same, discard the first and last characters: // "racecar" -> "aceca" // and keep going. // - the loop runs until: // - 2 characters are different (i.e. remaining String is AT LEAST 2 chars long) // - the String is empty ("") // - the String has one character ("e") // - once the loop finishes, check the length of the remaining String: // - if length > 1, return false (because that means some 2 characters were different) // - if length <=1, return true (because that means no characters were found to be different) // (see isPalindrome2 in WhileDemo) // Algorithm 2 (using a for loop with one index i): // - start at the first character (initialize index i to 0) // - update i by 1; run the loop until i = String's length / 2 // - inside the loop, keep comparing 2 characters: // - use i to index characters from the beginning // - use (String length - 1 - i) to index characters from the end // - if at any point in the loop 2 characters are found to be different, return false. // (note: whenever a "return" statement is executed inside a method - it doesn't matter where, or whether it's inside a loop - then the method stops.) // - if the loop stops and the method didn't return false, that means the String is a palindrome, // so return true. // (see isPalindrome in ForDemo) // we also looked at method "mystery" in WhileDemo: // we traced the code to see what it does - specifically, to see what values index i and j take on. // (we'll get more practice with this next time, because you'll see questions like these on your final).