Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 3 Test: Friday.

Similar presentations


Presentation on theme: "Unit 3 Test: Friday."— Presentation transcript:

1 Unit 3 Test: Friday

2 AP Exam Takers… Have you bought the Barron’s book?
Are you studying for 1 hour per night, at least?

3 ArrayList z = new ArrayList( );
z.add(1, “dog”); // result? This code will compile with no error, but there will be a runtime error: IndexOutOfBoundsException: Index: 1, Size: 0 Even though an ArrayList shrinks and grows, you still cannot access an index that does not exist.

4 public class Parent { private char letter = ‘a’; public int age = 18; // violates encapsulation! public double getGPA( ) { return 3.5; } } public class Child extends Parent { public void doStuff( ) { System.out.print(super.age); // valid, but it // violates encapsulation. double x = super.getGPA() // valid char y = super.letter; // not valid

5

6

7

8

9 for-each loops are often used with ArrayLists.
Open ForEachLoopDemo

10 Review: static methods
A static method can be called without having to create an object. It is called directly by using the class name (example: SavitchIn.readLine( ) ) It makes sense to make a method static when that method doesn’t have to be unique for each object that calls it. Demo: StaticMethodClass, StaticMethodClient

11 Static variables A variable can also be declared with the keyword static. Just like a static method, a static variable is not accessed through an object. It is accessed by using the name of the class itself. Why use a static variable? When that variable is not unique to each object of the class; when it is the same for all objects of the class. Now we know where instance variables get their name: they are unique for every instance (every object) of the class. A static variable is also known as a class variable. Demo: StaticVariableClass & StaticVariableClient

12 Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine that computes data” Recursion is a programming technique in which a method calls itself to solve a problem

13 Recursive Definitions
Mathematical formulas often are expressed recursively N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusive This definition can be expressed recursively as: 1! = 1 N! = N * (N-1)! The concept of the factorial is defined in terms of another factorial until the base case of 1! is reached

14 public int getSum (int num)
{ if (num == 1) // base case return 1; else return (num + getSum (num - 1)); }

15 Infinite Recursion All recursive definitions must have a non-recursive part If they don't, there is no way to terminate the recursive path The non-recursive part is called the base case, and is implemented using an if-statement Recursion without a base case causes infinite recursion This problem is similar to an infinite loop, and will cause a StackOverflowError exception

16 Recursive Programming
A method in Java can invoke (call) itself; if set up that way, it is called a recursive method The code of a recursive method must be structured to handle both the base case and the recursive case Each call to the method sets up a new execution environment, with new parameters and new local variables As always, when the method execution completes, control returns to the method that invoked it (which may be an earlier invocation of itself)

17 Demos: RecursionClass RecursionClient

18 Fibonacci Numbers The Fibonacci Sequence is a series of numbers where a number is found by adding up the two numbers before it. 0, 1, 1, 2, 3, 5, 8, 13 ….. On board: list the nth Fibonacci number. How can we write a recursive method to find the nth Fib #?

19 public int fib (int n) { if (n <= 1) // base case return n; else return fib(n-1) + fib(n-2); }

20 Recursion vs. Iteration (looping)
Just because we can use recursion to solve a problem, doesn't mean we should Every recursive algorithm (process for solving a problem) can be written iteratively (using a loop). Sometimes a loop is easier to understand, and more efficient Nevertheless, recursive solutions often are more simple and efficient than iterative solutions You must be able to determine when recursion is the correct technique to use

21

22 Assignments In a class called Recursion2, write the following methods:
factorial( ) -- receives an int parameter, return the factorial exponent( ) – receieves 2 int parameters, x and y, returns x to the y power Obviously, these must be recursive methods. You can’t use Math.pow( ) or any other method from the Math class. Now, write a client, Recursion2Client, to test the methods. Error check: other than the “x” (the base) in exponent( ), parameters must be positive. Complete 2014 Test (Sec 1: skip 5, 18, 23, 39, 40; Sec 2: Do #1-3)


Download ppt "Unit 3 Test: Friday."

Similar presentations


Ads by Google