/********************************************************************* * Author: Eric Heim * Date Created: 6/30/2011 * Course: CS0007 * Description: Uses the minimum method to find the minimum element * of an array. *********************************************************************/ public class MethodDoc { public static void main(String[] args) { //Array of numbers whose minimum will be found int[] numbers = {75, 65, 24, 11, 45, 32, 22, 3, 54, 5}; //Display the minimum number in the array by calling the //minimum method System.out.println("The minimum number in the array is " + minimum(numbers)); } /********************************************************************* * Method Name: minimum * Parameters: minArray - Array whose minimum will be found * Returns: The minimum element in the array * Description: Loops through the array and returns the smallest * element *********************************************************************/ public static int minimum(int [] minArray) { int champion; //If the array is empty, then just return the smallest value //possible if(minArray.length == 0) { champion = Integer.MIN_VALUE; } else { //initialize the smallest element to the first element champion = minArray[0]; //Loop through the array and if the current element is smaller //then the smallest element seen so far, replace the champion //with the new smallest element seen so far for(int i = 1; i < minArray.length; i++) { if(minArray[i] < champion) { champion = minArray[i]; } } } //returns the smallest element in the array return champion; } }