CMPU 101 - Lab 7

$Revision: 1.5 $

In this lab you will use top-down design to develop several simple array algorithms.  You will also write test code to make sure that the methods implementing the algorithms work correctly on some test data.

You should do all of your work in a class called Lab7.  Define this class in a source file "" in the Home directory (/home/cs101).

First Task

Write a static method that will reverse the elements in an array of double values.  Use top-down design:

  1. On paper, identify the input (parameters) and output (return value/type) of the algorithm

  2. On paper, write a pseudo-code outline for the method that will implement the algorithm.  This outline will become comments in the method that implements the algorithm

  3. On paper, identify data (variables) used in the algorithm.

  4. Define a static method to implement the algorithm.

In addition to designing and implementing the method to reverse the elements of an array of double values, you should also write a static method that will automatically test the method on at least one array of test data.  The test method (also a static method) should print the contents of both the original and reversed versions of the array.  In addition, it should throw an IllegalStateException if the reversed version of the array does not contain the correct sequence of values.  See from lecture 14 for an example of how to write a test method.

Execute your test method from the DrJava interactions window to make sure that your array reversal algorithm works correctly.  You will probably want to write an additional method to print out the values in an array of double values so you can visually confirm that your method is working correctly.

Second Task

Write a static method that takes a two-dimensional array of double values, the number of rows in the array, and the number of columns in the array, and prints out the values of each row of the array (using System.out).  Again, use top-down design to sketch the pseudo-code for the algorithm on paper, identify the input, output, and variables, and then translate your pseudo-code into Java.

The output should look something like this:

1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0

You should assume a row-major representation: that means that the first dimension of the array specifies the row, and the second dimension of the array specifies the column.

Write a test method that creates an array of test data and uses your method to print it.  Java provides a convenient shorthand for creating an array and initializing its contents.  For example, the array above can be created using the following statement:

double[][] testMatrix = {
   {1.0, 2.0, 3.0},
   {4.0, 5.0, 6.0},
   {7.0, 8.0, 9.0},

Feel free to use this as your test data.

Third Task (Extra Credit)

You may do one additional task for extra credit.

Write a static method that transposes the elements of a two-dimensional array of double values.  Transposing a two-dimensional array means that each element of the array should be moved to the location specified by reversing its index values: row becomes column and column becomes row.

Transposition only makes sense if both dimensions of the array are the same length: therefore, in addition to the input array the method should also take a single int value which specifies both the number of rows and the number of columns.

Here is what the 3x3 array above will look like after its elements are transposed:

1.0 4.0 7.0
2.0 5.0 8.0
3.0 6.0 9.0

Rather than modifying the input array, you should create a new array object with the same number of rows/columns as the input array to store the transposed values.  A reference to the new transposed array should be returned as the return value of the method.

Write a test method to transpose an array of test data (you can use the 3x3 array described above).  Your test method should both print the transposed array and also verify that it contains the correct values.


When you are done, demonstrate your work by running your test methods for a lab coach or instructor.

Submit your Java source file using the CS 101 Submission WebsiteMake sure you submit the Java source file: the filename should end in ".java", not ".java~" or ".class".