Starting from:
$30

$24

Assignment 1 Solution

Write a Java program that calculates the sum of a list of positive integers entered by the user. You will repeatedly prompt the user to enter an integer and use special integer values to determine when to print the sum, clear the sum, and quit the application. Your program should:




[10] Display text asking the user to enter an integer. You should indicate to the user that entering -3 will print the current sum, -2 will reset the sum to 0, and -1 will quit the application. For example:



Enter a positive integer (-3 to print, -2 to reset, -1 to quit):




[20] Read an integer from the user.



[10] If the user entered -3, print the current sum and then prompt the user for another integer.



[10] If the user entered -2, set the current sum back to 0 and then prompt the user for another integer.



[10] If the user entered -1, print the sum and then quit the application.



[10] If the user entered a positive integer, add it to the current sum and then prompt the user for another integer.



[10] If the user entered 0 or a negative number less than -3, simply ignore that and then prompt the user for another integer.



[20] Write code that is clear and efficient. Specifically, your code should be indented with respect to code blocks, avoid unnecessarily repetitive code, avoid code that is commented out or otherwise unused, use descriptive and consistent class/method/variable names, etc.



Here’s output from a sample run:




Enter a positive integer (-3 to print, -2 to reset, -1 to quit):




-3




Sum: 0




Enter a positive integer (-3 to print, -2 to reset, -1 to quit):




1




Enter a positive integer (-3 to print, -2 to reset, -1 to quit):




2




Enter a positive integer (-3 to print, -2 to reset, -1 to quit):




3




Enter a positive integer (-3 to print, -2 to reset, -1 to quit):

-3




Sum: 6




Enter a positive integer (-3 to print, -2 to reset, -1 to quit):




-4




Enter a positive integer (-3 to print, -2 to reset, -1 to quit):




-3




Sum: 6




Enter a positive integer (-3 to print, -2 to reset, -1 to quit):




-2




Enter a positive integer (-3 to print, -2 to reset, -1 to quit):




-3




Sum: 0




Enter a positive integer (-3 to print, -2 to reset, -1 to quit):




-1




Sum: 0




Please zip your Java source file(s), i.e., .java file(s), into a zipped file, rename that file as <Your Full NameAssignment1.zip, e.g., BillGatesAssignment1.zip, and then upload that file to Canvas. Do not put the Java source file(s) in a folder and zip that folder; instead, please directly zip all the Java source files into a single zipped file.

More products