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!
- Now you are not given a file to start from! Based on previous
hw's, create your own Hw9.java file. Be sure to name it exactly
correctly, to include a main method that calls checkAssertsAreEnabled and
testAll, and to have testAll call suitable test methods for all the methods
you create. Also, be sure that every method you write has exactly the
right name and the right signature.
- Be sure to include your name, your Andrew ID, and your section clearly on the top of each file in your assignment.
- For non-programming problems:
- Place all your solutions to the non-programming problems in a single
file named Hw9 (with whatever extension is appropriate for the format you
choose, such as Hw9.txt or Hw9.html, etc). You must use one of these
file formats: plain text (txt), or RTF, or HTML, or Word (doc, not docx), or
PDF. No other file formats will be accepted.
- Show your work. Correct answers without supporting
calculations will not receive full credit.
- For programming problems:
- Try to use well-named variables, proper indenting, reasonable commenting,
etc.
- Note: You may not use Java concepts we have not yet covered,
including l
oops (do/while/for), conditionals
("if" statements or tertiary operators (?:)), arrays, or methods
from any classes in java.util.* (besides Scanner or others we explicitly
use) to solve these problems
(which isn't a problem for most of you, seeing as we have not yet covered
these!). While they may be helpful, every problem here is solvable without
them, and they are not permitted for now.
- What to submit
- Create a submission directory like this: "koz-hw9" (replace
"koz" with your andrew id)
- Place all the files you are submitting in that directory, zip that
directory, and submit the zipped directory
- How to submit
- Send an email with the zipped submission directory as an attachment
to your CA by the submission deadline. Do not miss the deadline,
even by one minute! Email problems are not a valid excuse
for late submissions.
- It is recommended
that you "cc" yourself in that email, too, just to confirm that you properly
sent the email.
- Note:
- Improper submissions will be penalized up to 10 points and may be
rejected.
- Late submissions will be rejected.
- Finish Lab 9
- dotProduct
- rotate
- finalPosition
- 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.
- 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.
- 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.
- 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!