Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modifly this java program to add the following functionality. 1. Read in rainfal

ID: 667790 • Letter: M

Question

Modifly this java program to add the following functionality.

1. Read in rainfall values from the user. (Read in twelve values tor represent the amount of rainfall for each of the 12 months. Do not accept negative numbers. Store the values in a local array.

2. Offer a menu of options to the user. (The menu should let the user choose between six actions (described in 3-8) For each action, write a method to accomplish that action.)

3. Write a method to output the rainfall for each month.

4. Write a method to find the total rainfall for the year.

5. Write a method to find the average monthly rainfall.

6. Write a method to return the name of the month with the most rain. (if two months have the same maximum value, you can output either month.)

7. Write a method to return the name of the month with the least rain. (if two months have the same minimum value, you can output either month.)

8. Write a method to update the amount of rain in any given month.

Use constants when possible and avoid duplicating code.

import java.util.Arrays;
import java.util.Scanner;

public class RainFall
{
public static double totalRainfall(double[] rainfall)
{
double total = 0;

for(double d : rainfall)
{
total += d;
}

return total;

}

public static double averageRainfall(double[] rainfall)
{
double total = totalRainfall(rainfall);

return total/rainfall.length;

}

public static double minRainfall(double[] rainfall)
{
// sorting the array
Arrays.sort(rainfall);

return rainfall[0];
}

public static double maxRainfall(double[] rainfall)
{
// sorting the array
Arrays.sort(rainfall);

return rainfall[ rainfall.length - 1 ];
}


public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);

String monthNames[] =

{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Spe","Oct","Nov","Dec"};
double monRainfall[] = {0.40, 0.94, 3.21, 3.74, 1.73, 1.03, 1.27, 2.58, 6.98, 6.90,

2.80, 2.53};

System.out.println("Austin Tx Rainfall 2009");
System.out.println("---------------------------");

for(int i = 0; i < monthNames.length; i++)
{
System.out.println(monthNames[i]+" "+monRainfall[i]);
}
System.out.println();

System.out.println("Total : "+totalRainfall(monRainfall));
System.out.println("Average : "+averageRainfall(monRainfall));
System.out.println("Max : "+maxRainfall(monRainfall));
System.out.println("Min : "+minRainfall(monRainfall));


}

}

Explanation / Answer

final static int NUM_MONTHS = 12; // Months per year

public static void main(String[] args) {

int years; // Number of years
double monthRain;
List<Double> rainFall = new ArrayList<Double>();

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Get the number of years.
System.out.print("Enter the number of years: ");
years = keyboard.nextInt();

// Validate the input.
while (years < 1) {
System.out.print("Invalid. Enter 1 or greater: ");
years = keyboard.nextInt();
}

System.out.println("Enter the rainfall, in inches, "
+ "for each month.");

for (int y = 1; y <= years; y++) {

for (int m = 1; m <= NUM_MONTHS; m++) {

// Get the rainfall for a month.
System.out.print("Year " + y + " month " + m + ": ");
monthRain = keyboard.nextDouble();

// Accumulate the rainfall.
rainFall.add(new Double(monthRain));
}
}

// Display the statistics.
System.out.println(" Number of months: " + (years * NUM_MONTHS));
System.out.println("Total rainfall: "
+ calculateTotalRainFall(rainFall) + " inches");
System.out.println("Average monthly rainfall: "
+ calculateAverageRainFall(rainFall) + " inches");

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote