# JAVA assignment help

Printable View

• 10-15-2012, 05:35 AM
ScarabVenom
JAVA assignment help
Hi,

I'm new to Java and I have to write a program that sorts 3 names alphabetically. I didn't learn Arrays yet so, I can't use it. I wrote the program already using if-else-if statements and it's working but, in order to do this I had to basically code every single possibility. The professor said that there's a way in which you compare two of the names, find the smallest and keep it at the bottom and then compare the other two. She refused to say more and I really can't find a way. I hope someone can help.
I know that this is not a "do homework for you" forum. I already have the program compiling and working and I can provide my code. But, I just don't understand how comparing 2 names can show me the smallest one. For example, if I compared the first 2 names and found the smallest of them....well, the third one can turn out to be the smallest.
• 10-15-2012, 06:07 AM
Junky
Re: JAVA assignment help
Quote:

Originally Posted by ScarabVenom
find the smallest and keep it at the bottom

At the bottom of what?

Consider doing this with 3 ints (8,2,5). What you can do is compare the first value to the second and the first value to the third to see if it is smallest (or largest). If it is then you need to compare the other two values to know what order they fall in using a nested if statement.

So it would something like:
if (8 > 2 && 8 > 5)
if (2 > 5)
the order is 8 2 5 (or 5 2 8 depending on ascending or descending)
else
the order is 8 5 2 (or 2 5 8)
else if.... (a)

else
(b)

(a) We now know the first value is not the largest so all you need do is compare the second and third value
(b) We now know the last value must be largest.

In both (a) and (b) you will still need to perform the comparison between the other 2 values.