CIT 590 Assignment 11: RichArray API
Fall 2015, David Matuszek

Purpose of this assignment

General idea of the assignment

Define a new object type, named RichArray. A RichArray object is like a two-dimensional array of integers. However, it will be different from an array in a couple of respects: (1) RichArrays have a number of operations defined on them that arrays do not have, (2) RichArrays are immutable, in the same way that Strings are immutable; once created, they cannot be changed, and (3) RichArrays are just objects, so you can't use brackets with them as you can with arrays.

A "one-dimensional" RichArray is represented internally as a two-dimensional array with only one row. Therefore, every method defined for two-dimensional RichArrays should also work for RichArrays that appear to be one-dimensional.

Write a number of methods for manipulating RichArrays. 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.

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.

Constructors

public RichArray(int[][] array)
Creates a RichArray containing a copy of the given array.
public RichArray(int[] array)
Creates a RichArray containing the given array. (Internally, every RichArray object holds a two-dimensional array. Copy the given one-dimensional array of N locations into a 1xN two-dimensional array.)
public RichArray(int length)
Creates an RichArray of length integers, and fills it with the numbers 1 to length.
Example: new RichArray(12) returns 
1 2 3 4 5 6 7 8 9 10 11 12
Remember that all RichArrays are actually two-dimensional, so the array you create would be 1xlength in size.
public RichArray(int rows, int columns)
Create a RichArray of rows rows and columns columns, and fills it with the integers 1 to rowsxcolumns, left to right, top to bottom.
Example: new RichArray(3, 4) returns 
1 2 3 4
5 6 7 8
9 10 11 12

Methods

public RichArray reverse()
Returns a new RichArray whose values are in the reverse order of those in the given RichArray.
Example:   
3 1 4 1 6
.reverse() returns 
6 1 4 1 3

Example:
1 2 3 4
5 6 7 8
9 10 11 12
.reverse() returns
4 3 2 1
8 7 6 5
12 11 10 9
public RichArray rotateRight()
Returns a new RichArray which is "rotated" a quarter-turn clockwise.
Example:
1 2 3 4
5 6 7 8
9 10 11 12
.rotateRight() returns
9 5 1
10 6 2
11 7 3
12 8 4

public RichArray rotateLeft()
Returns a new RichArray which is "rotated" a quarter-turn counterclockwise.
Example:
1 2 3 4
5 6 7 8
9 10 11 12
.rotateLeft() returns
4 8 12
3 7 11
2 6 10
1 5 9
public RichArray transpose()
Transposes a RichArray of m rows and n columns to form a RichArray of n rows and m columns. The value in location [i][j] of the RichArray become the value in location [j][i] of the new RichArray.
Example:
1 2 3 4
5 6 7 8
9 10 11 12
.transpose() returns 
1 5 9
2 6 10
3 7 11
4 8 12
public RichArray ravel(int n)
Takes a one-dimensional RichArray of m × n numbers and returns a two-dimensional RichArray of m rows and n columns. The first n numbers of the given RichArray are copied into the first row of the new RichArray, the second n numbers into the second row, and so on. This method throws an IllegalArgumentException if the length of the input RichArray is not evenly divisible by n.
Example:
1 2 3 4 5 6 7 8 9 10 11 12
.ravel(4) returns 
1 2 3 4
5 6 7 8
9 10 11 12
public RichArray unravel()
Takes a m by n two dimensional RichArray and returns a one-dimensional RichArray of size m × n containing the same numbers. The first n numbers of the new RichArray are copied from the first row of the given RichArray, the second n numbers from the second row, and so on.
Example: 
1 2 3 4
5 6 7 8
9 10 11 12
.unravel() returns 
1 2 3 4 5 6 7 8 9 10 11 12
public RichArray reshape(int n)
Takes a two-dimensional array of r rows and c columns and reshapes it to have n columns by (r*c)/n rows. This method throws an IllegalArgumentException if r*c is not evenly divisible by n.
Example: 
1 2 3 4
5 6 7 8
9 10 11 12
.reshape(6) returns 
1 2 3 4 5 6
7 8 9 10 11 12

