Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Eva Goins
- * Chapter 7 part 1
- * Description: Program demonstrates four different methods
- * that utilize different operations on arrays. getTotal
- * sums all of the array contents and returns a total.
- * getAverage calls getTotal and uses the array's length
- * to divide the total in order to get the average.
- * getHighest compares a temp variable with the present
- * contents of the array and returns the highest array.
- * getLowest does the opposite.
- */
- public class Program10ArrayOperations
- {
- public static void main(String[] args)
- {
- double prices[] = {45.00, 63.13, 20.99, 139.88, 13.45};
- System.out.println("The total is: " + getTotal(prices));
- System.out.println("The average is: " + getAverage(prices));
- System.out.println("The highest is: " + getHighest(prices));
- System.out.println("The lowest is: " + getLowest(prices));
- }
- public static double getTotal(double[] array)
- {
- int index;
- double total = 0;
- for (index = 0; index < array.length; index++)
- {
- total += array[index];
- }
- return total;
- }
- public static double getAverage(double[] array)
- {
- double total = 0;
- total = getTotal(array);
- return total / array.length;
- }
- public static double getHighest(double[] array)
- {
- int index;
- double highest = array[0];
- for (index = 0; index < array.length; index++)
- {
- if (array[index] > highest)
- {
- highest = array[index];
- }
- }
- return highest;
- }
- public static double getLowest(double[] array)
- {
- int index;
- double lowest = array[0];
- for (index = 0; index < array.length; index++)
- {
- if (array[index] < lowest)
- {
- lowest = array[index];
- }
- }
- return lowest;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement