Specifications: Write a program that stores the total rainfall for each of the 12 months in an array. The program should have the following separate methods:
getRainfall – takes an array as an argument and reads the amount of rainfall in inches for each of the 12 months from the user and stores the values in the array. The method cannot accept a negative number from the user.
displayRainfall – takes the array containing the inches of rainfall per month as an argument and displays the rainfall for each month.
getTotalRainfall – takes the array containing the inches of rainfall per month as an argument and returns the total number of inches of rainfall for the year.
getAverageRainfall – takes the array containing the inches of rainfall per month as an argument and returns the average number of inches per month.
getRainfallAbove – takes the array containing the inches of rainfall per month and a number as arguments and returns the number of months that had rainfall exceeded the number argument.
Declare the array in the main method and call each of separate methods and output the results in main to demonstrate the working state of the entire program.
My code:
public static final int MONTHS = 12;
static double[] rain = new double[MONTHS]; //Crate an array to hold the rain values
public static void main(String[] args) throws IOException {
getRainfall(args);
displayRainfall();
}
public static void getRainfall(String[] args) throws IOException {
String[] months = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" }; //Each month in a year
//Crate an array to hold the rain values
double[] rain = new double[12];
//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Get rain values and store them in the rain array
for (int i = 0; i < months.length; i++)
{
System.out.print("Enter rainfall for " + months[(i)] + ": ");
rain[i] = keyboard.nextDouble();
if (rain[i] <0)
{
System.out.println("You can not use a negative number.\n");
i--;
}
}
}
private static void displayRainfall() {
String[] months = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" }; //Each month in a year
System.out.println("\nDisplaying rainfall for each month\n");
for(int i = 0; i < months.length; i++) {
System.out.print("Rainfall for " + months[i] + ": \n, ");
Output:
Enter rainfall for January: 1
Enter rainfall for February: 1
Enter rainfall for March: 1
Enter rainfall for April: 1
Enter rainfall for May: 1
Enter rainfall for June: 1
Enter rainfall for July: 1
Enter rainfall for August: 1
Enter rainfall for September: 1
Enter rainfall for October: 1
Enter rainfall for November: 1
Enter rainfall for December: 1
Displaying rainfall for each month
Rainfall for January:
Rainfall for February:
Rainfall for March:
Rainfall for April:
Rainfall for May:
Rainfall for June:
Rainfall for July:
Rainfall for August:
Rainfall for September:
Rainfall for October:
Rainfall for November:
Rainfall for December:
>Solution :
You got to return rain and use it in the display method
public static void main(String[] args) throws IOException {
double[] rain = getRainfall(args);
displayRainfall(rain);
}
public static void getRainfall(String[] args) throws IOException {
String[] months = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" }; //Each month in a year
//Crate an array to hold the rain values
double[] rain = new double[12];
//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Get rain values and store them in the rain array
for (int i = 0; i < months.length; i++)
{
System.out.print("Enter rainfall for " + months[(i)] + ": ");
rain[i] = keyboard.nextDouble();
if (rain[i] <0)
{
System.out.println("You can not use a negative number.\n");
i--;
}
}
return rain;
}
private static void displayRainfall(double[] rain) {
String[] months = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" }; //Each month in a year
System.out.println("\nDisplaying rainfall for each month\n");
for(int i = 0; i < months.length; i++) {
System.out.print("Rainfall for " + months[i] + ": " + rain[i] + "\n, ");
}
}