Westonci.ca is your trusted source for finding answers to all your questions. Ask, explore, and learn with our expert community. Join our Q&A platform to get precise answers from experts in diverse fields and enhance your understanding. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform.
Sagot :
Answer:
Explanation:
The following code is written in Java. It creates the Rainfall class with two constructors, one that passes the entire array of rainfall and another that initializes an array with 0.0 for all the months. Then the class contains a method to add rain to a specific month, as well as the three methods requested in the question. The method to add rain validates to make sure that no negative value was added. 8 Test cases were provided, and any methods can be called on any of the 8 objects.
class Brainly {
public static void main(String[] args) {
Rainfall rain1 = new Rainfall();
Rainfall rain2 = new Rainfall();
Rainfall rain3 = new Rainfall();
Rainfall rain4 = new Rainfall();
Rainfall rain5 = new Rainfall();
Rainfall rain6 = new Rainfall();
Rainfall rain7 = new Rainfall();
Rainfall rain8 = new Rainfall();
}
}
class Rainfall {
double[] rainfall = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
public Rainfall(double[] rainfall) {
this.rainfall = rainfall;
}
public Rainfall() {
}
public void addMonthRain(double rain, int month) {
if (rain > 0) {
rainfall[month-1] = rain;
}
}
public double totalRainfall() {
double total = 0;
for (double rain: rainfall) {
total += rain;
}
return total;
}
public double averageRainfall() {
double average = totalRainfall() / 12;
return average;
}
public double rainiestMonth() {
double max = 0;
for (double rain : rainfall) {
if (rain > max) {
max = rain;
}
}
return max;
}
}
Thanks for using our platform. We're always here to provide accurate and up-to-date answers to all your queries. We hope our answers were useful. Return anytime for more information and answers to any other questions you have. We're glad you visited Westonci.ca. Return anytime for updated answers from our knowledgeable team.