Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Spring 2015
Thursday Lab #7
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by the start of class on Friday, 03/27/2015

In this assignment, you will implement a number of methods that are useful when working with arrays. All of the following functions should be added to a single class, ArrayUtils.

A partial implementation is available here:

You will find this lab much easier if you use DrJava as your development environment. Use the Interactions pane to help with quick tests of the various methods.

# Problems

1. Write a function, isIn, which takes as arguments an array, A, of double values and a single double value, x. The function should return true if x occurs anywhere in A and false otherwise.

2. Write a function, concat, which takes as arguments two arrays of double values, A and B. The function should return the array that results from concatenating B onto the end of A (much like the + operator on String objects).

3. Write a function, repeat, which takes as arguments an array of double values, A, and an integer n. The function should return an array consisting of n copies of A. For example,

```double[] nums = {1.1, 2.2, 3.3};
double[] moreNums = repeat(nums,3);```

should assign to moreNums the array [1.1, 2.2, 3.3, 1.1, 2.2, 3.3, 1.1, 2.2, 3.3]

4. Write a function, remove, which takes as arguments an array A of double values, and a single double, n. The function should return a copy of A, with the first occurrence of n removed (HINT: You can do this with an array traversal, but it's much easier to use calls to slice and indexOf).

5. Write a function, shuffle, which takes as argument an array A of double values. The function should randomly rearrange the contents of A. One way to do this is to traverse the array, and at each index, pick a random location somewhere else in the array, and swap the two elements.

```int j = (int) (Math.random() * A.length);
double tmp = A[j];
A[j] = A[i];
A[i] = tmp;```

There is no return value.

# Turn In

The source code for your program, ArrayUtils.java, in a subfolder of your turn-in directory called "lab7". Please also provide a paper copy of your work.