(a) Classify each of the following as a pure substance, a solution, or a heterogeneous mixture: a gold coin, a cup of coffee, a wood plank, (b) What ambiguities are there in answering part (a) from the descriptions given?

Chapter 12: Recursion Recursion – defining an operation in terms of itself o solve a large problem by solving smaller instances of the same problem Recursion in programming – writing methods that call themselves o a good substitute for loops A non-programming example: find out how many people are sitting directly behind you, if you can’t see all the way to the back of the room o rather than 1 person solving the entire problem (like with a loop), each person solves a small part (recursion) o ask the person behind you if someone is sitting behind him – for everyone who answers “yes” add 1; stop when the final person answers “no” Recursion algorithms have 2 cases: 1. Base case – simple occurrence, directly answerable. Usually when a number n = 0 or 1 2. Recursive case – not directly answerable. Can apply to any number n Some problems will have multiple base or recursive cases. You need at least 1 of each Example: a method that prints a line of n stars, without using loops: o Base case – if n = 0, print nothing and end output. o Recursive case – if n > 0, print a star and call the method again for n-1, over and over until n = 0 public static void printStars(int n) { if (n == 0) { //base case, n=0, ends output System.out.print(); } else { //recursive case, prints 1 more star System.out.print(“*”); printStars(n-1); //n passed to the method is decreased by 1 } } This method calls itself and prints a star for each integer from n to 1, then when n = 0 the method ends. ***I noticed that this method will result in an infinite loop if n is a negative number. It doesn’t make sense to enter a negative number, but anyway, this can be fixed by changing line 2 to: if (n <= 0) { Here’s a trace of what happens when printStars(3); is called: printStars(3); //n>0, execute else statements System.out.print(“*”); printStars(2); //n>0, execute else statements System.out.print(“*”); printStars(1); //n>0, execute else statements System.out.print(“*”); printStars(0); //n==0, execute if statement System.out.println(); Infinite recursion – writing a method that never finishes executing (like an infinite loop – not good!) o Example: public static void writeStars(int n) { writeStars(n); } What is the output of calling the following code mystery(648); public static int mystery(int n) { if (n < 10) { return n; } else { int a = n / 10; int b = n % 10; return mystery(a + b); } } mystery(648) // 648 > 0, execute else statements a = 64; b = 8 return mystery (72); // 72 > 0, execute else statements a = 7; b = 2 return mystery(9); // 9 < 0, execute if statement return 9. What is the output of the following code mystery(348); public static int mystery(int n) { if (n < 10) { return (10 * n) + n; } else { int a = mystery(n / 10); int b = mystery(n % 10); return (100 * a) + b; } mystery(348); // 348 > 10, execute else statements a = mystery (348 / 10); // 34 > 10, execute else statements a = mystery(34 / 10); // 3 < 10, execute if statement return 10 * 3 +3; // a = 33 b = mystery(34 % 10); // 4 < 10, execute if statement return 10 * 4 +4; // b = 44 return (100 * 33) +44; // a = 3344 b = mystery(348 % 10); // 8 < 10, execute if statement return 10 * 8 + 8; // b = 88 return (100 * 3344) + 88; A recursive method pow that accepts an integer base and integer exponent and returns the base raised to that exponent o Base step – any number raised to the 0 power = 1 o Multiply the base times itself exponent number of times public static int pow(int base, int exponent) { if (exponent == 0) { return 1; } else { return base * pow(base, exponent – 1); } } A trace of pow(2, 3); pow(2, 3); // 3 > 0, execute else statement return 2 * pow(2, 2); // 2 > 0, execute else statement return 2 * pow (2, 1); // 1 > 0, execute else statement return 2* pow(2, 0) // 0 == 0, execute if statement return 1; return 2; return 4; return 8;