|
Assignment 11
Task
Write a program that reads in an arbitrary number of integers and then displays the entered list and their average.
Concepts: while loops, for loops; wrapper classes (including autoboxing and Integer.parseInt() ); ArrayList
Textbook: 5.5, 5.8; 3.8; 7.7
Steps
Use a loop to allow the user to enter as many integers as they like. Store each integer entered into an ArrayList . If the user enters nothing, the program should stop reading numbers. If the user enters anything other than (or in addition to) an integer, print an error message and ignore that input, but keep reading numbers.
Once the user has indicated that they are done entering numbers, use another loop to print the contents of the ArrayList. Also, print the sum of all the entered numbers and their average (as a double ).
Sample Output
D:\TA\grading\A11>java ZtomaszeA11
This program will average a series of numbers.
Enter an integer (or nothing to stop): 15
Enter an integer (or nothing to stop): -7
Enter an integer (or nothing to stop): oops
Bad input: please enter only integers (or, to quit, just hit enter).
Enter an integer (or nothing to stop): 100
Enter an integer (or nothing to stop): 0
Enter an integer (or nothing to stop): 3o
Bad input: please enter only integers (or, to quit, just hit enter).
Enter an integer (or nothing to stop): 30
Enter an integer (or nothing to stop):
You entered: 15 -7 100 0 30
Total: 138
Average: 27.6
D:\TA\grading\A11>java ZtomaszeA11
This program will average a series of numbers.
Enter an integer (or nothing to stop):
You entered:
Total: 0
Average: 0
Note how the user might not enter a number at all; your program should still handle this gracefully.
Hints
- You won't be able to use
nextInt() to read in the numbers for this one. If you did, the program would hang until the user entered one or more non-white-space characters.
- Instead, use
nextLine() to read in everything they typed. If this is an empty string, they mean to quit; otherwise, turn it into an integer using Integer.parseInt() . Remember that Integer.parseInt() throws a NumberFormatException if it cannot turn the given string into an int .
What to Submit
Upload your UsernameA11.java file to Tamarin.
Grading [5 points]
- 1 - Compiles
- Your program compiles successfully (no errors)
- 2 - Read loop
- Your program reads in an arbitrary number of integers (1.0). The program does not crash if given something other than an
int (0.5). The loop ends if the user enters nothing (0.5).
- 0.5 - ArrayList
- You store the numbers in an ArrayList.
- 1.5 - Results
- You print the list of
int s that the user entered (1.0). You print the total (0.2) and average (0.3). Average should be a double .
FAQs
- How do I tell if the user entered nothing?
- Compare the String that they entered to the empty String: "". (That's a String with zero characters in it.)
- Argh, I'm stuck!
- It usually helps me to write a hard program one step at a time, where I
compile, run, and test after each step. Igore the loop to start with and just try to get your program to compile/work for just a single number. After you read in the user's input,
print the number to the screen if it really is a number, print your error message if it's not a number, or print "quit" if it's an empty string.
Then, once you have this working, add the loop so it does the above as long
as you keep entering input. (This will be an infinite loop, but your output should now look something like the sample output above--except it will be printing the numbers you entered and just prints "quit" rather than acutally quitting. Remember, Ctrl-c will kill an infinite loop.)
Then change your program from just printing "quit" if the user enters nothing to actually
stopping the loop.
Then change it from just printing the number to adding the number to the ArrayList
instead. Now your input loop is done and you can work on printing out the contents of the ArrayList using a second loop.
- Things to note:
-
• Please declare your Scanner before the loop and not in the loop. This way, you only create one Scanner object, rather than creating a new Scanner for each number. Either way will work for you, but creating a new Scanner each time is inefficient and will currently mess up the grader. (If you already submitted it this way, don't worry about it--I'll deal with it.)
• Remember to specify what your ArrayList contains when you declare it, like this:
ArrayList<Integer> list = new ArrayList<Integer>();
If you leave off the <Integer> , it will compile but it will give you a warning. Also, when you get the numbers back out of the ArrayList, they will come out as Object s rather than as Integer s--a difference that should make more sense to you in a few weeks. At this point, you just need to know that you can't do math with Object s.
|