CS 226 Homework 2 Written (1-6): Due Friday February 9 (in class). Programming (7): Due Monday Febrary 12. 1. Order the following functions by asymptotic growth rate (* denotes multiplication; ^ denotes power): a. 4*n*log(n) + sqrt(n) b. 2^(log(n^2)) c. 3*n + 20 log n^2 d. n^3 e. n*log(n) f. 2^200 2. If d(n) is O(f(n)) and e(n) is O(g(n)), what is the order of d(n)*e(n)? Please prove your answer. 3. Show that if a(n) is O(f(n)) and b(n) is O(g(n)) and f(n) is O(g(n)) then a(n)+b(n) is O(g(n)). 4. Show that if p(n) is a polynomial in n, then log(p(n)) is O(log(n)). 5. Show that n^2 is Omega(n*log(n)) 6. Consider the following pseudocode: Input: Arrays A and B storing n integers Output: The number of elements of B equal to the prefix sums of A c = 0 for i = 0 to n -1 do { s = 0 for j = 0 to n-1 do { s = s+A[0] for k = 1 to j do s = s + A[k] } if B[i] == s then c = c+1 } return c a. Write the number of operations in each line of code. b. Compute a reasonable estimate or bound on the number of operations incurred by each line of code when executed (number of operations times number of times executed) c. Provide an asymptotic bound on the runtime 7. In class, we talked about using forwarding as a way to simulate multiple inheritance. Redo the code for the graphics example discussed in class to use use forwarding. Specifically, create a class implementing RotatableObject and ColoredObject, then use those classes in the implementations of Rectangle and Circle. In your submission, highlight the declaration of each of your "mixin" classes, and the definition of Rectangle and Cirlce using forwarding. My original code is included below: interface BasicGraphicsObject { // the name of this thing String Name(); // place the object somewhere void Place(int x, int y); // draw the object void Draw(); // erase the object void Erase(); } interface ColoredObject extends BasicGraphicsObject { // Set color of an object by explicit r/g/b triple void SetColor(char r, char g, char b); } interface RotatableObject extends BasicGraphicsObject { // Incremental rotation in degrees void RotateObject(double angle); } abstract class GenericGraphicsObject implements BasicGraphicsObject { int xpos; int ypos; GenericGraphicsObject() {xpos=ypos=0;} public abstract String Name(); public void Place(int x, int y) { xpos = x; ypos = y;} public void Draw() {System.out.print("Drawing " + Name() + ".\n");}; public void Erase() {System.out.print("Erasing " + Name() + ".\n");}; } class Star extends GenericGraphicsObject { public String Name() {return "Star";} }; class Rectangle extends GenericGraphicsObject implements ColoredObject, RotatableObject { public String Name() {return "Rectangle";} public void SetColor(char r, char g, char b) {System.out.print("Setting Color of " + Name() + " to " + (int)r + " " + (int)g + " " + (int)b + "\n");} public void RotateObject(double angle) {System.out.print("Rotating " + Name() + " through " + angle + ".\n");} }; class Circle extends GenericGraphicsObject implements ColoredObject { public String Name() {return "Circle";} public void SetColor(char r, char g, char b) {System.out.print("Setting Color of " + Name() + " to " + (int)r + " " + (int)g + " " + (int)b + ".\n");} } class GraphicsExample { static public void main(String input[]) { RotatableObject r = new Rectangle(); ColoredObject c = new Circle(); GenericGraphicsObject s = new Star(); BasicGraphicsObject x[] = new BasicGraphicsObject[3]; x[0] = r; x[1] = c; x[2] = s; for (int i = 0;i< 3; i++) { x[i].Place(i,i); if (x[i] instanceof ColoredObject) { ((ColoredObject)x[i]).SetColor((char)(i*2),(char)(i*4),(char)(i*6)); } if (x[i] instanceof RotatableObject) ((RotatableObject)x[i]).RotateObject(i*10); x[i].Draw(); } } }