Advertisement
Anthei

Program10ArrayOperations

Oct 27th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. /* Eva Goins
  2.  * Chapter 7 part 1
  3.  * Description: Program demonstrates four different methods
  4.  *      that utilize different operations on arrays. getTotal
  5.  *      sums all of the array contents and returns a total.
  6.  *      getAverage calls getTotal and uses the array's length
  7.  *      to divide the total in order to get the average.
  8.  *      getHighest compares a temp variable with the present
  9.  *      contents of the array and returns the highest array.
  10.  *      getLowest does the opposite.
  11.  */
  12.  
  13.  public class Program10ArrayOperations
  14.  {
  15.     public static void main(String[] args)
  16.     {
  17.         double prices[] = {45.00, 63.13, 20.99, 139.88, 13.45};
  18.        
  19.         System.out.println("The total is: " + getTotal(prices));
  20.         System.out.println("The average is: " + getAverage(prices));
  21.         System.out.println("The highest is: " + getHighest(prices));
  22.         System.out.println("The lowest is: " + getLowest(prices));
  23.     }
  24.    
  25.     public static double getTotal(double[] array)
  26.     {
  27.         int index;
  28.         double total = 0;
  29.        
  30.         for (index = 0; index < array.length; index++)
  31.         {
  32.             total += array[index];
  33.         }
  34.        
  35.         return total;
  36.     }
  37.    
  38.     public static double getAverage(double[] array)
  39.     {
  40.         double total = 0;
  41.         total = getTotal(array);
  42.         return total / array.length;
  43.     }
  44.    
  45.     public static double getHighest(double[] array)
  46.     {
  47.         int index;
  48.         double highest = array[0];
  49.        
  50.         for (index = 0; index < array.length; index++)
  51.         {
  52.             if (array[index] > highest)
  53.             {
  54.                 highest = array[index];
  55.             }
  56.         }
  57.        
  58.         return highest;
  59.     }
  60.    
  61.     public static double getLowest(double[] array)
  62.     {
  63.         int index;
  64.         double lowest = array[0];
  65.        
  66.         for (index = 0; index < array.length; index++)
  67.         {
  68.             if (array[index] < lowest)
  69.             {
  70.                 lowest = array[index];
  71.             }
  72.         }
  73.        
  74.         return lowest;
  75.     }
  76.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement