Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Fall 2014
Project #4
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by the start of class on Thursday, 10/30/2014

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.

You will find this 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, arrayToString, which takes as argument an array A of String values. The function should return a String, consisting of the elements in A, separated by commas, and the whole thing enclosed in between the characters "[" and "]". Note that this function can be used to test the remaining functions in this part of the assignment

    Solution (yes this one is free):

    public static String arrayToString(String[] A) {
        String aRepr = "[";
        
        if (A.length > 0) {
            aRepr = aRepr + A[0];
            int i = 1; 
            while(i < A.length) {
                aRepr = aRepr + "," + A[i];
                i += 1;
            }
        }
        
        aRepr += "]";
        
        return aRepr;
    }
    
  2. Write a function, copy, which takes as arguments an array A of String values. The function should return a copy of A.

  3. Write a function, indexOf, which takes as arguments an array A of String values, and a single String, n. The function should return the first index in Awhere n occurs. If n does not occur in A, return -1

  4. Write a function isIn which takes as arguments an array A of String values, and a single String, n. The function should return true or false, according to whether n is in A. (HINT: Try to solve this using a call to another method you've written, rather than reinventing the wheel. If you've solved indexOf, this one is very easy).

  5. Write a function, concat, which takes as arguments two arrays of String 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).

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

    String[] breakfast = {"spam","eggs","spam"};
    String[] morebreakfast = repeat(nums,3);

    should assign to moreNums the array ["spam","eggs","spam","spam","eggs","spam","spam","eggs","spam"]

  7. Write a function, slice, which takes as arguments an array of String values, A and two integers, i and j. The function should return the sub-array of A beginning at index i and continuing up to, but not including, index j (this is similar in behavior to the substring method that comes with String objects).

  8. Write a function, remove, which takes as arguments an array A of String values, and a single String, n. The function should return a copy of A, with the first occurrence of n removed (HINT #1: slice and indexOf to make this quite easy. HINT #2: remember not to test equality with ==)

  9. Write a function, bubSort, which takes as argument an array A of String values. The function should sort the contents of A, according to the following algorithm:

    do (n-1) times (where n == A.length):
        for each j from 0 to A.length - 2, incrementing by 1:
            if the jth and (j+1)st elements of A are out of order:
                swap them
    
    

    There is no return value.

    NOTE: You'll need to use the String method compareTo, in order to compare two String values.

Turn In

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

Standards (READ ME)

Formatting Requirements

See the Style Guide, available from the General Notes section of our course web site. Beginning with this assignment, all elements of beautiful, clear code style described there will be expected.

General


credits

This problem is adapted from one that appeared in a qualifying round of the ACM Intercollegiate Programming Competition. I have lost the attribution for the original (more complicated) version.


John H. E. Lasseter