Here is the answer for your question
in Java Programming Language.
Kindly upvote if you find
the answer helpful.
NOTE : I have given code for
both Excercise 1 and Excercise 2. Please comment if you have any
doubts.
###############################################################
CODE :
import java.text.DecimalFormat;
import java.util.Scanner;
public class RainFallStatistics {
static DecimalFormat f = new DecimalFormat("#.##");
public static void main(String[] args){
System.out.println("===========================================");
System.out.println("RAINFALL STATISTICS");
System.out.println("===========================================");
run();
}
public static void run(){
//Required variables
String[] months = { "JULY", "AUGUST", "SEPTEMBER", "OCTOBER",
"NOVEMBER", "DECEMBER" };
double[][] yearRainFalls = new double[2][7];
double[] totalRainFalls = {0,0};
double[] highestRainFall = {0,0};
double[] averageMonthlyRainFalls = new double[6];
Scanner s = new Scanner(System.in);
//Read information from user
for(int i = 0;i<2;i++){
//Read year as input from user
System.out.print("Enter the #" + (i+1) + " year: ");
yearRainFalls[i][0] = s.nextInt();
//validate it and ask repeatedly if invalid data enetered
while(!isValid(yearRainFalls[i][0],"year")){
System.out.println("Year should be greater than 1990....Try
again");
System.out.print("Enter the first year: ");
yearRainFalls[i][0] = s.nextInt();
}
for(int j=1;j<7;j++){
//Read rainfall of each month of the year
System.out.print("Enter rainfall of " + months[j-1] + " : ");
yearRainFalls[i][j] = s.nextDouble();
//validate it and ask repeatedly if invalid data enetered
while(!isValid(yearRainFalls[i][j],"rainfall")){
System.out.println("Rainfall cannot be negative....Try
again");
System.out.print("Enter rainfall of " + months[j-1] + " : ");
yearRainFalls[i][j] = s.nextDouble();
}
//Sum up rainfalls of current year
totalRainFalls[i] += yearRainFalls[i][j];
//Find highest rainfall
if(yearRainFalls[i][j] > highestRainFall[i]){
highestRainFall[i] = yearRainFalls[i][j];
}
}
System.out.println("=============================================");
}
//Calculate averge rainfall
for(int i=1;i<7;i++){
averageMonthlyRainFalls[i-1] = (yearRainFalls[0][i] +
yearRainFalls[1][i]) / 2;
}
//Display output
System.out.println("==============================");
System.out.println("Rain Fall Statistics");
System.out.println("==============================");
System.out.println("Total rainfall of year " +
(int)(yearRainFalls[0][0]) + " is " + totalRainFalls[0]);
System.out.println("Total rainfall of year " +
(int)yearRainFalls[1][0] + " is " + totalRainFalls[1]);
System.out.println("Average Monthly Rainfall: ");
for(int i = 0;i<6;i++){
System.out.println("For the month of " + months[i] + " : " +
f.format(averageMonthlyRainFalls[i]));
}
System.out.println("==============");
System.out.println("Highest rainfall of the year " +
(int)yearRainFalls[0][0] + " is " + highestRainFall[0]);
System.out.println("Highest rainfall of the year " +
(int)yearRainFalls[1][0] + " is " + highestRainFall[1]);
//EXERCISE - 2 CODE SEGMENT
System.out.println("Year that received most rain " +
(int)((totalRainFalls[0] >
totalRainFalls[1])?yearRainFalls[0][0]:yearRainFalls[1][0]));
}
//isValid method that takes value and type of input
public static boolean isValid(double value,String type){
//if it is year checks whether year is > 1990
if(type.equalsIgnoreCase("year")){
if(value > 1990)
return true;
}else if(type.equalsIgnoreCase("rainfall")){
//If it is rainfall checks if it is non-negative
if(value > 0)
return true;
}
return false;
}
}
|
######################################################################
SCREENSHOTS :
Please see the screensots of the code below for the indentations
of the code.
################################################################
OUTPUT :
Input
Output
Any doubts regarding this can be explained with pleasure
:)