I need major help. This assignment is due in a few days and I\'m totally lost. I
ID: 3779599 • Letter: I
Question
I need major help. This assignment is due in a few days and I'm totally lost. It's an assembly language class using a raspberry Pi with raspbian and an ARM processor.
Explanation / Answer
package com.chegg.QA;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;
public class RainFallTest {
public static void main(String[] args) {
Scanner inputReader = new Scanner(System.in);
int rainFallValues[] = new int[12];
double totalRainfall = 0;
double avg = 0;
int most = 0;
int least = 0;
System.out.println("Please enter in the rainfall for the 12 months ahead: ");
System.out.println("Month Rainfall (In Cms)");
String months[]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
Map<Integer, String> rainfallAndMonth=new HashMap<Integer, String>();
for (int i=0;i<months.length;i++) {
System.out.println(months[i]+" :");
int val=0;
try{
val = inputReader.nextInt();
}catch(InputMismatchException ime){
System.out.println("Invalid input! try again");
System.exit(0);
}
while(val<0){
System.out.println("negatives not allowed ! enter again");
val = inputReader.nextInt();
}
rainFallValues[i]=val;
totalRainfall+=val;
rainfallAndMonth.put(val, months[i]);
}
avg = totalRainfall / 12;
System.out.println("Total rainfall for the year in centimeters: " + totalRainfall+ " in cms");
System.out.println("The average rainfall was:" + avg + " in cms");
most =getMaxValue(rainFallValues);
least=getMinValue(rainFallValues);
System.out.println("The max rainfall in month("+rainfallAndMonth.get(most)+") is: " + most);
System.out.println("The min rainfall in month("+rainfallAndMonth.get(least)+") is:"+ least);
}
private static int getMaxValue(int[] rain) {
int max = 0;
for (int i : rain) {
if (i > max) {
max = i;
}
}
return max;
}
private static int getMinValue(int[] rain) {
int min = Integer.MAX_VALUE;
for (int i : rain) {
if (i < min) {
min = i;
}
}
return min;
}
}
Test Case :1
Please enter in the rainfall for the 12 months ahead:
Month Rainfall (In Cms)
January :
1
February :
2
March :
3
April :
4
May :
5
June :
6
July :
7
August :
8
September :
13
October :
8
November :
9
December :
4
Total rainfall for the year in centimeters: 70
The average rainfall was:5.0 in cms
The max rainfall in month(September) is: 13
The min rainfall in month(January) is:1
Explination
1.To read the input from user we need scanner and to store those rainfall values i took rainFallValues[].
2.The variables totalRainfall,avg,most,min are to store their relevant data.
3.First we allow the user to enter rainfall for 12 months.here i taken months[] to represent months to know print the month name.
4.Here i used for loop to read all rainFallValues of 12 months and restricted <0 values.
5.The Map rainfallAndMonth is used to stor rainfall and month name to display the names of month.
6.To found Min and Max values of array, defined getMinValue() and getMaxValue() where comparison done for all elements within the array.
7.Total rainfall calulated while reading input.
8.Finally disaply the results of Total rainfall,avg and to display month name passed most and min values to the rainfallAndMonth.