Given RainfallDemo class: public class RainfallDemo { public static void main(St
ID: 3532973 • Letter: G
Question
Given RainfallDemo 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());//defined in rainfall class; for loop
// Display the average rainfall.
System.out.println("The average rainfall for this year is " +
r.getAverageRainFall());//defined in rainfall class; divide by the size of the array; no loop required; envoke total rainfall
// 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.");//defined in rainfall class; use for loop to step through and look at each element in the array
// 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.");//defined in rainfall class; use for loop to step through and look at each element in the array
}
}
Write a Rainfall class that stores the 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
Input Validation: Do not accept negative numbers for monthly rainfall figures.
This is what I have so far for the Rainfall class:
public class Rainfall
{
private double[] thisYear; //References the rainfall data
/**
*The constructor accepts an array as an argument.
*/
public Rainfall(double[] r)
{
//Create a new array the same as r.
thisYear = new double[r.length];
//Copy the values in r to rainfall
for
(int i = 0; i < r.length; i++)
thisYear[i] = r[i];
}
//The getTotalRainFall method returns the total of the elements in the rainfall array.
public double getTotalRainFall()
{
double total = 0.0; //Accumlator
//Add up all the values in the sales array.
for (double value : thisYear)
total += value;
//Return the total
return total;
}
//The getAverageRainFall method returns the average of the elements in the rainfall array.
public double getAverageRainFall()
{
return getTotalRainFall()/thisYear.length;
}
//The getHighestMonth method returns the highest value stored in the rainfall array.
public double getHighestMonth()
{
//Store the first value in the rainfall array in the variable highest.
double high = thisYear[0];
//Search the array for the highest value.
for (int i = 1; i <thisYear.length; i++)
{
if (thisYear[i] > high)
high = thisYear[i];
}
//Return the highest value
return high;
}
//The getLowestMonth method returns the lowest value stored in the rainfall array.
public double getLowestMonth()
{
//Store the first value in the rainfall arrayin the variable lowest.
double low = thisYear[0];
//Search the array for the lowest value.
for (int i = 1; i <thisYear.length; i++)
{
if (thisYear[i] < low)
low = thisYear[i];
}
//Return the lowest value.
return low;
}
}
The rainfall class complies but when I try to compile the RainfallDemo class I get the following error:
Explanation / Answer
you didnt write code for this method:getRainAt()
you're assigning integers low and high with double values returned by getHighestmonth() and getLowestmonth()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.