Computer Science APEA 15-100, Summer 2009
Homework 9
Due:  Wed 15-Jul-2009 at 8:59am (email copy only)
(no late submissions accepted).


Read these instructions first!


  1. Finish Lab 9
  2. dotProduct
  3. rotate
  4. finalPosition

  1. Finish Lab 9
    Finish all the examples from lab 9.  Include the solutions in your submission, all in one file named Lab9.java.  Note: the lab collaboration policy continues to apply to the lab problems.  So you can collaborate at will to solve these problems.  This only applies to the lab problems -- all other problems in this assignment are subject to the usual homework collaboration policy.
     
  2. dotProduct
    Write the following method:
       public static int dotProduct(int[] a, int[] b)
    This method takes two (possibly-null) arrays of int's and returns the dot product of those two arrays.  If this is not possible, the method returns -1.  For more info, see the Wikipedia page on dot products.  Write your own test method.
     
  3. rotate
    Write the following method:
       public static int[] rotate(int[] a, int n)
    This method takes a (possibly-null) array of int's "a" and an int "n", and returns a new array which is the result of rotating the elements in the original array n places to the right with wraparound.  So, for example, if "a" equals { 1, 2, 3, 4, 5 }, then rotate(a,2) would return the array { 4, 5, 1, 2, 3 }.  If "a" is null, the method should return null.  Write your own test method.
     
  4. finalPosition
    Write the following method:
       public static int[] finalPosition(int[] dirs)
    This method takes a (possibly-null) array of int's representing directions (where  UP is 1, DOWN is 2, RIGHT is 3, and LEFT is 4), and returns the (x,y) coordinates that results from starting at the origin and following each of these directions in sequence.  So, if the array contains { 1, 3, 2, 3 }, this indicates to start at (0,0), go up to (0,1), right to (1,1), down to (1,0), and right to (2,0).  These values are returned in an array, so the result would be the array { 2, 0 }.  If  the "dirs" array is null, the method should return null.  Write your own test method.

Carpe diem!