|
Assignment 14
Task
Sort a list of user-entered integers.
Textbook: 3.8; 7.7
Concepts: wrapper classes (including autoboxing); ArrayList .
Steps
Ask the user to enter a series of integers. When they're done (indicated by entering nothing--as with A11), print all the numbers they entered in sorted order on one line, starting with the highest value.
Sample Output
This program will sort the numbers you enter.
Enter an integer (or nothing to stop): 0
Enter an integer (or nothing to stop): 12
Enter an integer (or nothing to stop): -2
Enter an integer (or nothing to stop): 12.5
Bad input: please enter only whole numbers (or, to quit, just hit enter).
Enter an integer (or nothing to stop): nothing
Bad input: please enter only whole numbers (or, to quit, just hit enter).
Enter an integer (or nothing to stop): enter
Bad input: please enter only whole numbers (or, to quit, just hit enter).
Enter an integer (or nothing to stop): 5
Enter an integer (or nothing to stop):
You entered: 12 5 0 -2
Hints
You're going to need to store the numbers given to you by the user in an ArrayList<Integer> until it's time to print them out. There's different ways to produce the sorted order:
- The easiest way is to add the integers to the list in a sorted order. That is, each time you go to add another number to the ArrayList, use a loop to go past each number already in the list that's larger than the number you're adding. Then add the number at that place in the list (which could be the end of the list). [Can be done with a single well-crafted loop; or with a loop and a conditional.]
- On the other hand, you could just add each number to the end of the list and then try to sort the list before printing it. One (rather brute-force) way to sort it is to loop through all the possible
int values from smallest to largest (or at least from the smallest value found in the list to the largest value found in the list). For each possible int value, loop through the list to see if that value is in the list. If it is, print it out once for each time it occurs in the list. [Can be done with nested for loops and a conditional.]
- A more efficient way to sort the list would be to create a second ArrayList. Then, loop through the original list looking for the largest value. Once found, remove that value from the original list and add it to the end other second ArrayList. Keep doing this until all values have been moved from the first to the second ArrayList; the second ArrayList will be sorted. [This is a variation of the selection sort algorithm; takes two nested loops and a conditional.]
What to Submit
Upload your UsernameA14.java file to Tamarin.
Grading [4 points]
- 1 - Compiles + Coding Standards
- Your program compiles successfully (no errors). Your code follows Java coding standards.
- 0.5 - ArrayList
- Your program uses an ArrayList to store the numbers read in from the user
- 1.5 - Input/Output
- Your program prompts the user to enter integers, stopping if the user enters nothing (empty string) (0.5). Your program then prints out a list on one line of all the numbers the user entered (1.0).
- 1 - Correctly sorted.
- When the program prints out the entered numbers, they are in sorted order, from highest to lowest value.
FAQs
- We went over a lot of code in lab and I didn't get it all down...
- Here's the code that would read in strings from the user and store them in an ArrayList: A14Demo.java
And the code for finding the smallest number in an ArrayList of numbers:
ArrayList<Integer> numbers = new ArrayList<Integer>();
...
int smallest = numbers.get(0);
for (int i = 1; i < numbers.size(); i++) {
if (numbers.get(i) < smallest) {
smallest = numbers.get(i);
}
}
System.out.println(smallest);
Update: Note that this code will fail if there is nothing in the list, since numbers.get(0) will throw an IndexOutOfBoundsException . So you might only want to try sorting the list if the size() of the list is > 0.
- I'm still stuck on sorting the numbers.
- As mentioned above, you can take each user-entered number and insert it into the list in sorted order. Or you can sort the list after you've read all the numbers in. Technically, you don't need the numbers again after printing them out, so you could just remove them from the list in sorted order and print them out. (The alternative is, when you remove a number from the list, copy it into the end of a second ArrayList. Then print the second, sorted ArrayList.)
To print an unordered list of numbers in highest-to-lowest order, you could do something like this:
while (there are still elements in the list) {
find the largest element in the list. (This requires a loop)
remove the largest element from the list and print it
}
The demo code above shows how to find the smallest number in a list. You'll probably need to save the index of the largest number as well as its value in order to remove it after the finding loop.
|