JAVA PROGRAM Chapter 7. PC #1. Rainfall Class Write a RainFall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the following: • the total rainfall for the year • the average monthly rainfall • the month with the most rain • the month with the least rain Demonstrate the class in a complete program. Main class name: RainFall (no package name)   HERE IS  A WORKING CODE, PLEASE MODIFY THIS CODE SO THE PUBLIC CLASS IS MAIN AND WERE EVER THERE IS MAIN IN THE CHANGE IT SO IT RUNS. ALSO, MAKE SURE WHEN I UPLOAD THE CODE TO HYPERGRADE IT PASSES ALL THE TEST CASES. THANK YOU

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter11: Inheritance And Composition
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

JAVA PROGRAM

Chapter 7. PC #1. Rainfall Class
Write a RainFall class that stores the total rainfall for each of 12 months into an array of
doubles. The program should have methods that return the following:
• the total rainfall for the year
• the average monthly rainfall
• the month with the most rain
• the month with the least rain
Demonstrate the class in a complete program.
Main class name: RainFall (no package name)
 
HERE IS  A WORKING CODE, PLEASE MODIFY THIS CODE SO THE PUBLIC CLASS IS MAIN AND WERE EVER THERE IS MAIN IN THE CHANGE IT SO IT RUNS. ALSO, MAKE SURE WHEN I UPLOAD THE CODE TO HYPERGRADE IT PASSES ALL THE TEST CASES. THANK YOU
 

import java.util.Scanner;

public class Main {
   
    private double[] rainfall = new double[12];

    public Main(double[] rainfall) {
        this.rainfall = rainfall;
    }

    public double getTotalRainfall() {
        double total = 0.0;
        for(double rain : rainfall) {
            total += rain;
        }
        return total;
    }

    public double getAverageMonthlyRainfall() {
        return getTotalRainfall() / 12;
    }

    public int getMonthWithMostRain() {
        int month = 0;
        double max = rainfall[0];
        for(int i = 1; i < rainfall.length; i++) {
            if(rainfall[i] > max) {
                max = rainfall[i];
                month = i;
            }
        }
        return month + 1;
    }

    public int getMonthWithLeastRain() {
        int month = 0;
        double min = rainfall[0];
        for(int i = 1; i < rainfall.length; i++) {
            if(rainfall[i] < min) {
                min = rainfall[i];
                month = i;
            }
        }
        return month + 1;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double[] monthlyRainfall = new double[12];
       
        for(int i = 0; i < 12; i++) {
            System.out.print("Enter the rainfall amount for month " + (i + 1) + ":\n");
            monthlyRainfall[i] = input.nextDouble();
        }

        Main rf = new Main(monthlyRainfall);

        System.out.println("Maximum rainfall: " + monthlyRainfall[rf.getMonthWithMostRain() - 1]);
        System.out.println("Minimum rainfall: " + monthlyRainfall[rf.getMonthWithLeastRain() - 1]);
        System.out.println("Total rainfall: " + rf.getTotalRainfall());
        System.out.printf("Average rainfall: %.1f\n", rf.getAverageMonthlyRainfall());
    }
}

 

 
 

Test Case 1

 
 
Enter the rainfall amount for month 1:\n
1.2ENTER
Enter the rainfall amount for month 2:\n
2.3ENTER
Enter the rainfall amount for month 3:\n
3.4ENTER
Enter the rainfall amount for month 4:\n
5.1ENTER
Enter the rainfall amount for month 5:\n
1.7ENTER
Enter the rainfall amount for month 6:\n
6.5ENTER
Enter the rainfall amount for month 7:\n
2.5ENTER
Enter the rainfall amount for month 8:\n
3.3ENTER
Enter the rainfall amount for month 9:\n
1.1ENTER
Enter the rainfall amount for month 10:\n
5.5ENTER
Enter the rainfall amount for month 11:\n
6.6ENTER
Enter the rainfall amount for month 12:\n
6.0ENTER
Maximum rainfall: 6.6\n
Minimum rainfall: 1.1\n
Total rainfall: 45.2\n
Average rainfall: 3.8\n
 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning