Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exam 2 Review 1.

Similar presentations


Presentation on theme: "Exam 2 Review 1."— Presentation transcript:

1 Exam 2 Review 1

2 Multidimensional Arrays
2

3 Two Dimensional Arrays in Java
Array 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 Array

4 Setting 2D Array Values at Compile Time
Initialize 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 }, };

5 Matrix Addition Matrix addition. Given two N-by-N matrices a and b, define c to 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];

6 Functions/Static Methods
6

7 Components of a Function

8 Things to Remember About Functions
How many values can a function return? Zero or One Functions can be overloaded Different argument types Different number of arguments Different return value is NOT overloading Functions are usually called by value Argument values are copied from function call-site to the function body These arguments are local variables in the function What is the other way of calling functions? Call by Reference

9 two different variables with the same name i
Scope Scope (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); scope of c scope of epsilon scope of t two different variables with the same name i 9 9

10 2.2 Libraries and Clients Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © · November 25, :00 tt 10

11 Libraries Library. A module whose methods are primarily intended for use by many other programs. Client. Program that calls a library. API. Contract between client and implementation. Implementation. Program that implements the methods in an API. Library houses the methods/functions. Client program calls these methods. To use the library, you need to know the API. API of the standard libraries are available online. Look at the Math and Arrays libraries. Explain what the “.” notation in “System.out.println” means 11 11

12 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 program 12 12

13 Recursion 13

14 Recursion What is recursion? When one function calls itself directly or indirectly. Gcd. Find largest integer d that evenly divides into p and q. base case reduction step, converges to base case 1) What is your first impression of this code? public static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } base case reduction step 14 14

15 How-To’s on Writing Recursive Functions
Base 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 Hanoi Are their return values chosen, combined? How would you implement gcd? 15 15

16 Recursion Exercise Factorial
n! = n*(n-1)! (Reduction Step) 0! = 1 (Base Case) public static int factorial( int n )  {     if ( n <= 0 )  // base case      return 1;     else    // reduction step      return ( n * factorial ( n - 1 ) ); }

17 Recursion Exercise Fibonacci Numbers
F(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 tree

18 Objects 18

19 Things to Remember About Objects
For this exam, you need to know only how to USE objects Class API Constructor Instance Methods Difference between constructors and methods


Download ppt "Exam 2 Review 1."

Similar presentations


Ads by Google