HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Inputting and sorting three integers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
threesortinginputtingandintegers

Problem

The problem to the question is:


Write a program that takes 3 integers as an input from the user using input dialog messages and sorts the three numbers.

I just want to know: is there any major differences between my code and the book's code in terms of efficiency or anything specific that I could have done better in my code? I am slowly learning Java and am trying to pick up the best techniques as I go along trying to learn this language. Also, I really don't understand the logic behind the book's answer. It seems as if they are only comparing two numbers, but how can they guess what the third number will be in the sort?

Note: I am following my textbook and the newest thing we have learned are if and else statements along with switch statements. I have not learned any looping and I am still in the basics. I am following Introduction To Java Programming 9th edition Daniel Liang and this is from the Chapter 3 exercises.

My solution:

```
package Chapter3Exercises;
import javax.swing.JOptionPane;

public class SortThreeIntegers
{
public static void main(String[] args)
{
//Prompt user to enter three integers

String stringNum1 = JOptionPane.showInputDialog(null, "Please enter 1st Integer: ");
String stringNum2 = JOptionPane.showInputDialog(null, "Please enter 2nd Integer: ");
String stringNum3 = JOptionPane.showInputDialog(null, "Please enter 3rd Integer: ");

int num1 = Integer.parseInt(stringNum1);
int num2 = Integer.parseInt(stringNum2);
int num3 = Integer.parseInt(stringNum3);

if ((num1 > num2 && num1 > num3))
{
if(num2 > num3)
{
System.out.print(num3 + " " + num2 + " " + num1);
}
else
System.out.print(num2 + " " + num3 + " " + num1);
}
else if ((num2 > num1 && num2 > num3))
{
if(num1 > num3)
{
System.out.print(num3 + " " + num1 + " " +

Solution

The books solution, is definitely cleaner, and the biggest difference I can see is the books solution is actually sorting the variables, rather than just changing the display order.

In terms of efficiency, displaying stuff to the console is more efficient than popping up message boxes. There's less conditional logic in the books solution and a lot less code duplication. Other than that, there is very little difference in the sorting operation, the books maybe slightly more efficient due to less conditional checks.

Overall though, in my opinion the book provides the better solution, because it has virtually no duplicated code, conditionals are a single level deep (much easier to read), actually sorts the numbers in memory (if this program were to do more, your variables would already be sorted for you next time you need them, as opposed to having to duplicate the conditional logic, and overall it's much easier to understand what it's doing and what's going on.

Context

StackExchange Code Review Q#38247, answer score: 4

Revisions (0)

No revisions yet.