Question 1

The objectives of this question are

  • to declare variables
  • to debug simple Java program
  • to write Java program with selection structure

(a) The following images show some items sold in Cold StorageTM Online Supermarket.

Suppose you are tasked to identify the variables needed to store data about each item. Note that

  • the size is stored in two parts: the magnitude and the unit.
  • the packaging type is stored as p for pack, b for bot, x for box etc.
  • the file name for the item image is also stored.

Write Java declaration statements to declare the variables. You are required to follow Java naming convention.

(b) A program shown in Figure 1 calculates which quarter of the year a month falls in and the percentage of year for that month. For example, month 4 falls in the second quarter and is 33.3% of a year, and month 9 falls in the third quarter and is 75.0% of a year. The month value is entered at the command line argument.

1. public class 1b
2. {
3. public static void main(String[] args)
4. {
5. int month;
6. int quarter;
7. double percentage;
8. month = (int) args[0];
9. quarter = (int) month/3.0 + 1;
10. percentage = (month/12)100;
11. System.out.printf("Quarter of year for month %1d",
12 month, " is %1d", quarter);
13. System.out.printf("Percentage of year for month %1f%%",
14. Percentage);
15. }
16. }

(i) This program does not compile successfully. Compile your program, correcting one error at a time until there is no more compilation error. Identifying only the compilation errors in the program, complete the table:

Line number
Compilation error
Explanation of error
Corrected statement (write the whole statement)

(ii) The program will encounter runtime and logic error. Modify the program to correct the errors so that

  • the first line of output shows your name and student number, and
  • the program produces correct result. For example, for the month value 4, the output should be:
Your Name Your Student Number
Quarter of year for month 4 is 2
Percentage of year for month 33.3%

Submit your program listing together with screenshot showing one run of your program.

(c) Refer to table 1 which lists the insurance premium for adults based on the occupation class (class 1-3 – refer to table 2) and the choice of coverage (standard or deluxe) See image.

Write a program that prompts and gets

  • the choice of coverage (standard or deluxe)
  • the class the insured falls under (1, 2 or 3)
  • the number of children to be insured together (0, 1, 2, …)

A sample run is shown below with user inputs are underlined:

Enter the choice of coverage (S - standard, D - deluxe): d
Enter the class of occupation (1-3): 1
Enter number of children to insure: 0
Total premium for Deluxe coverage for 1 class 1 adult only is $169.06

Your program output depends on the choice of coverage, the occupation class and the number of children to insured with the adult. Use printf to produce output in one of the following format:

Total premium for Standard coverage for 1 class 2 adult only is $120.91
or
Total premium for Standard coverage for 1 class 2 adult and 1 child is $147.66
or
Total premium for Deluxe coverage for 1 class 1 adult and 3 children is $310.30

Submit your program listing together with screenshot showing THREE runs of your program for a varying output. You should include your name and student number in the output.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.