CIT 590 Assignment 7: Array Operations
Spring 2009, David Matuszek

Purposes of this assignment:

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
123456 789101112
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(
31416
) returns
61413
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(
1234
5678
9101112
) returns
951
1062
1173
1284
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(
1234
5678
9101112
) returns
4812
3711
2610
159
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(
1234
5678
9101112
) returns
159
2610
3711
4812
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(
123456 789101112
, 4) returns
1234
5678
9101112
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(
1234
5678
9101112
) returns
123456 789101112
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(
1234
5678
9101112
, 6) returns
123456
789101112
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(
123
456
,
10203040
50607080
) returns
12310203040
45650607080
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(
72101108108111
) returns
'H''e''l' 'l''o'
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(
72101108108111
) returns "Hello".
public static int[] toInts(char[] array)
Given an array of characters, return an array of their ASCII values.
Example: toInts(
'H''e''l' 'l''o'
) returns
72101108108111
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
72101108108111

How to do this assignment:

You can save yourself quite a bit of typing if you follow these steps:

  1. In Eclipse, create the project, the package, and the class.
  2. Write each of the methods as stubs--that is, write the headers, but just have each method return null.
  3. 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:

  1. Fill in the tests for one of the methods.
  2. Run JUnit to make sure that your new test fails.
  3. Now write the method, replacing the stub code.
  4. Run JUnit to test whether your new method is correct.
  5. 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:

Structure of the assignment:

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 .