Write a Rainfall class that stores the total amounts of rainfall for each of the
ID: 3625796 • Letter: W
Question
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. The program should include methods that return the following:• the total rainfall for the year
• the average rainfall for the year
• the month with the most rain
• the month with the least rain.
Demonstrate the class with a GUI application that displays each of the above values.
Input Validation: Do not accept negative values for monthly rainfall
import java.io.*;
import java.util.*;
public class Rainfall
{
Scanner in = new Scanner(System.in);
private int month = 12;
private double total = 0;
private double average;
private double standard_deviation;
private double largest;
private double smallest;
private double months[];
public Rainfall()
{
months = new double[12];
}
public void setMonths()
{
for(int n=1; n <= month; n++)
{
System.out.println("Enter the rainfall (in inches) for month #" + n + ":" );
months[n-1] = in.nextInt();
}
}
public double getTotal()
{
total = 0;
for(int i = 0; i < 12; i++)
{
total = total + months[i];
}
System.out.println("The total rainfall for the year is" + total);
return total;
}
public double getAverage()
{
average = total/12;
System.out.println("The average monthly rainfall is" + average);
return average;
}
public double getLargest()
{
double largest = 0;
int largeind = 0;
for(int i = 0; i < 12; i++)
{
if (months[i] > largest)
{
largest = months[i];
largeind = i;
}
}
System.out.println("The largest amout of rainfall was" + largest +
"inches in month" + (largeind + 1));
return largest;
}
public double getSmallest()
{
double smallest = Double.MAX_VALUE;
int smallind = 0;
for(int n = 0; n < month; n++)
{
if (months[n] < smallest)
{
smallest = months[n];
smallind = n;
}
}
System.out.println("The smallest amout of rainfall was" + smallest +
"inches in month " + (smallind + 1));
return smallest;
}
public static void main(String[] args)
{
Rainfall r = new Rainfall();
r.setMonths();
System.out.println("Total" + r.getTotal());
System.out.println("Smallest" + r.getSmallest());
System.out.println("Largest" + r.getLargest());
System.out.println("Average" + r.getAverage());
}
}
Explanation / Answer
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class Rainfall
{
private double[] year;
public Rainfall(double[] y)
{
year = new double[y.length];
for(int index = 0; index<y.length;index++)
year[index] = y[index];
}
public double getTotal()
{
double total = 0;
for(int index = 0; index < year.length; index++)
{
total += year[index];
}
return total;
}
public double getAverage()
{
double average = getTotal()/12;
return average;
}
public double getlargest()
{
double largest = 0;
int largeind = 0;
for(int index = 0; index < year.length; index++)
{
if (year[index] > largest)
{
largest = year[index];
largeind = index;
}
}
return largest;
}
public double getsmallest()
{
double smallest =year[0];
int smallind = 0;
for(int index = 1; index < year.length; index++)
{
if (year[index] < smallest)
smallest = year[index];
}
return smallest;
}
}
public class Rainfalls
{
public static void main(String[] args)
{
final int month = 12;
double[] year = new double[month];
setmonths(year);
Rainfall r =new Rainfall(year);
JOptionPane.showMessageDialog(null, "The total rain fall " + r.getTotal()+"The average rainfall"+r.getAverage()+"largest rainfall"+r.getlargest()+"smallest rainfall"+r.getsmallest() );
System.exit(0);
}
private static void setmonths(double[] a)
{
String input;
for(int i = 0; i<a.length;i++)
{
input = JOptionPane.showInputDialog("Enter "+ (i+1)+"Rainfall :");
a[i] = Double.parseDouble(input);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.