Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Rainfall Class. Write a Rainfall class that stores the total amounts of rai

ID: 3599035 • Letter: J

Question

Java

Rainfall Class.    Write a Rainfall class that stores the total amounts of rainfall for each of the 12 months of a year into an array of doubles. You should have a setter that accepts the array from the main program and copies that into a private array. The program should include methods that return the following:

prints the values in the array in a JOptionPane or scrollable textbox

the total rainfall for the year

the average rainfall for the year

the month with the most rain

the month with the least rain

Write a second program that instantiates the Rainfall class and tests it by prompting the user for the rainfall data, using a setter to pass that array, and then displays the results of each of the above methods in a JOptionPane.

Input Validation: Do not accept negative values for monthly rainfall.

Draw a UML diagram (rather than a flowchart) for this class.

Project Rubric

Explanation / Answer

Solution:

Please find below Rainfall.java and Runner.java containing the method calls.

Rainfall.java

import java.util.Arrays;

import javax.swing.JOptionPane;

public class Rainfall {

private double[] rainfallData;

public double[] getRainfallData() {
return rainfallData;
}
public void setRainfallData(double[] rainfallData) {
this.rainfallData = rainfallData;
}

public void printRainfallData(){
JOptionPane.showMessageDialog(null, Arrays.toString(rainfallData));
}

public void totalRainfall(){
double totalRainfall = 0.0f;
for(double rainData : rainfallData){
totalRainfall += rainData;
}
JOptionPane.showMessageDialog(null, String.format("Total rainfall for the year was: %.2f", totalRainfall));
}

public void averageRainfall(){
double totalRainfall = 0.0f;
double averageRainfall = 0.0f;
for(double rainData : rainfallData){
totalRainfall += rainData;
}
averageRainfall = totalRainfall / 12;
JOptionPane.showMessageDialog(null, String.format("Average rainfall for the year was: %.2f", averageRainfall));
}

public void monthWithMaxRain(){
int monthIndex = 0;
double maxRain = 0;
for(int i=0;i<12;i++){
if(rainfallData[i] > maxRain){
maxRain = rainfallData[i];
monthIndex = i;
}
}
JOptionPane.showMessageDialog(null, String.format("Maximum rainfall for the year was %.2f in month no %d", maxRain, monthIndex+1));
}

public void monthWithMinRain(){
int monthIndex = 0;
double minRain = rainfallData[0];
for(int i=0;i<12;i++){
if(rainfallData[i] < minRain){
minRain = rainfallData[i];
monthIndex = i;
}
}
JOptionPane.showMessageDialog(null, String.format("Minimum rainfall for the year was %.2f in month no %d", minRain, monthIndex+1));
}
}

Runner.java

import java.util.Scanner;

public class Runner {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
  
System.out.println("Please enter rainfall data for 12 months: ");
double[] rainfallData = new double[12];
for(int i=0;i<12;i++){
double rainData = scanner.nextDouble();
while(! (rainData > 0)) {
System.out.println("Please enter value greater than 0 !!");
rainData = scanner.nextDouble();
}
rainfallData[i] = rainData;
  
}
  
Rainfall rainfallObject = new Rainfall();
  
rainfallObject.setRainfallData(rainfallData);
  
rainfallObject.printRainfallData();
  
rainfallObject.totalRainfall();
  
rainfallObject.averageRainfall();
  
rainfallObject.monthWithMaxRain();
  
rainfallObject.monthWithMinRain();

scanner.close();
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote