In: Computer Science
Write a driver program with a double array that is initialized with the following test data. You may hard code this data into your array.
Echo the input in a neat table formatted exactly as below (5 elements to a line and aligned). Then output the number of elements in the array, smallest, largest, range, array total and average, using the methods of the array utilities class defined below.
Java Eclipse
ArrayUtilities.java
class ArrayUtilities {
        
        // calculating the smallest value
        public static double smallest( double[] a ) {
                
                // initializing first element
                double smallest = a[0];
                
                // finding the smallest element
                for(int i=0; i<a.length ;i++) {
                        if(smallest > a[i]) {
                                smallest = a[i];
                        }
                }
                
                // returning smallest element
                return smallest;
        }
        
        // calculating the largest value 
        public static double largest( double[] a ) {
                
                // initializing largest element
                double largest = a[0];
                
                // finding largest element
                for(int i=0; i<a.length ;i++) {
                        if(largest < a[i]) {
                                largest = a[i];
                        }
                }
                
                // returning largest element
                return largest;
        }
        
        // calculating range of array
        public static String range( double[] a ) {
                double smallest = smallest(a);
                double largest = largest(a);
                // returning the range
                return String.format("[%f %f]", smallest, largest);
        }
        
        // calculating total sum of array
        public static double arrayTotal( double[] a ) {
                double total = 0;
                for(int i=0; i<a.length ;i++) {
                        total += a[i];
                }
                return total;
        }
        
        // calculating average
        public static double average( double[] a ) {
                return arrayTotal(a)/a.length;
        }
        
}
Driver.java
public class Driver {
        public static void main(String[] args) {
                
                double[] a = new double[] {3, 5, 1, 56, 9, 10};
                
                // printing all properties of array utilities
                System.out.println("Number of Elements : "+a.length);
                System.out.println("Smallest Element   : "+ArrayUtilities.smallest(a));
                System.out.println("Largest Element    : "+ArrayUtilities.largest(a));
                System.out.println("Range of Elements  : "+ArrayUtilities.range(a));
                System.out.println("Array Total        : "+ArrayUtilities.arrayTotal(a));
                System.out.println("Array Average      : "+ArrayUtilities.average(a));
                
                
        }
}
Code
ArrayUtilities.java
Driver.java
Output