CS 160, Spring 2015
Programming Assignment P8
Array Processing
Programming due Monday, Mar. 30 at 6:00pm; late deadline Mar. 30 at 11:59pm.
This lab has the goal of teaching you how to:
- Declare arrays of different types dynamically.
- Iterate through arrays, processing all of the elements.
- Write methods with arrays as parameters and return values.
- Test your own class with code that you add to main.
Description
In this assignment you will create your own class and write the methods
in it. The class you write is called P8 and it has four methods that build
arrays and four methods that process arrays. All methods should be public
and static.
Instructions
Create a project called P8 and a class named P8 in a file called P8.java,
then follow the instructions below exactly:
- 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.
- 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.
- 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.
- 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!
- Note:Each of the arrays must be allocated to be exactly
the correct size needed for the contents specified above.
- 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.
- 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.
- 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.
- 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.
- Add a main method with the usual signature that instantiates
the P8 class and tests its methods as follows:
public static void main(String[] args) {
// Create arrays
int[] integerArray = createIntegers();
System.out.println(Arrays.toString(integerArray));
double[] doubleArray = createDoubles();
System.out.println(Arrays.toString(doubleArray));
String[] stringArray = createStrings("Hello There");
System.out.println(Arrays.toString(stringArray));
char[] charArray = createChars("Java1234!&");
System.out.println(Arrays.toString(charArray));
// 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)));
}
Testing
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]
Specifications
Your program must meet the following specifications:
- Work on your own, as always.
- The name of the source code file must be exactly P8.java.
- Name the file exactly - upper and lower case matters!
- Assignments should be implemented using Eclipse.
- Assignments should be implemented using Java 1.5 or 1.6 or 1.7.
- Make sure your code runs on machines in the COMCS 120 lab.
- Submit your program to the Checkin tab as you were shown in the recitation.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- All arrays must be exactly the size needed for the contents!
- The preliminary test is just a compile test.
- compileTest: checks that code in the P8 class compiles. (0 points)
- You must implement all of the methods above correctly to pass this test. Even
without preliminary testing, if you match the test output above without hard-coding
anything, then you should get a good grade.
- Final Tests
- test1: tests that createArray builds the correct array. (10 points)
- test2: tests that createDoubles builds the correct array. (10 points)
- test3: tests that createStrings builds the correct array. (10 points)
- test4: tests that createChars builds the correct array. (10 points)
- test5: tests sumArray method. (15 points)
- test6: tests findLargest method. (15 points)
- test7: tests charFrequency method. (15 points)
- test8: tests translateChars method. (15 points)
Submit your P8.java file to the Checkin tab on the course website, as you were shown in
the recitation, and read the syllabus for the late policy.
© 2015 CS160 Colorado State University. All Rights Reserved.