CS 163/164, Spring 2018
Programming Assignment P7
Array Processing

Programming due Monday, Mar. 5th at 6:00pm

Late Tuesday, Mar. 6th at 8:00am

This lab has the goal of teaching you how to:
  1. Declare arrays of different types dynamically.
  2. Iterate through arrays, processing all of the elements.
  3. Write methods with arrays as parameters and return values.
  4. Test your own class with code that you add to main.


In this assignment you will create your own class and write the methods in it. The class you write is called P7 and it has four methods that build arrays and four methods that process arrays. All methods should be public and static.


Create a project called P7 and a class named P7 in a file called P7.java, then follow the instructions below exactly:
  1. Write a method named createIntegers that allocates, initializes, and returns an array of integers with the following entries: 16, 21, 34, 49, 55, 60, 72, 83, 101. The method has no parameters and returns an array of 9 integers.
  2. Write a method named createDoubles that builds an array of floating point values that represent the squares of the numbers from 10.0 to 13.0, in steps of 0.5, inclusive of both boundaries. The method has no parameters and returns an array of doubles. There are exactly 7 numbers in this array.
  3. Write a method named createStrings that takes a String as an input parameter, and returns an array of string with 4 elements. The first element is the original string passed in, the second element is the same string converted to uppercase, the third element is the same string converted to lowercase, and the fourth element is the same string with the first character removed.
  4. Write a method named createChars that extracts the characters from a string and builds an array with them. The method takes a parameter of type String and returns an array of characters. Hint: the size of this array is very easy to determine!
  5. Note:Each of the arrays must be allocated to be exactly the correct size needed for the contents specified above.
  6. Write a method called sumArray that takes an array of integers as a parameter, and returns an integer equal to the sum of all elements in the array.
  7. Write a method called findLargest that takes an array of doubles as a parameter, and returns a double equal to the largest element in the array.
  8. Write a method called charFrequency that takes an array of strings and a single character as parameters. The method should iterate the array, counting instances of the character passed in. For example, if the array is ["Hello", "There"] and we are asked to count the character 'e', the return value will be three. If the character is not found in any of the elements in the array, the return value should be zero.
  9. Write a method called translateChars that takes an array of characters and returns an array of integers with the values that correspond to each element of the character array. For example, the character 'A' will be translated to 65, the character '0' will be translated to 48, and the character '%' will be translated to 37. The integer array returned should be of the same size as the array of characters passed in.
  10. Add a main method with the usual signature that instantiates the P7 class and tests its methods as follows:
    public static void main(String[] args) {

        // Create arrays
        int[] integerArray = createIntegers();
        double[] doubleArray = createDoubles();
        String[] stringArray = createStrings("Hello There");
        char[] charArray = createChars("Java1234!&");

        // Test processing
        System.out.println("Sum of integer array = " + sumArray(integerArray));
        System.out.println("Largest of double array = " + findLargest(doubleArray));
        System.out.println("Frequency of 'e' = " + charFrequency(stringArray, 'e'));
        System.out.println("Translated characters = " + Arrays.toString(translateChars(charArray)));


Based on the provided test code in the main method, you should see the following output if your methods are correct:
[16, 21, 34, 49, 55, 60, 72, 83, 101]
[100.0, 110.25, 121.0, 132.25, 144.0, 156.25, 169.0]
[Hello There, HELLO THERE, hello there, ello There]
[J, a, v, a, 1, 2, 3, 4, !, &]
Sum of integer array = 491
Largest of double array = 169.0
Frequency of 'e' = 9
Translated characters = [74, 97, 118, 97, 49, 50, 51, 52, 33, 38]


Your program must meet the following specifications:

Grading Criteria

Submit your P7.java file to the Checkin tab on the course website.

© 2017 CS163 Colorado State University. All Rights Reserved.