CMPS 240 Summer 2019
Programming Assignment #1: Same Difference

Requirements

For this assignment, you are to complete the development of the Java class SameDifference. Its two static public methods —findPairs() and findPairsDefensive()— serve the same purpose, which is to return an ArrayList (i.e., an instance of the class java.util.ArrayList) containing all pairs (i,j) (i.e., instances of the class PairOfInts) satisfying the property that subtracting the j-th element of the second of two given arrays from the i-th element of the first array yields a specified "delta" value.

The two methods differ only in that one assumes as a precondition that both arrays passed to it contain elements that are in increasing order while the other method explicitly tests that condition and throws an exception if it is not satisfied. (The latter of the two methods exemplifies a defensive programming approach.) Of course, in addition to completing the two public methods, you are free to add private methods that play a supporting role.

To illustrate the idea, suppose that the two arrays of interest are as illustrated below and the delta value is 4.

     0    1    2    3    4    5    6    7    8
  +----+----+----+----+----+----+----+----+----+
a | -1 |  2 |  5 |  6 | 12 | 15 | 20 | 21 | 29 |
  +----+----+----+----+----+----+----+----+----+

     0   1    2    3     4    5    6    7    8    9   10
  +----+----+----+----+----+----+----+----+----+----+----+
b | -4 | -1 |  2 |  3 |  5 |  8 | 14 | 18 | 20 | 22 | 25 |
  +----+----+----+----+----+----+----+----+----+----+----+

There are three pairs (i,j) of locations that satisfy the condition a[i] - b[j] = delta = 4, and they are (3,2), (4,5), and (8,10).

For your convenience, the Java application SameDifferenceTester is provided to you. In order to make fruitful use of it, you must pass to it three command-line arguments (or what jGrasp calls "run arguments"). the first two of which indicate the lengths of the two arrays to be compared and the third of which indicates the desired "delta" value. How to supply command-line arguments when running a program in jGrasp is explained here.

What SameDifferenceTester does is to create two arrays of the lengths indicated by the first two command-line arguments, populates them with pseudo-randomly chosen values (but ensuring that each array's elements are in increasing order), calls the findPairs() method (passing to it as parameters the arrays and the delta value indicated by the third command-line argument), and prints the pairs that occupy the ArrayList returned by the method. Any "false positives" (i.e., pairs that are in the returned ArrayList but that should not be) are identified as such. "False negatives" (i.e., pairs that do not appear in the returned ArrayList but should be there) are not identified. In this respect, this "tester application" is somewhat lacking.

% java SameDifferenceTester 10 15 3
First array:
  [-1, 4, 7, 8, 9, 13, 15, 16, 18, 19]
Second array:
  [0, 4, 7, 10, 15, 18, 23, 25, 28, 33, 37, 42, 44, 49, 52]

Pairs of locations whose elements differ by 3:
(2,1)
(5,3)
(8,4)

To illustrate, on the right is shown an invocation of DifferenceTester from the command line in a UNIX environment (shown in boldface) and the output that results.


Note

Remember that this is a course about data structures and algorithms. The algorithm that you develop to serve as the basis for the Java code that you write should exploit the fact that it processes arrays whose elements are in increasing order.