ArrayList to sumValues() JAVA I give a score if all the criteria are applied. :)
When submitting this lab, submit a .java file called ArrayListSum, and create the following structure in Eclipse:
Package Name: week8
Class Name: ArrayListSum
Write a program that asks the user to enter numbers into an ArrayList. Allow the user to enter as many numbers as they want. Validate that each of these numbers is an integer. When the user is done entering numbers, they will hit the enter key at the prompt without entering any data.
Have your main() call a second method that you write called sumValues() passing the ArrayList as an argument. Your sumValues() method will add up all the numbers in the ArrayList and return the total back to main(). You must use a loop in your sumValues() method to add the numbers. Use of any Java method to add items in an ArrayList is not allowed. Print out the ArrayList of numbers with each number's corresponding ArrayList index as shown in the example run from the main().
Passing an ArrayList to sumValues
Then have your main() print how many elements are in the ArrayList and their sum total.
After the program has finished, ask the user if they would like to play again. Use input validation to ensure the user answered with a "Y" or "N". Keep the program running in an infinite loop until the user indicates they want to quit.
Criteria (READ PLEASE)
The ArrayList of numbers is populated with user input, allowing the user to decide how many numbers to enter.
Write a method sumValues() which is called from main(), passing your ArrayList as an argument.
This criterion is linked to a Learning OutcomeA calculation of the sum is done using a For-loop (use of any Java method to calculate the sum is NOT allowed).
This criterion is linked to a Learning OutcomeInput validation is done on all numbers in the ArrayList and the "Y"/"N" play again question.
Global variables are NOT used.
Student output looks like the example run.
Example Run
This program will ask the user to enter a series of numbers.
The user may enter as many numbers as they wish, hitting the enter key with no data when they wish to stop.
The program will then display the array of numbers and the sum total of those numbers.
Please enter a number (or just hit enter to finish): number
Invalid Response! Please enter a whole number.
Please enter a number (or just hit enter to finish): 4
Please enter a number (or just hit enter to finish): 5
Please enter a number (or just hit enter to finish): six
Invalid response. Please enter a whole number.
Please enter a number (or just hit enter to finish): 6
Please enter a number (or just hit enter to finish):
Index Item
0 4
1 5
2 6
There are 3 items in the ArrayList.
The sum total of numbers in the ArrayList is 15.
Do you wish to play again? (Y/N): I do!
Invalid response. Please enter either a 'Y' or 'N'.
Do you wish to play again? (Y/N): y
This program will ask the user to enter a series of numbers.
The user may enter as many numbers as they wish, hitting the enter key with no data when they wish to stop.
The program will then display the array of numbers and the sum total of those numbers.
Please enter a number (or just hit enter to finish): 1
Please enter a number (or just hit enter to finish): 2
Please enter a number (or just hit enter to finish):
Index Item
0 1
1 2
There are 2 items in the ArrayList.
The sum total of numbers in the ArrayList is 3.
Do you wish to play again? (Y/N): n