Chapter 4, exercises 2 to 6 Really try to work out the solutions on paper, before you run a program to check your solution. 2. The looping statement in Java uses the "for" keyword, and consists of three parts. The first part is an initialization statement, which can also be used to declare the loop variable. The second part is a test for termination; the loop will execute as long as this expression returns true. The final part is the increment, which is a statement that is evaluated to update the loop variable. Consider the following main method. Describe the effect produced by the program when it is executed with three command-line arguments. public static void main (String[] args) { for (int i = 0; i < args.length; i = i + 1) System.out.println(args[i]); } 3. Now consider the following slightly more complex method: public static void main (String[] args) { for (int i = 0; i < args.length; i = i + 1) for (int j = 0; j <= i; j = j + 1) System.out.println(args[i]); } Describe the pattern of the output when this method is executed with three command-line arguments. 4. Consider the following main method: public static void main (String[] args) { String result = ""; for (int i = args.length - 1; i >= 0; i = i - 1) result = result + " " + args[i]; System.out.println(result); } What does this method do? What will be the result printed given the arguments "Sam saw Sarah said Sally"? 5. Another useful method provided by the class String is the substring() operation. This takes an integer argument, and returns the portion of the string that remains following the given index position. For example, if "word" is a variable containing the string "unhappy", then "word.substring(2)" returns the string "happy". This operation is used in the following method. What will the output be given the command-line argument "Sally"? public static void main (String[] args) { String name = args[0]; String shortName = name.substring(1); System.out.println(name + "," + name + ", bo-B" + shortName); System.out.println("Banana-fana Fo-F" + shortName); System.out.println("Fee, Fie, mo-M" + shortName); System.out.println(name + "!"); } 6. By placing the code shown in the previous question inside a loop, write a program that will take any number of command-line arguments, and write one verse of the name game for each.