So I am writing a class for this rainfall assignment, I think I could get it working but I am having a problem with the final portion of the program "Rainfall rain = new Rainfall();".
I know that I probably have some logical errors in the code so far, but I am focusing on trying to at least get it to print so I can fix those issues. Thanks!
/**
Rainfall.java calculated the total annual and average
monthly rainfall from the array. This program also returns
the month with the most rainfall and the month with the least rainfall.
*/
public class Rainfall
{
//set integer month to 12
private int month = 12;
private double[] months;
/**
Constructor
@param scoreArray An array of test scores
*/
public Rainfall(double[] rainfallArray)
{
months = rainfallArray;
}
//Get total annual rainfall
public double getTotal()
{
double total = 0;
//for loop to go through the entire array to calculate the total amount of rainfall
for (int i = 0; i < month; i++)
{
total = total + months[i];
}
return total;
}
//Calculate average monthly rainfall
public double getAverage()
{
double total = 0; //To hold the current total amount of rainfall
double average; //To hold the average amount of rainfall
for (int i = 0; i < month; i++)
{
total = total + months[i];
}
//Calculate average rainfall
average = total / (months.length + 1);
return average;
}
//Get month with the most rain
public double getMost()
{
double most; //To hold the most amount of rainfall
//set the first month in the array
most = months[0];
int m=0;
for (int i = 0; i < month; i++)
{
if (months[i] < most)
{
m=i;
}
}
return most;
}
//Get month with the least rain
public double getLeast()
{
double least; //To hold the least amount of rainfall
int m=0;
//set the first month in the array
least = months[0];
for (int i = 0; i < month; i++)
{
if (months[i] < least)
{
m = i;
}
}
return least;
}
public static void main(String[ ] args)
{
Rainfall rain = new Rainfall();
rain.setMonths();
//Display results of the Total, Avg, and Most and Least calculations of rainfall
System.out.println("The total rainfall for the year: " + rain.getTotal());
System.out.println("The average rainfall for the year: " + rain.getAverage());
System.out.println("The month with most rain: " + rain.getMost());
System.out.println("The month with least rain: " + rain.getLeast());
}
}