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)   PLEASE MODIFY THIS CODE SO WHEN I UPLOAD IT TO HYPERGRADE IT PASSES THE TEST CASES. IT HAS TO PASS THE TEST CASES BECAUSE WHEN I UPLOAD IT TO HYPERGRADE IT DOES NOT PASS THE TEST CASES. I HAVE PROVIDED PROOF OF THE PART OF THE TEST CASE THAT FAILED.    import java.util.Scanner; public class Rainfall {     double rain[];         public Rainfall()     {         rain = new double[12];     }         public double averageRainfall()     {         return getTotalRain()/12.0;     }     public double getTotalRain()     {         double total = 0.0;         for(int i=0;i<12;i++)             total = rain[i];                 return total;     }         public int lowRainMonth()     {         double min = rain[0];         int month = 0;         for(int i=1;i<12;i++)         {             if(rain[i] < min)             {                 min = rain[i];                 month = i;             }         }                 return month;     }         public int highRainMonth()     {         double max = rain[0];         int month = 0;         for(int i=1;i<12;i++)         {             if(rain[i] > max)             {                 max = rain[i];                 month = i;             }         }                 return month;     }        public static void main(String[] args)    {        Scanner sc = new Scanner(System.in);                 Rainfall obj = new Rainfall();                for(int i=0;i<12;i++)        {            System.out.println("Enter the rainfall amount for month " + (i+1) + ":");            obj.rain[i] = sc.nextDouble();        }               System.out.println("Maximum rainfall: " + obj.rain[obj.highRainMonth()]);         System.out.println("Minimum rainfall: " + obj.rain[obj.lowRainMonth()]);         System.out.println("Total rainfall:  " + obj.getTotalRain());         System.out.println("Average rainfall:  " + obj.averageRainfall());    } } 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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
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)
 
PLEASE MODIFY THIS CODE SO WHEN I UPLOAD IT TO HYPERGRADE IT PASSES THE TEST CASES. IT HAS TO PASS THE TEST CASES BECAUSE WHEN I UPLOAD IT TO HYPERGRADE IT DOES NOT PASS THE TEST CASES. I HAVE PROVIDED PROOF OF THE PART OF THE TEST CASE THAT FAILED. 
 

import java.util.Scanner;

public class Rainfall
{
    double rain[];
   
    public Rainfall()
    {
        rain = new double[12];
    }
   
    public double averageRainfall()
    {
        return getTotalRain()/12.0;
    }
    public double getTotalRain()
    {
        double total = 0.0;
        for(int i=0;i<12;i++)
            total = rain[i];
       
        return total;
    }
   
    public int lowRainMonth()
    {
        double min = rain[0];
        int month = 0;
        for(int i=1;i<12;i++)
        {
            if(rain[i] < min)
            {
                min = rain[i];
                month = i;
            }
        }
       
        return month;
    }
   
    public int highRainMonth()
    {
        double max = rain[0];
        int month = 0;
        for(int i=1;i<12;i++)
        {
            if(rain[i] > max)
            {
                max = rain[i];
                month = i;
            }
        }
       
        return month;
    }
   
   public static void main(String[] args)
   {
       Scanner sc = new Scanner(System.in);
       
        Rainfall obj = new Rainfall();
       
       for(int i=0;i<12;i++)
       {
           System.out.println("Enter the rainfall amount for month " + (i+1) + ":");
           obj.rain[i] = sc.nextDouble();
       }
      
       System.out.println("Maximum rainfall: " + obj.rain[obj.highRainMonth()]);
        System.out.println("Minimum rainfall: " + obj.rain[obj.lowRainMonth()]);
        System.out.println("Total rainfall:  " + obj.getTotalRain());
        System.out.println("Average rainfall:  " + obj.averageRainfall());
   }
}

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

 

 
 
Maximum rainfall: 6.6\n
Minimum rainfall: 1.1\n
Total rainfall: 6.0\n
Average rainfall: 0.5\n
Transcribed Image Text:Maximum rainfall: 6.6\n Minimum rainfall: 1.1\n Total rainfall: 6.0\n Average rainfall: 0.5\n
Expert Solution
Step 1: Program Approach
  1. Class Definition:

    • The program defines a class called Rainfall.
  2. Instance Variable:

    • The class has an instance variable rain, which is an array of doubles to store the rainfall data for each month.
  3. Constructor:

    • The class includes a constructor Rainfall(), which initializes the rain array to hold 12 months of rainfall data.
  4. averageRainfall() Method:

    • This method calculates and returns the average rainfall across all 12 months. It does this by dividing the total rainfall by 12 and rounding the result to one decimal place.
  5. getTotalRain() Method:

    • This method calculates and returns the total rainfall for all 12 months by summing up the values stored in the rain array.
  6. lowRainMonth() Method:

    • This method identifies the month with the lowest rainfall and returns its index (0-based). It iterates through the rain array and keeps track of the minimum value and its corresponding month.
  7. highRainMonth() Method:

    • This method identifies the month with the highest rainfall and returns its index (0-based). It iterates through the rain array and keeps track of the maximum value and its corresponding month.
  8. main() Method:

    • Inside the main() method, a Scanner object sc is created to read input from the user.

    • An instance of the Rainfall class, obj, is created to work with rainfall data.

    • A loop is used to input the rainfall data for each of the 12 months, and the values are stored in the rain array.

    • After inputting the data, the program displays the following statistics:

      • Maximum rainfall by calling obj.highRainMonth() to get the month index and then accessing the corresponding value in the rain array.
      • Minimum rainfall by calling obj.lowRainMonth() to get the month index and then accessing the corresponding value in the rain array.
      • Total rainfall by calling obj.getTotalRain().
      • Average rainfall by calling obj.averageRainfall().
  9. User Interaction:

    • The program prompts the user to enter rainfall data for each month and then displays the calculated statistics.
steps

Step by step

Solved in 6 steps with 3 images

Blurred answer
Knowledge Booster
Array
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education