
Assignment 06
Task
Sort 3 numbers entered by the user.
Textbook: 5.1  5.2
Concepts: comparison/relational operators; if ,
else , else if , nested if s.
Steps
Ask the user to enter three integers.
Read these in using Scanner's nextInt() method.
Then print those three numbers out in both smallesttolargest order and largesttosmallest order.
So your final output might look something like this:
D:\TA\grading\A06>java ZtomaszeA06
Please enter 3 integers to sort: 1 2 3
Sorted lowesttohighest: 1 2 3
Sorted highesttolowest: 3 2 1
D:\TA\grading\A06>java ZtomaszeA06
Please enter 3 integers to sort: 79
0
14
Sorted lowesttohighest: 14 0 79
Sorted highesttolowest: 79 0 14
D:\TA\grading\A06>java ZtomaszeA06
Please enter 3 integers to sort: 35 6 12
Sorted lowesttohighest: 6 12 35
Sorted highesttolowest: 35 12 6
D:\TA\grading\A06>java ZtomaszeA06
Please enter 3 integers to sort: 7 12 7
Sorted lowesttohighest: 7 7 12
Sorted highesttolowest: 12 7 7
Since the point of this assignment is to practice using conditionals, please not use any loops (such as while ), arrays, or other collections (such as an ArrayList ) on this assignment.
What to Submit
Submit your UsernameA06.java file to Tamarin.
Grading [5 points]
 1  Compiles
 Your submitted program compiles successfully (no errors)
 1  Input + Output
 Read in 3 ints from the user, and those numbers appear again in your output.
 3  Sorting
 Your program prints the userentered ints in two sorted orders: smallest to largest and also largest to smallest. (1 if you use a loop, array, or collection.)
FAQs
 Demo code

This is my solution to the demo we did in lab: LegalAge.java. There's definitely more than one way to do it, so find one that works for you. You may want to try it different ways: as a series of unconnected ifs (as I did), as nested ifs, some combination, or even as a single if/elseif/else series. (What would you have to do to get this last one to work?)
Also, although you don't need to compare Strings for this assignments, here is how you might do it: Rumpelstiltskin.java
 How do I make all the entered numbers appear on one line?
 If you're using
nextInt() , this depends entirely on how the user enters the numbers. You have no control over it. (You could control it if you wanted to read in a whole line at a time and then parse the line into an int yourself. But you haven't learned how to do that parsing yet.) Basically, if you make 3 calls to nextInt() , it will skip over the white space whether it is a space or a \n after each number. In the sample code above, I entered the 3 numbers on one line with spaces between them, except on the second run, when I hit enter after each number.
Even if you prompted the user for each number individually, one per line, the user could choose to enter all 3 numbers at the first prompt (with spaces between them) and the program would still work. It wouldn't wait for the user to enter the second and third numbers, since they would already be in the Scanner's buffer from the first call to nextInt() . (Try it, and you'll see what I mean.)
Similarly, you can enter multiple blank lines or tabs between the numbers and nextInt() will skip over all that whitespace too.
 Hints
 This program is actually quite tricky, since a computer can only compare two values at once. Although there's alway more than one way to do these things, if you're completely stuck, here's a couple techniques you might consider:
A variablebased "moving the numbers around" approach. In short, you want to compare two of the values at a time and, based on the result, move them around within some variables until you know where the smallest, middle, and largest values are. Now, it is possible to do this reusing the original variables (at least in part). However, you may want to use some new variables so that you can track the status (that is, what you know) about each value during the process.
In brief: first sort only the first and second number. That is, determine which of the numbers is the small one and which is the large one, and copy them into such variables as appropriate. (Alternatively, you could just swap their values, reusing the first and second variables instead of creating small and large .)
Then, determine where the third number falls in that order: before the small number, between the small and large , or after the large . Use 3 new variables (such as smallest , middle , largest ) to store the values of the 3 numbers in a final sorted order.
Once you've done the comparisons and have the correct values stored in smallest , middle , and largest variables, the printing is easy.
A "bruteforce comparison" approach. On the other hand, you could consider all the possible configuration the 3 numbers can to come to you in. That is, the user is going to enter 3 numbers (named here, first , second , and third ). One will be classified as the smallest, one will be the middle number, and one will be the largest. (This is true even if two or even three of the numbers actually have the same value.)
So this means there are only six possible orders for the numbers as they are entered:
First Second Third
S M L
S L M
M S L
M L S
L S M
L M S
If there are only 6 possible arrangements, you could use an if/elseif structure with 6 cases to determine which arrangement is currently true and so print the values sorted accordingly.
As it turns out, both of these approaches are about as long: the first is 3 if/else blocks; the other is six connected if/elseif/else cases. So six cases with either approach. (With more than 3 numbers, some sort of "moving the numbers around" approach generally scales better than a "brute force comparison" approach. But, for this particular problem of 3 numbers, the two approaches are about equivalent.)
Again, these are just some ideas to get you moving. There are many (other) ways that you may also be able to solve this problem. You just need to find a single way that works for you!
