Problem 1: Asymptotic Notation

Consider the following six functions:

- a(n) = 2n log
_{2}(n) + n + 5 - b(n) = 3n
^{2}+ 7n + 4 - c(n) = 17
- d(n) = 2
^{n}+ 1 - e(n) = log
_{3}(n) - f(n) = 3n -2

For each of the following sets, indicate which of the above functions are members of the set:

- O(n)
- O(n
^{2}) - O(log
_{2}(n)) - O(3
^{n})

Problem 2: Recurrence Equations

For each of the recurrence equations below:

- Give a closed form solution to the equations
- Classify the solution to the equations using O(g) notation.

As an example, the recurrence equations for selection sort on an array of n elements would be:

T(n) = n + T(n - 1), n > 0,

T(0) = 0

(Assume that finding the minimum of n elements takes n operations.) The closed-form solution to the recurrence equation can be derived as follows:

T(n) = n + T(n-1)

= n + (n - 1) + T(n-2)

= n + (n - 1) + (n - 2) + T(n-3)

...

= n + (n -1) + (n - 2) + (n - 3) + ... + 3 + 2 + 1

= n(n+ 1)/2 is an element of O(n^{2})

**a. **

T(n) = 3 + T(n - 1), n > 0

T(0) = 0

**b.**

T(n) = 3n + T(n - 1), n > 0

T(0) = 0

**c.**

T(n) = 1 + 3T(n - 1), n > 0

T(0) = 0

**d. **

T(n) = 1 + T(n/3), n ≥ 1

T(n) = 0, n < 1

**e. **

T(n) = n + T(n/3), n ≥ 1

T(n) = 0, n < 1

Problem 3: Running Time Analysis

Below are five class methods, all of which compute the value of 2
raised to the n^{th} power. For each function, give the following:

a.A recurrence equation that approximates theworst-caserunning time T(n) of the routine as a function of the input n. In all cases, you may assume that T(n) = 0 for n < 1 and that the time for the non-call operations within the body of each method is constant (for convenience, assume that the constant is 1).

b.A solution to the recurrence equation expressed in O(g) notation.

You may find the following facts helpful in some of your analyses.

- log
_{2}(a) + log_{2}(b) = log_{2}(a*b) - log
_{2}(a) - log_{2}(b) = log_{2}(a/b) - n! = n*(n-1)*(n-2)*... 3*2*1
- log
_{2}(n!) is an element of O(n*log_{2}(n))

public static int two1 (int n) { if (n == 0) { return 1; } else { return 2 * two1(n - 1); } } public static int two2 (int n) { if (n == 0) { return 1; } else { return two2(n - 1) + two2(n - 1); } } public static int two3 (int n) { if (n == 0) { return 1; } else { return two3(n - 1) + two1(n - 1); // Note call to two1. } } public static int two4 (int n) { // To simplify analysis, assume that the input to two4 is a power of 2. if (n == 0) { return 1; } else if (n % 2 == 0) { int x = two4(n / 2); return x * x; } else { return 2 * two4(n - 1); } } public static int two5 (int n) { if (n == 0) { return 1; } else { return two5(n - 1) + two4(n - 1); // Note call to two4. } }

This week's programming problems will introduce you to
object-oriented programming. In this first problem, you will be
working with a bank account class that is very similar to the
`Account` class discussed in lecture.

Start by creating a new directory
"hw2" in your "cs201" directory, and save the files `BankAccount.java` and `BankAccountTest.java`.
Compile both files, and run

> java BankAccountTestStudy the code and the output and make sure you understand what's going on.

Now, starting with the code in `BankAccount.java`, create a new
class `PasswordAccount.java` that implements a "secure"
account, which requires a password for creating an account, as well as
for invoking the `withdraw` and `printBalance` methods.
Use the file `PasswordAccountTest.java`
to test your new implementation.
A sample transcript of how your
`PasswordAccount` class should behave is shown here:

> java PasswordAccountTest *** Created two new accounts jane=Account #135 john=Account #277 *** depositing $50 to jane Deposited $50.0. *** trying to deposit -$80 to jane You must deposit a positive amount of money. *** trying to print jane's balance, password 'aapl' Sorry, incorrect password. *** trying to print jane's balance, password 'apple' Your balance is $2050.0. *** trying to withdraw $200 from john, password 'pare' Sorry, incorrect password. *** trying to withdraw $200 from john, password 'pear' Withdrew $200.0. *** trying to withdraw $500 from john, password 'pear' Sorry, you must withdraw a positive amount of money up to $300.0. *** trying to withdraw $300 from john, password 'pear' Withdrew $300.0. *** depositing $2000 to john Deposited $2000.0. *** trying to print john's balance, password 'pear' Your balance is $2000.0.

if (s1.equals(s2)) ....If you use == for comparison, you are testing whether s1 and s2 are the same object, which might not be true.

Develop a class called `Buggle` that implements these creatures. Your
code should provide functionality for the following methods:

// construct a new Buggle public Buggle() // move forward one grid cell public void forward() // move forward 'k' grid cells public void forward(int k) // rotate 90 degrees to right (EAST -> SOUTH -> WEST -> NORTH -> EAST) public void right() // rotate 90 degrees to left (EAST -> NORTH -> WEST -> SOUTH -> EAST) public void left() // return a string representation of the object public String toString()

Write another class `BuggleTest` to test your `Buggle` class.
Copy and paste the following code into the main method:

// create a Buggle Buggle becky = new Buggle(); // print it (this invokes the toString() method) System.out.println("becky: " + becky); // move becky around a bit and keep printing it becky.forward(); System.out.println("becky: " + becky); becky.left(); System.out.println("becky: " + becky); becky.forward(5); System.out.println("becky: " + becky); // create another Buggle and print it Buggle bobby = new Buggle(); System.out.println("bobby: " + bobby); // move bobby without printing bobby.right(); bobby.forward(5); bobby.right(); bobby.forward(20); bobby.left(); // move becky some more becky.left(); becky.forward(3); // report final state of both Buggles System.out.println("becky: " + becky); System.out.println("bobby: " + bobby);

If you have implemented your Buggle class correctly, you should get the following output:

> java BuggleTest becky: x=1, y=1, direction=EAST becky: x=2, y=1, direction=EAST becky: x=2, y=1, direction=NORTH becky: x=2, y=6, direction=NORTH bobby: x=1, y=1, direction=EAST becky: x=-1, y=6, direction=WEST bobby: x=-19, y=-4, direction=SOUTH