Here’s Main.java (same as professor’s “RainfallDemo.java”):
package academy.learnprogramming;
public class Main {
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.");
}
}
// DO NOT MAKE ANY CHANGES TO RainfallDemo.java!!!!!!!!! This project uses .replit and run_button.sh
// to configure RainfallDemo.java instead of RainfallDemo.java. When clicking the Run button it will run
// RainfallDemo.java.
//
// Write a Rainfall.java 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
// return the amount of rain given the index number for the month.
// total rainfall for the year
// the average monthly rainfall
// the month with the most rain
// the month with the least rain
// return the amount of rain given the index number for the month.
//
// Demonstrate the class in a complete program called RainfallDemo.java.
//
// Input Validation: Do not accept negative numbers for monthly rainfall figures.
//
// Hint: The UML file image may help when building your class file.
//
// Make sure you include a comment block at the top of your code with the following information:
// Your name Date Project name a brief description of what the file does
//
// Submit as described.
Here’s Rainfall.java:
package academy.learnprogramming;
public class Rainfall {
private double[] rain;
public Rainfall(double[] r) { // This is a constructor.
this.rain = r;
}
public double getTotalRainFall() { // This and those below are methods.
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};
double sum = 0;
for (int i = 0; i < thisYear.length; i++) {
sum += thisYear[i];
}
return sum;
}
public double getAverageRainFall() {
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};
double sum = 0;
for (int i = 0; i < thisYear.length; i++) {
sum += thisYear[i];
}
return sum / (double) thisYear.length;
}
public int getHighestMonth() {
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 max = 1;
for (int i = 0; i < thisYear.length; i++) {
if (thisYear[i] > thisYear[max]) max = i;
}
return max;
}
public int getLowestMonth() {
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 min = 1;
for (int i = 0; i < thisYear.length; i++) {
if (thisYear[i] < thisYear[min]) min = i;
}
return min;
}
public double getRainAt(int e) {
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};
double high = thisYear[0];
double low = thisYear[0];
for (int i = 0; i < thisYear.length; i++) {
if (thisYear[i] > high) {
high = thisYear[i];
}
if (thisYear[i] <= low) {
low = thisYear[i];
}
}
return low;
}
}