## CS 201 - Homework 2 - Asymptotic run-time analysis and OO programming

#### Due: Wednesday 9/27 at 8am

This assignment has three written problems and two programming problems. Submit the written answers on paper at the beginning of class on Wednesday, and the programs (PasswordAccount.java and Buggle.java) using the HW 2 submission page.

### Problem 1: Asymptotic Notation

Consider the following six functions:

1. a(n) = 2n log2(n) + n + 5
2. b(n) = 3n2 + 7n + 4
3. c(n) = 17
4. d(n) = 2n + 1
5. e(n) = log3(n)
6. f(n) = 3n -2

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

1. O(n)
2. O(n2)
3. O(log2(n))
4. O(3n)

### Problem 2: Recurrence Equations

For each of the recurrence equations below:

1. Give a closed form solution to the equations
2. 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(n2)

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 nth power. For each function, give the following:

a. A recurrence equation that approximates the worst-case running 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.

• log2(a) + log2(b) = log2(a*b)
• log2(a) - log2(b) = log2(a/b)
• n! = n*(n-1)*(n-2)*... 3*2*1
• log2(n!) is an element of O(n*log2(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.
}
}```

### Problem 4: Bank Accounts

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 BankAccountTest
```
Study 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'
*** trying to print jane's balance, password 'apple'
*** trying to withdraw \$200 from john, password 'pare'
*** 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'
```
Note: to compare two strings s1 and s2 in Java, use
```    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.

### Problem 5: Buggles

A "Buggle" is a creature that moves on an infinite grid in the x-direction or y-direction, and has an orientation east, north, west, or south. (Unlike the Buggle example shown in class, we will not represent the color or the brushUp / brushDown property here.) A buggle can respond to commands to move one or more grid cells forward, or stay in the same location and turn 90 degrees to the left or right from its current orientation. The state of a Buggle can always be described by giving its x-coordinate, y-coordinate, and direction (EAST, NORTH, WEST, or SOUTH). When a new Buggle is constructed, it should be at coordinates (1, 1) facing EAST. Note that (1, 1) are the coordinates of the bottom left cell in the grid (unlike pixel coordinates, where the origin is in the top left).

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
```

#### Naming conventions and documentation

This is a reminder that classes requested for assignments should always be named as in the assignment. This is necessary so I can test your work easily. Also, Java programmers tend to follow certain naming conventions. Constants are all upper-case, classes begin with upper-case, variable and method names begin with lower case, using upper-case for multi-word names ("camelCase"). You should also follow these conventions. Finally, names help the reader understand what you have written.