Sort 3 numbers entered by the user.
Textbook: 5.1 - 5.2
Concepts: comparison/relational operators;
else if, nested
Ask the user to enter three integers.
Read these in using Scanner's
Then print those three numbers out in both smallest-to-largest order and largest-to-smallest 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 lowest-to-highest: 1 2 3 Sorted highest-to-lowest: 3 2 1 D:\TA\grading\A06>java ZtomaszeA06 Please enter 3 integers to sort: 79 0 -14 Sorted lowest-to-highest: -14 0 79 Sorted highest-to-lowest: 79 0 -14 D:\TA\grading\A06>java ZtomaszeA06 Please enter 3 integers to sort: 35 6 12 Sorted lowest-to-highest: 6 12 35 Sorted highest-to-lowest: 35 12 6 D:\TA\grading\A06>java ZtomaszeA06 Please enter 3 integers to sort: 7 12 7 Sorted lowest-to-highest: 7 7 12 Sorted highest-to-lowest: 12 7 7 D:\TA\grading\A06>java ZtomaszeA06 Please enter 3 integers to sort: one five nine Sorry, but you must enter only integers (whole numbers).
Since the point of this assignment is to practice using conditionals, please do not use any loops (such as
while), arrays, or other collections (such as an
ArrayList) on this assignment.
UsernameA06.java file to Tamarin.
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
parseInt, which we haven't talked about in lab yet.) Basically, if you make 3 calls to
nextInt(), it will skip over the white space whether it is a space or a
\nafter each number. In the sample output above, sometimes I entered the 3 numbers on one line with spaces between them; other times, 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.
A variable-based "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
second variables instead of creating
Then, determine where the third number falls in that order: before the
small number, between the
large, or after the
large. Use 3 new variables (such as
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
largest variables, the printing is easy.
A "brute-force 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,
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/else-if 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/else-if/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!