//recall the HighLowGUI2 with the while loop... //let's limit the maximum number of guesses HighLowMaxGUI h2 = new HighLowMaxGUI(10); h2.makeGuess() //let's print the square of numbers from 1 to 4: System.out.println(1 * 1); System.out.println(2 * 2); System.out.println(3 * 3); System.out.println(4 * 4); //what if we want to do it for 1 to 300? //we need a loop! WhileDemo.printSquares(10) //printing a string backward: WhileDemo.printRString("ABCDE") String r r = WhileDemo.printRString("ABCDE"); //what's wrong? how we store the result back? //we need to write a method that reverses the string and returns the result as a string. //reversing a string: "abcd" -> "dcba" //starting from the end, we go backward and concat characters: // "d" // + "c" // + "b" // + "a" // end index: length - 1 // character at position i: s.charAt(i) WhileDemo.reverseString("Thursday") String r; r = WhileDemo.reverseString("Thursday") //let's do it another way (there are many different ways to do it //watch out for boundary condition in the loop WhileDemo.reverseString2("Thursday") int i = 0; while( i < 10) { System.out.println( i); i++; } /* we can do the above while loop using "for loop": * for(initialization; condition; updatestatement) { * body * } */ for(int i = 0; 10 < 5; i++) { System.out.println(i); } //for loops are good to use when you know the range //example; decreasing for (int j=8; j>2; j--){ System.out.println(j); } //the following causes infinite loop, why? for (int j=8; j>2; j++){ System.out.println(j); } //let's do the string reversal using for loop. ForDemo.reverse("Today is Thursday.") ForDemo.reverse(null) //let's write a tester for reverse in ForDemo //we didn't cover this in lecture, but very simple!