public RichArray join(RichArray array)
Adjoins a RichArray with n rows and m1 columns to the parameter RichArray with n rows and m2 columns, forming a new RichArray with n rows and m1+m2 columns. This method throws an IllegalArgumentException if the input RichArrays do not have the same number of rows.
Example: 
1 2 3
4 5 6
.join(
10 20 30 40
50 60 70 80
) returns 
1 2 3 10 20 30 40
4 5 6 50 60 70 80

public RichArray stack(RichArray array)
Forms a new RichArray with n rows and m1+m2 columns by putting the recipient RichArray with n1 rows and m columns on top of the parameter RichArray of n2 rows and m columns. This method throws an IllegalArgumentException if the input RichArrays do not have the same number of columns.
Example: 
1 2 3 4
5 6 7 8
.stack(
10 20 30 40
) returns 
1 2 3 4
5 6 7 8
10 20 30 40
public int rowCount()
Returns the number of rows in the RichArray. For a one-dimensional RichArray, this returns 1.
public int columnCount()
Returns the number of columns in the RichArray. For a one-dimensional RichArray, this is the number of values in the RichArray.
public RichArray rows(int firstRow, int lastRow)
Returns a new RichArray containing values from row firstRow to row lastRow, inclusive, of the recipient RichArray.
Example: 
1 2 3 4
5 6 7 8
9 10 11 12
.rows(1, 2) returns 
5 6 7 8
9 10 11 12

public RichArray columns(int firstColumn, int lastColumn)
Returns a new RichArray containing values from column firstColumn to column lastColumn, inclusive, of the recipient RichArray.
Example: 
1 2 3 4
5 6 7 8
9 10 11 12
.columns(1, 2) returns 
2 3
6 7
10 11

public RichArray slice(int firstRow, int lastRow, int firstColumn, int lastColumn)
Returns a new RichArray containing values from the given portion of the recipient RichArray.
Example: 
1 2 3 4
5 6 7 8
9 10 11 12
.columns(1, 2, 2, 3) returns 
7 8
11 12

public RichArray replace(RichArray array, int row, int column)
Returns a new RichArray in which the parameter array replaces the values of the recipient RichArray, starting at the given row and column. This method throws an IllegalArgumentException if the parameter RichArray would go beyond the bounds of the recipient array.
Example: 
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
.replace(
55 66 77
88 99 100
, 1, 2) returns 
1 2 3 4 5
6 7 55 66 77
11 12 88 99 100
16 17 18 19 20
@Override
public boolean equals(Object o)
Returns true if and only if this RichArray contains an array of the same shape and containing the same values as Object o.
Here's how to write an equals method:
  1. Make sure you declare the parameter to be of type Object, not RichArray.
  2. Test if Object o is actually a RichArray, with (o instanceof RichArray). If it isn't, return false.
  3. Declare a new variable that of type RichArray, and assign (RichArray)o to it. (This is a "cast").
  4. Write code to determine if this RichArray and that RichArray are actually equal, by comparing their instance variables. In this case you need to compare the number of rows (array.length), the number of columns (array[0].length), and every value in those arrays.
@Override
public int hashCode()
Arrays have a hashCode method. Your RichArray object contains an array. This method should return the hashCode of that array. Don't ask why, just do it.
public int[] toArray1D()
Returns a one-dimensional array of the values in the recipient RichArray. If the RichArray is two-dimensional, it is first unraveled.
public int[][] toArray2D()
Returns a copy of the two-dimensional array of the values in the recipient RichArray. If the RichArray is one-dimensional, the result will be an array containing, as its single element, a one-dimensional array. (Do not return the actual array; make a copy. If you were to return the actual array, and someone were to change some value in the array, that would change the value in the RichArray. That violates our intention to make RichArrays immutable.)
public int at(int row, int column)
Returns the integer at the given row and column.

Things to remember

Structure of the assignment

Due date

Zip your entire package and submit it to Canvas by 6am Wednesday, November 18.