Lab 14: Array Statistics
Traversal, sum, max, min, average, and linear search
Learning Objectives
After completing this lab, you will be able to:
- Declare, initialize, and fill an array from user input
- Traverse an array to compute sum, average, maximum, and minimum
- Implement linear search returning an index or -1
- Use
Arrays.toString()to print array contents for debugging
What You’re Building
A statistics calculator that reads integers into an array, then computes sum, average, max, min, and searches for a target value. Each operation is its own static method that accepts an int[] parameter. This is the standard pattern for array utility methods in Java.
Concepts and Common Misconceptions
| Concept | What students get wrong |
|---|---|
| Array declaration | Writing int arr[]; (valid but not idiomatic) instead of int[] arr;. Both work, but the autograder examples use the second form. |
Arrays.toString |
Printing an array directly: System.out.println(arr) prints a memory address like [I@6d06d69c. Always use Arrays.toString(arr). |
| Max/min initialization | Initializing max to 0 instead of arr[0]. If all values are negative, 0 is never replaced and the result is wrong. |
| Average as double | Using sum / arr.length produces integer division. Cast first: (double) sum / arr.length. |
| Linear search return | Returning true/false instead of the index. The spec requires returning the index where the value was found, or -1 if not found. |
Checkpoint 1: Fill and Print (1 pt)
Write a method fillArray(Scanner scanner, int size) that creates an int[] of the given size, prompts the user for each value, and returns the filled array.
In main, call fillArray, then print the result with Arrays.toString().
Enter 5 values:
10 20 30 40 50
Your array: [10, 20, 30, 40, 50]
Debugging tip: If you get an ArrayIndexOutOfBoundsException, your loop runs past size - 1. Array indices go from 0 to length - 1.
Checkpoint 2: sum, average, max, min (1 pt)
Write four static methods, each accepting an int[] parameter:
sum(int[] arr)– return the sum of all elements.average(int[] arr)– return the average as adouble.max(int[] arr)– return the largest element.min(int[] arr)– return the smallest element.
int[] data = {10, 20, 30, 40, 50};
sum(data) // 150
average(data) // 30.0
max(data) // 50
min(data) // 10
Initialize max to arr[0] and min to arr[0], then loop starting at index 1.
Debugging tip: If average returns 30 instead of 30.0, you are doing integer division. Cast the sum to double before dividing: return (double) sum(arr) / arr.length;.
Checkpoint 3: linearSearch (1 pt)
Write linearSearch(int[] arr, int target) that returns the index of the first occurrence of target in arr, or -1 if not found.
int[] data = {10, 20, 30, 40, 50};
linearSearch(data, 30) // 2
linearSearch(data, 99) // -1
Loop through the array. When you find a match, return the index immediately. If the loop finishes without finding a match, return -1.
Debugging tip: If your method always returns -1, check your comparison. For int arrays, == is correct (unlike Strings). If it always returns 0, you might be returning outside the loop and hitting the default before the loop runs.
How to Debug
- Use Arrays.toString for everything. After filling the array, print it immediately to verify the contents before running any calculations.
- Test with known data. Hard-code
int[] test = {1, 2, 3, 4, 5};and verify thatsumreturns 15,averagereturns 3.0,maxreturns 5,minreturns 1. - Test edge cases. An array with one element. An array with all identical values. An array with negative numbers.
- Read the stack trace.
ArrayIndexOutOfBoundsExceptiontells you exactly which index was out of range. The line number points to the exact loop iteration that failed.
Scoring Breakdown
| Component | Points |
|---|---|
| Checkpoint 1: Fill array and print with Arrays.toString | 1 |
| Checkpoint 2: sum, average, max, min methods | 1 |
| Checkpoint 3: linearSearch returning index or -1 | 1 |
| Autograder correctness | 5 |
| Timeliness (on-time submission) | 2 |
| Total | 10 |