Need help with java coding.. here is the assignment:::::::::::: Write a program
ID: 3548344 • Letter: N
Question
Need help with java coding.. here is the assignment::::::::::::
Write a program that stores the monthly rainfall in Long Beach city. The program displays the following information:
Rainfall data for 12 months:
0.5,1.0,0.7,2.5,1.2,0.0,0.1,3.5,1.4,0.7,2.1, and 1.2
Output sample:
August has the largest rainfall( 3.5 inches).
June has the smallest rainfall( 0.0 inches).
The average rainfall is 1.2416666666666665 inches.
Enter the month: (1-12)
5
The rainfall for May is 1.2 inches.
Note: Below is the code to display the month that has the highest rainfall along the rainfall value. The code should provide you some guidances and ideas to complete items 2, 3 and 4 in the lab assignment.
import java.util.*;
import java.util.*;
public class Test
{public static void main(String []args)
{
Scanner input = new Scanner(System.in);
double rainFall[ ] = {0.5,1.0,0.7,2.5,1.2,0.0,0.1,3.5,1.4,0.7,2.1,1.2};
String months[]={ "January","February","March","April","May","June","July","August","September","October","November","December"};
//Code to display the month with the highest rainfall
double largestRainFall = rainFall[0];//Assume the january rainfall is the largest
int monthwithLargestRainFall = 0; //January
for(int month = 1; month < rainFall.length; month++)
{
if(rainFall[month] > largestRainFall)
{ largestRainFall = rainFall[month];
monthwithLargestRainFall = month;
}
}
System.out.println(months[monthwithLargestRainFall]+ " has the largest rainfall" + "( " + largestRainFall +" inches).");
Explanation / Answer
There is the typo in the question :)
I think it should be...
display the month that has the highest rainfall along the rainfall value
display the month that has the lowest rainfall along the rainfall value.....
My code will calculate the month with the maximum value of the rainfall, and also the month with minimum value of rainfall...
******************************************************************************************************************************
The OUTPUT from my code :
August has the largest rainfall( 3.5 inches).
June has the smallest rainfall( 0.0 inches).
Average rainfall is : 1.2416666666666665
Enter the month: (1-12) :
10
The rainfall for October is 0.7 inches.
Code :
import java.util.Scanner;
public class Test{
public static void main(String []args){
Scanner input = new Scanner(System.in);
double rainFall[ ] = {0.5,1.0,0.7,2.5,1.2,0.0,0.1,3.5,1.4,0.7,2.1,1.2};
String months[]={ "January","February","March","April","May","June","July","August","September","October","November","December"};
// Code to display the month with the highest rainfall
double largestRainFall = Integer.MIN_VALUE;
double smallestRainFall = Integer.MAX_VALUE;
int largestRainMonth = 0;
int smallestRainMonth = 0;
for(int i = 1; i < rainFall.length; i++)
{
if(rainFall[i] > largestRainFall){
largestRainFall = rainFall[i];
largestRainMonth = i;
}else if(rainFall[i] < smallestRainFall){
smallestRainFall = rainFall[i];
smallestRainMonth = i;
}
}
System.out.println(months[largestRainMonth]+ " has the largest rainfall" + "( " + largestRainFall +" inches).");
System.out.println(months[smallestRainMonth]+ " has the smallest rainfall" + "( " + smallestRainFall +" inches).");
getAverageRainfall(rainFall);
System.out.println("Enter the month: (1-12) : ");
int monthInput = input.nextInt();
if(monthInput != 0){
System.out.println("The rainfall for "+months[monthInput-1] +" is " + rainFall[ monthInput-1] +" inches.");
}
}
private static void getAverageRainfall(double[] rainFall) {
double sum = 0;
for(int i = 0; i < rainFall.length; i++){
sum += rainFall[i];
}
System.out.println("Average rainfall is : " + sum/rainFall.length);
}
}