CPTR 124 Fundamentals of Programming


In the first part of this lab you will develop code that works with simple arrays. In Part 2 you will choose to implement one of several interesting array projects.


  1. Teams

    You are encouraged to work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.

  2. Preparing your working folder

    Make a subfolder named Lab11 within your H:\cptr124 folder.

  3. Part 1.

    Develop a utility class named Utility that includes the following class methods that work with integer arrays:

    • public static void sort(int[] a) { ... }

      Physically rearranges the elements of a so they are in ascending order.

      For example, if list contains the elements 2, 1, 3, 1, 5, and 2, the call Utility.sort(list) reorders list to contain 1, 1, 2, 2, 3, 5.

    • public static int max(int[] a) { ... }

      Returns the maximum value in array a. The behavior of this method is undefined if a does not contain at least one element.

      Array a is not affected by this method.

    • public static int min(int[] a) { ... }

      Returns the minimum value in array a The behavior of this method is undefined if a does not contain at least one element.

      Array a is not affected by this method.

    • public static int[] evens(int[] a) { ... }

      Returns a new integer array containing only the even elements of a. The elements in the returned array are ordered the same way with respect to each other as they were in a. For example, if list contains the elements 1, 2, 3, 4, 5, and 6, the call Utility.evens(list) returns an array containing 2, 4, and 6

      Array a is not affected by this method.

    • public static int count(int[] a, int n) { ... }

      Returns the number of times element n appears in a.

      Array a is not affected by this method.

    • public static int find(int[] a, int n) { ... }

      Returns the location (zero is the first position) of the first occurrence of n within a. Said another way, it returns the lowest index in a that contains the value n. If n cannot be found within a, -1 is returned, since -1 is not a legal index within any Java array.

      Array a is not affected by this method.

    • public static int[] subsequence(int[] a, int beginIndex, int count) { ... }

      Returns a new array that contains a "slice" of a beginning with the element at index beginIndex. The slice contains the rest of the array (all the elements, in order, that follow a[beginIndex]) up to a maximum of count elements total. If beginIndex is greater than the last legal index of the array, an empty array is returned.

      For example, if list contains the elements 1, 2, 3, 4, 5, and 6,

      • Utility.subsequence(list, 3, 10) returns an array containing 4, 5, and 6
      • Utility.subsequence(list, 2, 3) returns an array containing 3, 4, and 5
      • Utility.subsequence(list, 6, 3) returns an empty array

      Array a is not affected by this method. The method's behavior is undefined for negative values of beginIndex and/or count.

    The behavior of each of the above methods is undefined if the array passed to it is null; this means you do not need to worry about handling a null array a client might pass. However, unless otherwise noted above, your methods should behave properly if the array passed is empty. An empty array is a properly allocated array with zero elements.

    None of the methods should do any I/O. This means when you are finished the method should not contain any printing statements and should not expect any input from the keyboard or a file. You are welcome to use printing statements during development, just remove them when you are satisfied the method is correct. The only "input" to a method is via its parameters, and its only "output" is its return value.

    For grading purposes your Utility class will be subjected to a suite of JUnit tests. While not required, it might be useful to assign to one team member the task of writing JUnit tests while the other team member writes the actual code. Any JUnit tests your team writes will not be handed in, but if you write good tests and ensure that your code passes all your tests, your code will have a better chance of performing well against the grading test cases.

  4. Part 2.

    Write one of the following programs:

  5. Check out

    Your finished programs will be evaluated for correctness and compliance. When approved, you should submit a printout of your code. Be sure your name and your partner(s) names are included on the printout.