In: Computer Science
Homework
Arrays and Tables
In this assignment you are to create an algorithm, flowchart, and pseudocode for a solution of the following problem. This solution will include the use of arrays needed to complete all parts of the logic.
You have requested to develop a
program that will record and process the rainfall totals of a 12
month period. You would use an array to store each months total.
Once all 12 months amounts are entered then your solution need to
process this array to complete the following
requirements:
The user wants to know the total rainfall for the year.
Then you will need to calculate the average monthly rainfall
You will then need to find the month with the most rainfall and the month with the least amount of rainfall
When you have completed this assignment drop it into the dropbox for this assignment.
RainfallTest.java
import java.util.Arrays;
import java.util.Scanner;
public class RainfallTest {
static String month[] = {"January", "Fabruary",
"March", "April", "May", "June", "July", "August",
"September","October","November","December"};
public static void main(String[] args) {
double
monthlyRainfall[] = new double[12];
Scanner scan =
new Scanner(System.in);
for(int i=0;
i<monthlyRainfall.length; i++){
System.out.print("Enter rainfall amount (in
inches) for "+month[i]+":");
monthlyRainfall[i] = scan.nextDouble();
}
System.out.println("Entered rainfall details
"+Arrays.toString(monthlyRainfall));
displayTotalRainFall(monthlyRainfall);
displayAverageRainFall(monthlyRainfall);
displayMaximumRainFall(monthlyRainfall);
displayMinimumRainFall(monthlyRainfall);
}
public static void displayEachMonthRainFall(double
months[]){
for(int i=0; i<months.length;
i++){
System.out.println(month[i]+": "+months[i]);
}
}
public static void displayTotalRainFall(double
months[]){
double total = 0;
for(int i=0; i<months.length;
i++){
total = total +
months[i];
}
System.out.printf("Total Rainfall
for the Year (in inches): %.2f\n",total);
}
public static void displayAverageRainFall(double
months[]){
double total = 0;
double avg = 0;
for(int i=0; i<months.length;
i++){
total = total +
months[i];
}
avg =
total/(double)months.length;
System.out.printf("Average Rainfall
for the Year (in inches): %.2f\n",avg);
}
public static void displayMaximumRainFall(double
months[]){
double max = 0;
for(int i=0; i<months.length;
i++){
if(max <
months[i]){
max = months[i];
}
}
System.out.println("Maximum Monthly
Rainfall (in inches): "+max);
}
public static void displayMinimumRainFall(double
months[]){
double min = months[0];
for(int i=0; i<months.length;
i++){
if(min >
months[i]){
min = months[i];
}
}
System.out.println("Minimum Monthly
Rainfall (in inches): "+min);
}
}
Output:
Enter rainfall amount (in inches) for January:22
Enter rainfall amount (in inches) for Fabruary:21
Enter rainfall amount (in inches) for March:33
Enter rainfall amount (in inches) for April:44
Enter rainfall amount (in inches) for May:55
Enter rainfall amount (in inches) for June:11
Enter rainfall amount (in inches) for July:88
Enter rainfall amount (in inches) for August:66
Enter rainfall amount (in inches) for September:77
Enter rainfall amount (in inches) for October:25
Enter rainfall amount (in inches) for November:45
Enter rainfall amount (in inches) for December:32
Entered rainfall details [22.0, 21.0, 33.0, 44.0, 55.0, 11.0, 88.0,
66.0, 77.0, 25.0, 45.0, 32.0]
Total Rainfall for the Year (in inches): 519.00
Average Rainfall for the Year (in inches): 43.25
Maximum Monthly Rainfall (in inches): 88.0
Minimum Monthly Rainfall (in inches): 11.0