/** * Class with methods that work on a one dimensional array * of integers * @author Barb Ericson */ public class IntArrayWorker { // the integer array private int[] numArray; /** * A constructor that takes the integer array * @param theArray the integer array to use */ public IntArrayWorker(int[] theArray) { numArray = theArray; } /** * @return the elements of the array as a string */ public String toString() { String result = ""; int num = 0; for (int i = 0; i < numArray.length - 1; i++) { num = numArray[i]; result = result + String.valueOf(num) + ", "; } return result + numArray[numArray.length-1]; } /** * Method to find and return the smallest value in the array * @return the smallest value in the array */ public int getSmallest() { int smallest = Integer.MAX_VALUE; for (int num : numArray) { if (num < smallest) { smallest = num; } } return smallest; } /** * Method to set the number array * @param theArray the new array to use */ public void setNumArray(int[] theArray) { numArray = theArray; } public static void main(String[] args) { int[] testArray = {1, 2, 3, 4, 5}; IntArrayWorker arrayWorker = new IntArrayWorker(testArray); System.out.println(arrayWorker); int value = arrayWorker.getSmallest(); System.out.println("smallest is " + value); int [] t2 = {6, 4, 2}; value = arrayWorker.getSmallest(); System.out.println("smallest is " + value); } }