Exam 2 ReviewMultidimensional ArraysTwo Dimensional Arrays in JavaSetting 2D Array Values at Compile TimeMatrix AdditionFunctions/Static MethodsComponents of a FunctionThings to Remember About FunctionsScope2.2 Libraries and ClientsLibrariesKey Points to RememberRecursionSlide 14How-To’s on Writing Recursive FunctionsRecursion Exercise FactorialRecursion Exercise Fibonacci NumbersObjectsThings to Remember About ObjectsExam 2 ReviewMultidimensional Arrays3Two Dimensional Arrays in JavaArray access. Use a[i][j] to access element in row i and column j. Zero-based indexing. Row and column indices start at 0.double[][] a = new double[10][3];for (int i = 0; i < 10; i++) { for (int j = 0; j < 3; j++) { a[i][j] = 0.0; }}Declaring and Initializing a 10-by-3 Array4Setting 2D Array Values at Compile TimeInitialize 2D array by listing values. double[][] p = { { .02, .92, .02, .02, .02 }, { .02, .02, .32, .32, .32 }, { .02, .02, .02, .92, .02 }, { .92, .02, .02, .02, .02 }, { .47, .02, .47, .02, .02 }, };5Matrix AdditionMatrix addition. Given two N-by-N matrices a and b, define cto be the N-by-N matrix where c[i][j] is the sum a[i][j] + b[i][j]. double[][] c = new double[N][N];for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) c[i][j] = a[i][j] + b[i][j];Functions/Static MethodsComponents of a FunctionThings to Remember About FunctionsHow many values can a function return?Zero or OneFunctions can be overloadedDifferent argument typesDifferent number of argumentsDifferent return value is NOT overloadingFunctions are usually called by valueArgument values are copied from function call-site to the function bodyThese arguments are local variables in the functionWhat is the other way of calling functions?Call by Reference9ScopeScope (of a name). The set of statements that can refer to that name.public class Newton { public static double sqrt(double c) { double epsilon = 1e-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) { double x = sqrt(a[i]); StdOut.println(x); } }}two differentvariables withthe same name iscope of cscope of epsilonscope of t2.2 Libraries and ClientsIntroduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · 1/14/19 1/14/1911LibrariesLibrary. A module whose methods are primarily intended for useby many other programs.Client. Program that calls a library.API. Contract between client andimplementation.Implementation. Program thatimplements the methods in an API.Key Points to Remember•Create a class file with all your functions•Use the standard Save -> Compile sequence•This class file does NOT have a main() function•Use the name of the class to invoke methods in that library•Can do this from any Java program12Recursion14RecursionWhat is recursion? When one function calls itself directly or indirectly.Gcd. Find largest integer d that evenly divides into p and q.base casereduction steppublic static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q);}base casereduction step,converges to base case15How-To’s on Writing Recursive FunctionsBase Case: You must check if we’ve reached the base case before doing another level of recursion! Make Progress Towards Base Case:Your recursive calls must be on a smaller or simpler input. Eventually this must reach the base case (and not miss it).Multiple recursive calls:Sometimes more than one recursive call.–H-Tree, Towers of HanoiAre their return values chosen, combined?Recursion ExerciseFactorialn! = n*(n-1)! (Reduction Step)0! = 1 (Base Case)public static int factorial( int n )J {JJJ if ( n <= 0 )J // base caseJJJJ return 1;JJJ elseJJJ // reduction stepJJJJ return ( n * factorial ( n - 1 ) );}Recursion ExerciseFibonacci NumbersF(N) = F(N-1) + F(N-2) (Reduction Step)F(0) = 0; F(1) = 1 (Base Case)public static long Fib(int n) {if (n == 0) return 0;if (n == 1) return 1;return Fib(n-1) + Fib(n-2);} What is the running time (computational cost) of this program?Draw the recursion treeObjectsThings to Remember About ObjectsFor this exam, you need to know only how to USE objectsClass APIConstructorInstance MethodsDifference between constructors and
View Full Document