Question

In: Computer Science

Java Coding Program. In this assignment, you will write a program that analyzes Phoenix area 2018...

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
}
}

Solutions

Expert Solution

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


Related Solutions

Write the Java program: In this assignment, you will create a program implementing the functionalities of...
Write the Java program: In this assignment, you will create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at a ticket counter). The Queue3503 class will contain: a. An int[] data filed named...
For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
using java For this assignment, you will write a program that guesses a number chosen by...
using java For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to choose a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
Coding in Java Assignment Write the following static methods. Assume they are all in the same...
Coding in Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a...
In java coding, write a program that will print out a representation of a “W” using...
In java coding, write a program that will print out a representation of a “W” using a character that a user provides.
write a java program for area and perimeter of right triangle
write a java program for area and perimeter of right triangle
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
In this programming assignment, you are asked to write a multi-threaded program using JAVA to compute...
In this programming assignment, you are asked to write a multi-threaded program using JAVA to compute the sum of an arithmetic series from 1 to 200 using pthreads.   The main thread is responsible for creating two child threads The first child thread should compute the partial sum of the series from 1 to 100 and the second computes the partial sum of another series from 101 to 200. The main thread should accumulate the partial sums computed by the child...
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS)...
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS) which works as a simple calculator. The program will get two integer numbers, and based on the requested operation, the result should be shown to the user. a. The program should print a meaningful phrase for each input, and the result. i. “Enter the first number” ii. “Enter the second number” iii. “Enter the operation type” iv. “The result is” b. The user should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT