I need someones help with this program,please. I have to write a program called
ID: 656506 • Letter: I
Question
I need someones help with this program,please. I have to write a program called RainFall, that stores the total rain fall for each of the 12 months into an array of doubles. The program should display the following:
* the total rain fall for the year
* the average monthly rain fall
* the month(s) with the most rain
* the month(s) with the least rain
This is what I have:
import java.util.Arrays;
public class RainFall
{
public static void main(String[] args)
{
double[] rainFall = {3, 5, 9, 15, 5, 7, 1, 1, 6, 8, 3, 2};
double total = 0;
int[] monthsWithLeastRain = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
double leastRainFall = rainFall[0];
for(int i = 0; i < rainFall.length; i++)
{
total = total + rainFall[i];
if (rainFall[i] < leastRainFall)
{
leastRainFall = rainFall[i];
}
}
System.out.println("The total rain fall this year is " + total + " inches");
System.out.println("The average monthly rain fall this year is " + total/12 + " inches");
int pointer = 0;
for(int i = 0; i < rainFall.length; i++)
{
if (rainFall[i] == leastRainFall)
{
monthsWithLeastRain[pointer] = i;
pointer++;
}
}
System.out.println(Arrays.toString(monthsWithLeastRain));
}
/**
indexToMonth
Input: index, an int
Output: month, a String matching the month corresponding to index
"January" if index = 0
...
"December" if index = 11
Example: indexToMonth(5) returns "June"
**/
public static String indexToMonth(int index)
{
String month;
switch (index)
{
case 0:
month = "January";
break;
case 1:
month = "February";
break;
case 2:
month = "March";
break;
case 3:
month = "April";
break;
case 4:
month = "May";
break;
case 5:
month = "June";
break;
case 6:
month = "July";
break;
case 7:
month = "August";
break;
case 8:
month = "September";
break;
case 9:
month = "October";
break;
case 10:
month = "November";
break;
case 11:
month = "December";
break;
default:
month = "Not a valid month";
break;
}
return month;
}
}
And this is what is printed to the console right now:
Welcome to DrJava. Working directory is C:Userslisa95naDesktopDr.Java
> run RainFall
The total rain fall this year is 65.0 inches
The average monthly rain fall this year is 5.416666666666667 inches
[6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
>
And this is what should be printed to the console:
I dont know how go on from where I am, how to display the month from the list of numbers I get and then convert them into their actual names. These are the hints I got, but I still dont know how to apply them:
Hints:
* The variable pointer is actually an index, it points to the spot in monthsWithLeastRain that is to be updated. Once the loop
is done executing, pointer denotes the number of months having an equal amount of least rain.
* Use pointer, indexToMonth, and a loop to help output the months with least rain fall. A similar procedure is needed to
output the months with most rainfall.
* What we are doing in one of the for loops is called a sequential search.
It would be really nice, if someone could help me solving this problem! Thank you!
Explanation / Answer
public class Rainfall
{
final static int SIZE = 12;
public static double totalRain(double []x)
{
double s=0;
for(int i=0;i<x.length;i++) s+=x[i];
return s;
}
public static double averageRain(double []x)
{
return totalRain(x)/x.length;
}
public static int getHighest(double []x)
{
double s=x[0];int id=0;
for(int i=1;i<x.length;i++) if(s<x[i]) { s=x[i]; id=i;}
return id;
}
public static int getLowest(double []x)
{
double s=x[0]; int id=0;
for(int i=1;i<x.length;i++) if(s>x[i]) { s=x[i]; id=i;}
return id;
}
public static void main (String [] args)
{
double highest = 0;
double lowest = 0;
double total = 0;
double average=0;
double [] rainfall = new double [SIZE];
initialize_array (rainfall);
highest = getHighest (rainfall);
lowest = getLowest (rainfall);
total = getTotal (rainfall);
average=averageRain(rainfall);
System.out.println(average);
}
public static void initialize_array (double [] rain)
{
Scanner S=new Scanner(System.in);
for (int i = 0; i<rain.length;i++) {
System.out.println("Enter rainfall for month"+(i+1));
rain[i]=S.nextDouble();
}
}
}