TA office hours are over after this week, but Dan and Maja will still be around for the next month Feel free to e-mail me to request an appointment. My schedule is flexible. We will have more office hours before the exam, too. These will be announced on WebCT. 1
Recursion: solve a problem of size n by solving smaller instances of that same problem using them to build the solution for n
Example Write a recursive method m() that computes the partial sum of the following series: 1/3 + 2/5 + 3/7 + 4/9 +... + i / (2i + 1) In other words, m(2) should return result of 1/3 + 2/5 m(3) should return result of 1/3 + 2/5 + 3/7 m(0), m(-1) are undefined 3
Example Write a recursive method that computes the partial sum of the following series: 1/3 + 2/5 + 3/7 + 4/9 +... + i / (2i + 1) Idea: to compute m(n), assume I know how to compute m(n-1): m(n) = n/(2n + 1) + m(n-1) 4
Example Write a recursive method that computes the partial sum of the following series: 1/3 + 2/5 + 3/7 + 4/9 +... + i / (2i + 1) Idea: to compute m(n), assume I know how to compute m(n-1): m(n) = n/(2n + 1) + m(n-1) Base case (simplest case, no recursive call) m(1) = 1.0/3 5
Example Write a recursive method that computes the partial sum of the following series: 1/3 + 2/5 + 3/7 + 4/9 +... + i / (2i + 1) How it works for m(4): m(4) = 4/(2*4 + 1) + m(3) m(3) = 3/(2*3 + 1) + m(2) m(2) = 2/(2*2 + 1) + m(1) m(1) = base case = 1/3 6
Recursive program + try-catch blocks + command-line parameters Write a class with two methods: catalan() and main() catalan() is a recursive method that computes the n th Catalan number: C(0) = 1 C(n) = (4*n -2)/(n+1)*C(n-1) The main() method reads a String from the command-line, converts it to an integer with Integer.parseInt(), calls the catalan() method on it and prints the result. If the number of arguments is not 1, if the given argument is < 1 or if parseint() throws an exception print something like Usage: java RecursiveCatalan <positive-integer> Solution: See RecursiveCatalan.java 7
Another recursive program + arrays Write a recursive program that, given an array of integers (the integers can be negative, 0, positive), returns the largest number in the array. For solution, see Exercise 20_13.java 8
2D arrays 9
Common midterm error: jagged arrays int[][] a = new int[2][1]; int[] a0 = {1,2,3; int[] a1 = {4; a[0] = a0; a[1] = a1; The above example is legal: it compiles and runs fine. (But on the midterm, lots of students stated that this kind of operation is not allowed.) a[0] contains an address pointing to an array of size 1. We can't change the size of the array a[0] points to, but we can change the content of a[0] so that it points to another array (of any size). This is what's happening on the second-to-last line of the example above. 10
exercise: 2D arrays Write a method that accepts an int[][] called img as parameter and returns a new int[][] that is the transpose of img {{ 1, 2, 3, { 4, 5, 6 produces { { 1, 4, {2, 5, {3, 6 11
Solution public static int[][] transpose(int[][] img){ int width = img.length; int height = img[0].length; int[][] newimg = new int[height][width]; for(int y = 0; y < height; y++) for(int x = 0; x < width; x++) newimg[y][x] = img[x][y]; return newimg;
exercise: more 2D arrays [no solution provided (compile and test your code to see whether it's doing the right thing. having trouble? drop by during office hours)] Write a method that (given a 2D int array called img, (x,y) coordinates, a vertical distance and a horizontal distance), returns a new array that is a cropped version of img You may assume that all the parameters are within the bounds of the array e.g. given coordinates 2,2 and v distance 1 and h distance 0 { { 0, 0, 0, 0, produces { 0, 0, 0, 0, { 0, 0, 0, 0, { 0, 0, 0, 0 { {0, {0, {0 13
Extra exercises 14
a longer exercise for ArrayLists in picture-collection.pdf This exercise is adapted from an old assignment, and the solutions to that assignment are provided in w13-thursday.zip. You will not be able to compile or run this code, unfortunately. (Since some files are missing.) The solutions do not match the instructions in picture-collection.pdf exactly, but they're close. For good practice with file i/o, try to write code to match the specification found in picture-collection/docs/simplecollectionloader.html and picturecollection/docs/simplecollectionwriter.html 15
What will this print? public class MyInteger { private int x; public MyInteger(int myx) { x = myx; public double add(int y, int z) { System.out.println("add(i,i): " + x + " + " + y + " + " + z); return x + y + z; public double add(double y, double z) { System.out.println("add(d,d): " + x + " + " + y + " + " + z); return x + y + z; public static void main(string[] args) { MyInteger n1 = new MyInteger(1); MyInteger n2 = new MyInteger(2); double r1, r3; r1 = n1.add(3, 4); r3 = n1.add(5, 6.0); System.out.println("n1.add(3, 4) == " + r1); System.out.println("n2.add(3, 4) == " + n2.add(3, 4)); System.out.println("n3.add(5, 6.0) == " + r3);
Solution add(i,i): 1 + 3 + 4 add(d,d): 1 + 5.0 + 6.0 n1.add(3, 4) == 8.0 add(i,i): 2 + 3 + 4 n2.add(3, 4) == 9.0 n3.add(5, 6.0) == 12.0
Command-line parameters What is displayed by the following program if we run it like this: java Cat java Cat 4 5 java Cat \ 4\ java Cat 1 2 3 public class Cat{ public static void main(string[] elephants){ String result = ""; for(int i = 0; i < elephants.length; i++){ result = result + elephants[i]; System.out.println(result);
This is the output at my terminal mfrydr@jack:~$ java Cat 1 2 3 123 mfrydr@jack:~$ java Cat mfrydr@jack:~$ java Cat \"4\" "4" mfrydr@jack:~$ java Cat "4" 5 45
class UselessObject{ int evennumber; int oddnumber; public UselessObject(){ evennumber = 6; oddnumber = 1; public int getsum(){ return evennumber + oddnumber; public void incrementall(int step){ if(step%2 == 0){ evennumber = evennumber + step; oddnumber = oddnumber + step;
What is printed when we run ObjectExercise? public class ObjectExercise{ public static void main(string[] args){ UselessObject a = new UselessObject(); UselessObject b = new UselessObject(); a.evennumber = -4; b.incrementall(4); System.out.println(a.getSum() - b.getsum());