File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Simple Logic Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Logic Question" Watch "Simple Logic Question" New topic

Simple Logic Question

J Morberg

Joined: Mar 02, 2003
Posts: 5
Hi! I'm very new to Java, so please be gentle *_*
I'm trying to write a .java file that will get three numeric values from the user, sort them in ascending order, and print the sorted values.
This is the basis for the program:The most important thing is to be able to make this work with ONLY 3 IF statements.
Can anyone help? I assume it's pretty easy...but I have not been able to figure it out.
Thank you so much in advance...
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ March 02, 2003: Message edited by: Dirk Schreckmann ]
Syamsul Hussin
Ranch Hand

Joined: Feb 09, 2003
Posts: 59
why not use an array of integers instead. try this (selection sort):-

im not sure if it is correct. basically how it work :-
1) find the smallest element of the array(from index j+1 to the end of the array)
2) swap it with index at k
hope this helps.
[ March 02, 2003: Message edited by: Syamsul Hussin ]

----------------------------------<br />SCJP 1.4
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
Put 3 coins of different size on the tabletop.
What comparisons do you need to make to see which is the biggest, smallest and the mid-sized coin?
Once you have a system that works, see if you can reduce it to 3 comparisons.
Sometimes it helps to (1) assume that whatever your looking for is the first one you get then (2) compare at all others and if you assumtion proves wrong, change your choice.
[ March 02, 2003: Message edited by: Elouise Kivineva ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You could also do it with no ifs if you use the Arrays class and the appropriate sort() method.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
J Morberg

Joined: Mar 02, 2003
Posts: 5
Thank you everyone for your help~!
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

You could also do it with no ifs if you use the Arrays class and the appropriate sort() method.

Yes, and actually, anything else would be a suboptimal solution. Why code the buble-heap-selection-quicksort routines from scratch if you have a standard, well tested, language-supported functionality?
Incidentally, the default sort implementation in JDK1.4.1 is the modified mergesort which guarantees Nlog(N) performance.
I agree. Here's the link:
subject: Simple Logic Question
It's not a secret anymore!