A river basin monitoring system assigns each basin a unique code in the format RB followed by a numeric identifier (for example, RB4319) of 4 digits only.
Write a program to accept a basin code and rainfall readings for N days, where 5 ≤ N ≤ 20. If the basin code is not in the correct format or if the value of N is outside the specified range, display an appropriate error message.
Your program should perform the following tasks:
- Extract the basin number from the basin code.
- Calculate the sum of all digits in the basin number.
- Store the rainfall readings and display:
i. Highest rainfall
ii. Lowest rainfall
iii. Average rainfall - Count the number of days on which the rainfall exceeds 150 mm.
- Display the rainfall status:
If the average rainfall is greater than 150 mm, display: FLOOD WARNING GENERATED
Otherwise, display NORMAL CONDITIONS
Test your program with the following data and some random data:
Example 1
INPUT:
Basin Code: RB4319
N: 6
180, 145, 170, 155, 190, 130
OUTPUT:
Basin Number: 4319
Digit Sum: 17
Highest Rainfall: 190
Lowest Rainfall: 130
Average Rainfall: 161.67
Days exceeding 150 mm: 4
FLOOD WARNING GENERATED
Example 2
INPUT:
Basin Code: RB5824
N: 5
120, 135, 140, 160, 145
OUTPUT:
Basin Number: 5824
Digit Sum: 19
Highest Rainfall: 160
Lowest Rainfall: 120
Average Rainfall: 140.00
Days exceeding 150 mm: 1
NORMAL CONDITIONS
Example 3
INPUT:
Basin Code: RB7608
N: 7
210, 185, 160, 175, 220, 195, 180
OUTPUT:
Basin Number: 7608
Digit Sum: 21
Highest Rainfall: 220
Lowest Rainfall: 160
Average Rainfall: 189.29
Days exceeding 150 mm: 7
FLOOD WARNING GENERATED
Example 4
INPUT:
Basin Code: R4319
OUTPUT:
Invalid Basin Code
Example 5
INPUT:
Basin Code: RB4319
N = 3
OUTPUT:
Invalid Number of Days
import java.util.Scanner;
class RiverBasin{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Basin Code: ");
String code = in.nextLine().toUpperCase();
if(code.length() != 6 || !code.startsWith("RB")){
System.out.println("Invalid Basin Code");
return;
}
for(int i = 2; i < code.length(); i++){
char ch = code.charAt(i);
if(!Character.isDigit(ch)){
System.out.println("Invalid Basin Code");
return;
}
}
System.out.print("N: ");
int n = Integer.parseInt(in.nextLine());
if(n < 5 || n > 20){
System.out.println("Invalid Number of Days");
return;
}
int a[] = new int[n];
System.out.println("Enter rainfall readings:");
int lowest = 0;
int highest = 0;
int total = 0;
int count = 0;
for(int i = 0; i < a.length; i++){
a[i] = Integer.parseInt(in.nextLine());
total += a[i];
if(a[i] > 150)
count++;
if(i == 0)
lowest = highest = a[i];
else{
if(lowest > a[i])
lowest = a[i];
if(highest < a[i])
highest = a[i];
}
}
int sum = 0;
int num = Integer.parseInt(code.substring(2));
System.out.println("Basin Number: " + num);
while(num != 0){
sum += num % 10;
num /= 10;
}
System.out.println("Digit Sum: " + sum);
System.out.println("Highest Rainfall: " + highest);
System.out.println("Lowest Rainfall: " + lowest);
double avg = (double)total / n;
avg = Math.round(avg * 100) / 100.0;
System.out.println("Average Rainfall: " + avg);
System.out.println("Days exceeding 150 mm: " + count);
if(avg > 150)
System.out.println("FLOOD WARNING GENERATED");
else
System.out.println("NORMAL CONDITIONS");
}
}