Design a program that uses nested loops to collect data and calculate the averag
ID: 671620 • Letter: D
Question
Design a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period. Modify the average rainfall program so that the total inches of rain and the average rainfall per month is displayed for each year as well as the final totals requested by the problem. In addition in flowchart please Write a brief description of implementation of the program development cycle when solving this problem
Explanation / Answer
To calculate sum and average
static double calculateTotalRainFall(List<Double> rainFall) {
Double sum = new Double(0);
for (Double i : rainFall) {
sum = sum + i;
}
return sum;
}
static double calculateAverageRainFall(List<Double> rainFall) {
Double sum = 0d;
for (Double vals : rainFall) {
sum += vals;
}
return sum = sum / rainFall.size();
}
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");
}
To test the code is
@Test
public void test_calculateTotalRainFall () {
List<Double> listToSum = new ArrayList<Double>();
listToSum.add(new Double(10));
listToSum.add(new Double(10));
listToSum.add(new Double(10));
listToSum.add(new Double(10));
listToSum.add(new Double(10));
Double sum = AverageRainfall.calculateTotalRainFall(listToSum);
assertEquals(50, sum, 0);
}
@Test
public void test_calculateAverageRainFall () {
List<Double> listToSum = new ArrayList<Double>();
listToSum.add(new Double(10));
listToSum.add(new Double(10));
listToSum.add(new Double(10));
listToSum.add(new Double(10));
listToSum.add(new Double(10));
Double sum = AverageRainfall.calculateAverageRainFall(listToSum);
assertEquals(10, sum, 0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.