CIT 590 Assignment 7: Array Operations
Spring 2009, David Matuszek
Purposes of this assignment:
- To get you started with JUnit 4
- To give you additional practice with arrays
- To help you understand APIs (Application Programmer Interfaces)
General idea of the assignment:
Write a number of methods for manipulating arrays. Test
all your methods thoroughly with JUnit 4.
There are a lot of methods listed below, but don't worry--they are all
very short, just a couple of for loops each. Most of your
work will be in writing the JUnit tests for them.
Note 1: There is no need for a main method
anywhere in this assignment. You are not writing a program, you are writing
a class that could be used by other programs.
Note 2: You will not be assigned a partner for this program. Please do it
alone.
Specific Methods:
public static int[] fill(int length)
- Creates an array of
length integers, and fills it with the numbers
1 to length.
Example: fill(12) returns |
|
public static int[] reverse(int[] array)
- Returns a new array whose values are in the reverse order of those in the given array.
Example: reverse( |
|
) returns |
|
public static int[][] rotateRight(int[][] array)
- Takes a two-dimensional input array and returns a new array which is "rotated"
a quarter-turn clockwise.
Example: rotateRight( |
|
) returns |
|
public static int[][] rotateLeft(int[][] array)
- Takes a two-dimensional input array and returns a new array which is "rotated"
a quarter-turn counterclockwise.
Example: rotateLeft( |
|
) returns |
|
public static int[][] transpose(int[][] array)
- Takes an input array of
m rows and n columns, and
transposes it to form an array of n rows and m columns.
The value in location [i][j] of the input array is copied into
location [j][i] of the new array.
Example: transpose( |
|
) returns |
|
public static int[][] ravel(int[] array, int n)
- Takes a one-dimensional input array of
m × n
numbers and returns a two-dimensional array of m rows and
n columns. The first n numbers of the given array are
copied into the first row of the new array, the second n numbers
into the second row, and so on. This method throws an
IllegalArgumentException if the length of the input array
is not evenly divisible by n.
Example: ravel( |
|
, 4) returns |
|
public static int[] unravel(int[][] array)
- Takes a
m by n two dimensional array and returns
a one-dimensional array of size m × n containing the
same numbers. The first n numbers of the new array are copied
from the first row of the given array, the second n numbers
from the second row, and so on.
Example: unravel( |
|
) returns |
|
public static int[][] reshape(int[][] array, int n)
- Takes a two-dimensional array of
r rows and c columns
and reshapes it to have (r*c)/n by n columns.
The value in location [i][j] of the input array is copied into
location [j][i] of the new array. This method throws an
IllegalArgumentException if r*c
is not evenly divisible by n.
Example: reshape( |
|
, 6) returns |
|
public static int[][] join(int[][] array1, int[][] array2)
- Takes two two-dimensional arrays, one with
n rows and
m1 columns, and the other with n rows and
m2 columns, and adjoins them to form a new array with
n rows and m1+m2 columns. This method throws an
IllegalArgumentException if the input arrays do not have the
same number of rows.
Example: join( |
|
, |
|
) returns |
|
public static char[] toChars(int[] array)
- Given an array of integers in the range 0 to 127, return an array of characters
with those ASCII values. This method throws an
IllegalArgumentException
if any of the integers are outside the range 0 to 127.
Example: toChars( |
|
) returns |
|
public static char[] String toString(int[] array)
- Given an array of integers in the range 0 to 127, return an String of characters
with those ASCII values. This method throws an
IllegalArgumentException
if any of the integers are outside the range 0 to 127.
Example: toString( |
|
) returns "Hello". |
public static int[] toInts(char[] array)
- Given an array of characters, return an array of their ASCII values.
Example: toInts( |
|
) returns |
|
public static int[] toInts(String string)
- Given a String, return an array of the ASCII values of the characters
in that String.
Example: toInts("Hello") returns |
|
How to do this assignment:
You can save yourself quite a bit of typing if you follow these steps:
- In Eclipse, create the project, the package, and the class.
- Write each of the methods as stubs--that is, write the
headers, but just have each method return
null.
- Use Eclipse to create a JUnit 4 (not 3) test class; go through the
steps carefully, so you don't miss the part where Eclipse creates
stubs for all your test methods.
Next, repeat the following steps:
- Fill in the tests for one of the methods.
- Run JUnit to make sure that your new test fails.
- Now write the method, replacing the stub code.
- Run JUnit to test whether your new method is correct.
- Debug as necessary. (Remember, it's possible that the test itself,
rather than the method being tested, is wrong.)
Do the above for each of the methods, until you are thoroughly familiar
with the pattern.
Things to remember:
- In your JUnit test class, you have to import the class being tested.
- Because all your methods are static, you can say
import static arrayOperations.ArrayOperations.*;
- With this kind of import, you can call your methods directly,
as for example
fill(5).
- With a "normal" (nonstatic) import, you would have to
say
ArrayOperations.fill(5).
- In
your JUnit test class, declare the
variables as instance variables of the class, but assign them
their initial values in the
setUp method.
- JUnit has an
assertArrayEquals(expected array, actual result)
method.
- JUnit tests are intended to find anything that could be wrong
with your methods, not to "prove" that they are correct. As
you write them, you are the prosecuting attorney, trying to expose any
flaws in the defendant's (the method's) story. For example, do the methods
handle zero-length arrays correctly?
- You can construct test cases more easily if you use array initialization.
For example,
int[] oneD = new int[] { 1, 2, 3, 4, 5 };
and
int[][] twoD = new int[][] { { 1, 2, 3 },
{
4, 5, 6 } };
Structure of the assignment:
-
Project name:
ArrayOperations
- Package name:
arrayOperations
- Class name:
ArrayOperations
- Method signatures: Exactly as given above.
- Provide JUnit tests for: All required methods.
- Provide Javadoc documentation for all classes and methods, except JUnit
tests.
- Document JUnit tests if they are complex or do something non-obvious
- Name to use for Blackboard:
ArrayOperations_yourName
Grading:
My JUnit tests will be applied to your methods. They will test everything that I can
think of that your methods might possibly get wrong.
Your JUnit tests will be applied to my methods. There will be errors in some of the
methods, which I expect your JUnit tests to find.
In order for this to work, you must use the method signatures exactly as listed.
(Check the course web page later, in case I have to correct anything.) If your
methods can't be called from my JUnit tests, or vice versa, that will cost you
significant points.
Due date:
Before Spring Break .