Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using JAVA, write a nested loops program that collects, calculates, and displays

ID: 3700758 • Letter: U

Question

Using JAVA, write a nested loops program that collects, calculates, and displays the average lung cancer rate over a period of years. The user will be asked to enter the number of years.The outer loop will iterate once for every year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the number of cancer cases for that month. After all iterations, the program should display the number of months, the total number of lung cancer cases, and the average lung cancer cases per month for the entire period. Input Validation: Do not allow negative numbers for the monthly lung cancer cases.

Explanation / Answer

LungCancerCasesTest.java

import java.util.Scanner;

public class LungCancerCasesTest {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter the number of years: ");

int n = scan.nextInt();

int totalCases = 0,cases;

for(int i=1;i<=n;i++) {

for(int j=1;j<=12;j++) {

System.out.println("Enter year "+i+" month "+j+" lung cases: ");

cases = scan.nextInt();

while(cases<0) {

System.out.print("Invalid Input. ");

System.out.println("Enter year "+i+" month "+j+" lung cases: ");

cases = scan.nextInt();

}

totalCases+=cases;

}

}

System.out.println("The number of months: "+n*12);

System.out.println("The total number of lung cancer cases: "+totalCases);

System.out.println("The average lung cancer cases per month for the entire period: "+(totalCases)/(double)(n*12));

}

}

Output:

Enter the number of years:
2
Enter year 1 month 1 lung cases:
11
Enter year 1 month 2 lung cases:
22
Enter year 1 month 3 lung cases:
33
Enter year 1 month 4 lung cases:
44
Enter year 1 month 5 lung cases:
55
Enter year 1 month 6 lung cases:
66
Enter year 1 month 7 lung cases:
77
Enter year 1 month 8 lung cases:
88
Enter year 1 month 9 lung cases:
99
Enter year 1 month 10 lung cases:
100
Enter year 1 month 11 lung cases:
112
Enter year 1 month 12 lung cases:
13
Enter year 2 month 1 lung cases:
33
Enter year 2 month 2 lung cases:
1
Enter year 2 month 3 lung cases:
2
3Enter year 2 month 4 lung cases:
4
Enter year 2 month 5 lung cases:
5
Enter year 2 month 6 lung cases:
6
Enter year 2 month 7 lung cases:
7
Enter year 2 month 8 lung cases:
8
Enter year 2 month 9 lung cases:
9
Enter year 2 month 10 lung cases:
10
Enter year 2 month 11 lung cases:
11
Enter year 2 month 12 lung cases:
12
The number of months: 24
The total number of lung cancer cases: 858
The average lung cancer cases per month for the entire period: 35.75

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote