Question: 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
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:
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.
Input Validation: Do not accept negative numbers for monthly rainfall figures.
PLEASE USE BLUE J
/** * Rainfall Class * The Rainfall class stores information * about rainfall amounts. */ public class Rainfall { private double[] rain; // Array of rainfall data /** * Constructor */ public Rainfall(double r[]) { // Create a new array. rain = new double[r.length]; // Copy the argument's elements to the // new array. } /** * getTotalRainFall method */ public double getTotalRainFall() { double total = 0.0; // Accumulator } /** * getAverageRainFall method */ public double getAverageRainFall() { } /** * getHighestMonth method */ public int getHighestMonth() { } /** * getLowestMonth method */ public int getLowestMonth() { } /** * getRainAt method * Returns the value in the specified array * element. */ public double getRainAt(int e) { return rain[e]; } } Demo
/** * Rainfall Demo * This program demonstrates the Rainfall class. */ public class RainfallDemo { public static void main(String[] args) { // Array with this year's rainfall data double[] thisYear = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7 }; int high; // To hold the month with the highest amount int low; // To hold the month with the lowest amount // Create a Rainfall object initialized with // this year's data. Rainfall r = new Rainfall(thisYear); // Display the total rainfall. System.out.println("The total rainfall for this year is " + r.getTotalRainFall()); // Display the average rainfall. System.out.println("The average rainfall for this year is " + r.getAverageRainFall()); // Get and display the month with the highest rainfall. high = r.getHighestMonth(); System.out.println("The month with the highest amount of rain " + "is " + (high+1) + " with " + r.getRainAt(high) + " inches."); // Get and display the month with the lowest rainfall. low = r.getLowestMonth(); System.out.println("The month with the lowest amount of rain " + "is " + (low+1) + " with " + r.getRainAt(low) + " inches."); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
