Answer:
The JAVA program for the scenario is given below.
import java.util.Scanner;
import java.lang.*;
public class Insurance
{
//static variables to hold mandatory values
static int age;
static int premium;
static int decades;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//user prompted to enter present age
System.out.print("Enter your age in current year: ");
age = sc.nextInt();
//computation to calculate premium amount
decades = age/10;
premium = (decades + 15)*20;
System.out.println("");
System.out.println("The annual premium to be paid for age "+age+" is $"+ premium);
}
}
OUTPUT
Enter your age in current year: 45
The annual premium to be paid for age 45 is $380
Explanation:
1. The variables are declared with integer datatype to hold customer age, decades and premium.
2. All the variables are declared static so that they can be accessible inside main() which is a static method.
3. Inside main(), object of Scanner is created, sc.
4. User is asked to enter present age.
5. User input is taken into age using scanner object, sc and nextInt() method.
6. Following this, the decades in customer’s age is computed by dividing age by 10 and assigned to decades variable.
7. Next, annual premium is calculated by adding 15 to decades and then, multiplying the result by 20. The final result is assigned to premium variable.
8. A new line is inserted before printing the annual premium amount.
9. The calculated annual premium amount based on customer’s present age is displayed.
10. The class having main() method is named Insurance. Hence, the program is named as Insurance.java.
11. Only one class is taken in the program since nothing is specifically mentioned about classes in the question.
12. The Insurance class is declared public since execution begins with main() method.
13. If more than one class is present in the Java program, only the class having main() method is declared public; other classes are not declared public.