Starting from:
$29

$26.10

Assignment 1-Loops and conditionals

 Assignment 1 – 110/100 points possible 
The goals of this assignment are to get your development environment set up and to provide
experience with using loops and conditionals in Java and with building a simple executable Java
application.
Write a Java program which sums a list of positive integers entered by the user. To accomplish
this, you’ll repeatedly prompt the user to enter an integer and use special-case integer values
to determine when to print the sum, clear the sum, and quit the application. You may get user
input either from the console or by writing a simple UI. We’ll go over the former method in
class. Your program should:
1. [10] Display text asking the user to enter an integer. You should indicate to the user that
entering -1 will print the current sum, -2 will reset the current sum, and -3 will quit the
application. For example:
Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
2. [15] Read an integer from the user.
3. [15] If the user entered -1, print the current sum and then prompt the user for another
integer.
4. [15] If the user entered -2, set the current sum back to 0 and then prompt the user for
another integer.
5. [15] If the user entered -3, print the sum and then quit the application.
6. [15] If the user entered a positive integer, add it to the sum and then prompt the user for
another integer.
7. [15] If the user entered a negative number less than 3, simply ignore the number and then
prompt the user for another integer. It’s worth noting that your application will likely crash if
the user enters text that cannot be parsed as an integer (depending on your input method).
We will cover this later in the term, so handling this case is not required (see extra credit).
8. [+10] (Extra credit) Allow the user to enter non-integer input without crashing the
application. ☺ Hint: you’ll likely need to use a try/catch block, which is covered in chapter 7 of
the text.
Here’s output from a sample run:
Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
1Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
2
Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
3
Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
-1
Sum: 6
Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
4
Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
-1
Sum: 10
Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
-2
Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
-1
Sum: 0
Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
1
Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
-1
Sum: 1
Enter a positive integer (-1 to print, -2 to reset, -3 to exit):
-3
Sum: 1

More products