In: Computer Science
Java Coding Program.
In this assignment, you will write a program that analyzes Phoenix area 2018 rainfall data. Inside the main() method, first you will get the rainfall data for each of the 12 months in 2018 from the user and stores them in a double array. Next, you will need to design the following four value-returning methods that compute and return to main() the totalRainfall, averageRainfall, driestMonth, and wettestMonth. The last two methods return the index (or number) of the month with the lowest and highest rainfall amounts, not the amount of rain that fell those months. Notice that this month index (or number) can be used to obtain the amount of rain that fell those months.
I need help with driestMonth and wettestMonth. Wettest month is not giving me the correct output.
This is how it should read when it prints out on screen. Example : "The least rain fell in May with 0.35 inches."
This is what I have so far.
----------------------------------------------------------------------------
import java.util.Scanner;
import java.text.DecimalFormat;
public class Assignment12
{
public static void main(String[] args)
{
// variables
final int numMonths = 12;
double[] rainArray = new double[numMonths];
String[] month = {"Januray", "February", "March", "April",
"May",
"June", "July", "August", "September",
"October", "November", "December"};
Scanner scan = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat("0.00");
// get user input for rainfall each month
for(int i=0; i < numMonths; i++)
{
System.out.print("\nEnter " + month[i] + " rainfall amount:
");
rainArray[i] = scan.nextDouble();
}
System.out.print("\n==== 2018 Rain Report for Phoenix, AZ
====");
System.out.print("\nTotal rainfal: " +
fmt.format(getTotal(rainArray)));
System.out.print("\nAverage monthly rainfall: " +
fmt.format(getAverage(rainArray)));
System.out.print("\nThe least rain feel in " +
getWettestMonth(rainArray));
System.out.print("\nThe most rain fell in ");
}
public static double getTotal(double[] rainArray)
{
double total = 0;
final int numMonths = 12;
for(int i=0; i < numMonths; i++)
{
total = total + rainArray[i];
}
return total;
}
public static double getAverage(double[] rainArray)
{
double total = 0;
double average = 0;
final int numMonths = 12;
for(int i=0; i < numMonths; i++)
{
total = total + rainArray[i];
}
average = total / numMonths;
return average;
}
public static int getWettestMonth(double[] rainArray)
{
int mostRainIndex = 0;
final int numMonths = 12;
for(int i=0; i < numMonths; i++)
{
if(rainArray[i] > rainArray[mostRainIndex])
{
mostRainIndex = i;
}
}
return mostRainIndex;
}
public static int getDriestMonth(double[] rainArray)
{
//code
}
}
import java.text.DecimalFormat;
import java.util.Scanner;
public class RainFall {
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
System.out.println("Enter the rain
fall in inches for each ");
double sum = 0, numMonths =
12;
String month[] = { "January",
"February", "March", "April", "May", "June", "July", "August",
"September",
"October", "November", "December" };
double rainArray[] = new
double[12];
// reading the rains fall for each
month
/*
* for (int i = 0; i < 12;)
{
* System.out.println("Enter the
rain fall for month " + months[i]); arr[i] =
* sc.nextDouble(); // if it is
negative, reading again the for same month if
* (arr[i] < 0) {
* System.out.println("value must be
0 or more. Please re-enter"); } else { sum
* += arr[i]; i++; }
*
* } sum = getTotal(arr); double avg
= getAverage(arr); int max =
* getWettestMonth(arr); int min =
getDriestMonth(arr);
*
* // finding the min
*
* System.out.println("Total
rainfall: " + sum + " inches");
* System.out.println("Average
rainfall for the year is " + avg + " inches");
* System.out.println("The most rain
fill in " + months[max] + "with " +
* arr[max] + " inches");
System.out.println("The least rain fill in " +
* months[min] + "with " + arr[min]
+ " inches");
*/
// get user input for rainfall each
month
DecimalFormat fmt = new
DecimalFormat("0.00");
for (int i = 0; i <
numMonths; i++) {
System.out.print("\nEnter " + month[i] + " rainfall amount:
");
rainArray[i] =
scan.nextDouble();
}
System.out.print("\n==== 2018
Rain Report for Phoenix, AZ ====");
System.out.print("\nTotal rainfal:
" + fmt.format(getTotal(rainArray)));
System.out.print("\nAverage monthly
rainfall: " + fmt.format(getAverage(rainArray)));
System.out.print("\nThe least rain
feel in " + month[getWettestMonth(rainArray)]+"with
"+rainArray[getWettestMonth(rainArray)]+" inches");
System.out.print("\nThe most rain
fell in "+ month[getDriestMonth(rainArray)]+"with
"+rainArray[getDriestMonth(rainArray)]+" inches");
}
private static int getDriestMonth(double[] arr)
{
int min = 0;
for (int i = 1; i < 12;
i++)
if (arr[min]
> arr[i])
min = i;
return min;
}
private static int getWettestMonth(double[] arr)
{
int max = 0;
// finding the max
for (int i = 1; i < 12;
i++)
if (arr[max]
< arr[i])
max = i;
return max;
}
private static double getAverage(double[] arr)
{
return (getTotal(arr) /
arr.length);
}
private static double getTotal(double[] arr)
{
double sum = 0;
// finding the sum
for (double d : arr) {
sum += d;
}
return sum;
}
}

Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me