-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrainFall.java
More file actions
81 lines (65 loc) · 2.03 KB
/
Copy pathrainFall.java
File metadata and controls
81 lines (65 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.Scanner;
/* @author Lemuel M. Uhuru
* @Date May 2, 2013
* Book Title: From Control Structures through Objects by Tony Gaddis
* Page: 490 Problem 1
*
* Program that creates a class and stores the total
* rainfall for each 12 months into an array of doubles
* and demonstrates the class in a complete program.
*/
public class rainFall {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
final int SIZE = 12;
double[] monthlyRain = new double[SIZE];
System.out.println("Enter the monthly rain fall amount: ");
int i = 0;
// Added input validation with 'Do-While Loop'
for(i = 0; i < SIZE; i++){
do{
System.out.print("Month " + (i+1) + ": ");
monthlyRain[i] = keyboard.nextDouble();
}
while(monthlyRain[i] < 0);
}
System.out.println("The annual sum of rain: " + sumRain(monthlyRain));
System.out.println("The average sum of rain: " + averageRain(monthlyRain));
System.out.println("The most amount of rain fall: " + mostRain(monthlyRain));
System.out.println("The least amount of rain fall: " + leastRain(monthlyRain));
}
// Create a function that returns the total rainFall for the year.
static double sumRain(double[] array){
double sum = 0;
for(int i = 0; i < array.length; i++){
sum += array[i];
}
return sum;
}
// Create a function that returns the average monthly rainFall for the year.
static double averageRain(double[] array){
double average = 0;
average = sumRain(array)/array.length;
return average;
}
// Create a function that returns the month with the most rain.
static double mostRain(double[] array){
double most = 0;
for (int i = 0; i < array.length; i++){
if(array[i] > most){
most = array[i];
}
}
return most;
}
// Create a function that returns the month with the least rain.
static double leastRain(double[] array){
double least = array[0];
for(int i = 0; i < array.length; i++){
if(array[i] < least){
least = array[i];
}
}
return least;
}
}