Assignment 06


Sort 3 numbers entered by the user.

Textbook: 5.1 - 5.2
Concepts: comparison/relational operators; if, else, else if, nested ifs.


Ask the user to enter three integers. Read these in using Scanner's nextInt() method. 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

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.

What to Submit

Submit your file to Tamarin.

Grading [5 points]

1 - Compiles
Your submitted program compiles successfully (no errors)
1 - Input + Output
Reads in 3 ints from the user, and those numbers appear again in your output (0.5). Handles non-integer input with an error message (and no other output) (0.5).
3 - Sorting
Your program prints the user-entered ints in two sorted orders: smallest to largest and also largest to smallest. (-1 if you use a loop, array, or collection.)


Demo code?
Section 001 & 004: This is my solution to the demo we did in lab:
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/else-if/else series. (What would you have to do to get this last one to work?)
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 using 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 \n after 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.

This program is actually quite tricky, since a computer can only compare two values at once. Although there's always more than one way to do these things, if you're completely stuck, here's a couple techniques you might consider:

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 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 "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, 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

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!