Created
November 18, 2013 20:54
-
-
Save fado/7535109 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uk.ac.qub.programming; | |
public class PartTwo { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
double[] temperatures = {3.4, 4.2, 9.0, 2.2, 4.5, 6.4, 3.1}; | |
double total = 0; | |
double average; | |
double minimum; | |
double maximum; | |
// get total | |
for (double temp : temperatures) { | |
total += temp; | |
} | |
// get average | |
average = total/temperatures.length; | |
// print average | |
System.out.printf("Average: %.2f\n", average); | |
minimum = getMin(temperatures); | |
maximum = getMax(temperatures); | |
System.out.printf("Minimum temperature: %.2f\n", minimum); | |
System.out.printf("Maximum temperature: %.2f", maximum); | |
} | |
/** | |
* Method to get the minimum value in an array. | |
* @param array | |
* @return | |
*/ | |
public static double getMin(double[] array) { | |
int counter = 0; | |
double minimum = Double.MAX_VALUE; | |
if (array == null) { | |
return 0; | |
} else { | |
while (counter < array.length){ | |
if (array[counter] < minimum) { | |
minimum = array[counter]; | |
} | |
counter++; | |
} | |
} | |
return minimum; | |
} | |
/** | |
* Method to get the maximum value in the array. | |
* @param array | |
* @return | |
*/ | |
public static double getMax(double[] array) { | |
int counter = 0; | |
double maximum = Double.MIN_VALUE; | |
if (array == null) { | |
return 0; | |
} else { | |
while (counter < array.length){ | |
if (array[counter] > maximum) { | |
maximum = array[counter]; | |
} | |
counter++; | |
} | |
} | |
return maximum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment