Write a Java program that calculates the sum of a list of integers entered by the user. You will repeatedly prompt the user to enter a letter or an integer, and then print the sum, clear the sum, add the integer to the sum, or quit the program.
1. Display the text to ask the user for the input. You should indicate to the user that entering p will print the current sum, r will reset the current sum, q will quit the program, and i to add an integer to the sum. It looks like this:
Enter p to print, r to reset, q to quit, and i to add an integer:
2. The user entered a letter. Read the user’s input.
3. If the user entered a letter other than p, r, q, and i, ignore it and then prompt the user for another input. (It is capitalization sensitive here, so R is not r, and vice versa, for example.)
4. If the user entered p, print the current sum and then prompt the user for another input.
5. If the user entered r, set the current sum to 0, print 0, and then prompt the user for another input.
6. If the user entered q, print the current sum and then quit the program.
7. If the user entered i, prompt the user for the integer. It looks like this:
Enter the integer:
8. Afterwards, the user entered an integer. Read it and add it to the current sum, and then prompt the user for another input.
9. Write codes that are clear and efficient. Specifically, your codes should be indented with respect to code blocks, avoid unnecessarily repetitive codes, avoid codes that are commented out or otherwise unused, use descriptive and consistent class/method/variable names, etc.
Here’s the output from a sample run:
Enter p to print, r to reset, q to quit, and i to add an integer:
p
Sum: 0
Enter p to print, r to reset, q to quit, and i to add an integer:
i
Enter the integer:
1
Enter p to print, r to reset, q to quit, and i to add an integer: i
Enter the integer:
2
Enter p to print, r to reset, q to quit, and i to add an integer: i
Enter the integer:
-4
Enter p to print, r to reset, q to quit, and i to add an integer:
p
Sum: -1
Enter p to print, r to reset, q to quit, and i to add an integer: i
Enter the integer:
-2
Enter p to print, r to reset, q to quit, and i to add an integer:
p
Sum: -3
Enter p to print, r to reset, q to quit, and i to add an integer: i
Enter the integer:
4
Enter p to print, r to reset, q to quit, and i to add an integer:
s
Enter p to print, r to reset, q to quit, and i to add an integer: r
Sum: 0
Enter p to print, r to reset, q to quit, and i to add an integer: q Sum: 0