Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Sort numbers in descending order using nested if statements

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm not sure what Im doing wrong in my code. I am trying to get the code to allow a user to type in three double numbers and then have the program put it in descending. This is the code I wrote:



So then I output the code (at least with the set of numbers I used) it has the correct largest and smallest value but for the middle value it sets it to 0. What is it that I am doing wrong in my code?
 
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You won't get sense out of lots of code like that; you need to plan the code before writing anything. You can try going through the code with a pencil and paper, but you will probably find it simpler to start by writing out the algorithm and starting the code anew.

Sorry to appear so negative.
 
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My interpretation of the "nesting" requirement is that you shouldn't end up with any logical AND's (&&) in your if() expressions. Something like this
 
Bartender
Posts: 2483
108
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code would be way shorter with Collections or Arrays. You can use the built in methods to sort and then assign the values to these three variables. If this is your training assignment and the above topics are not covered, I'd stick to if-else conditions.
 
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simplify this quite a bit if you use deductive reasoning to eliminate certain checks.
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with this is that
requires an &&, which, if you break it down correctly, is not necessary.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:The problem with this is that
requires an &&


Why is that a problem? It's effectively the same as having another nested if statement.
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Carey Brown wrote:The problem with this is that
requires an &&


Why is that a problem? It's effectively the same as having another nested if statement.


Well, yes, it can be handled by another level of nesting, but that seems to be Hanna's problem, she jumps to && instead of nesting.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:but that seems to be Hanna's problem, she jumps to && instead of nesting.


I still don't see why that's a problem. Did I miss a requirement that says "use nested if-statements instead of the && operator"? If not, then I don't see why you can't use &&. You still have to nest some if-statements regardless. Not using && only makes you add more nesting levels (maybe). Do the requirements specify that you need more than one level of nesting?
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you're right. I got an idea in my head and ran with it. It seems to me that, as an exercise, it would be trivial to sort three numbers with a bunch of else-if's loaded with &&'s. An assignment that would get you to stretch your logical abilities though, would be to implement it using only nesting and no &&'s. So, I surmised that that's what this was about, possibly wrongly.
 
Hanna Roberts
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I ended up having this as my code:
]


it worked out well for my assignment. thanks for all the suggestions.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hanna Roberts wrote:. . . it worked out well . . .

Well done

I would have used the ?: operator myself. It is easy enough to find the largest and smallest of three, which I did on JShell, but ran out of time and energy enough to work out the middle value.

jshell> int max(int i, int j, int k)
  ...> {
  ...>    return i > j ?
  ...>           i > k ? i : k :
  ...>           j > k ? j : k;
  ...> }
|  created method max(int,int,int)

jshell> max(123, 234, 345)
$5 ==> 345

jshell> max(123, 345, 234)
$6 ==> 345

jshell> max(345, 234 ,123)
$7 ==> 345

jshell> int min(int i, int j, int k)
  ...> {
  ...>    return i < j ?
  ...>           i < k ? i : k :
  ...>           j < k ? j : k ;
  ...> }
|  created method min(int,int,int)

jshell> min(345, 234 ,123)
$9 ==> 123

jshell> min(345, 123, 234)
$10 ==> 123

jshell> min(123, 345, 234)
$11 ==> 123

Remember that ?: is equivalent to an if‑else squashed into a single expression, and ?: expressions can be nested inside one another. Did you have a stipulation that the three values be different? A Scanner will accept an int with nextDouble() and convert an input like 123 to 123.0.
 
Hanna Roberts
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Hanna Roberts wrote:. . . it worked out well . . .

Well done

I would have used the ?: operator myself. It is easy enough to find the largest and smallest of three, which I did on JShell, but ran out of time and energy enough to work out the middle value.

jshell> int max(int i, int j, int k)
  ...> {
  ...>    return i > j ?
  ...>           i > k ? i : k :
  ...>           j > k ? j : k;
  ...> }
|  created method max(int,int,int)

jshell> max(123, 234, 345)
$5 ==> 345

jshell> max(123, 345, 234)
$6 ==> 345

jshell> max(345, 234 ,123)
$7 ==> 345

jshell> int min(int i, int j, int k)
  ...> {
  ...>    return i < j ?
  ...>           i < k ? i : k :
  ...>           j < k ? j : k ;
  ...> }
|  created method min(int,int,int)

jshell> min(345, 234 ,123)
$9 ==> 123

jshell> min(345, 123, 234)
$10 ==> 123

jshell> min(123, 345, 234)
$11 ==> 123

Remember that ?: is equivalent to an if‑else squashed into a single expression, and ?: expressions can be nested inside one another. Did you have a stipulation that the three values be different? A Scanner will accept an int with nextDouble() and convert an input like 123 to 123.0.



Thanks and oh I don't think I've learned that operator yet so I probably shouldn't use it yet.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're trying to learn about nested if-statements, you wouldn't be using the ternary operator anyway.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that's why you didn't tell me off about nested ?:
 
Hanna Roberts
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:If you're trying to learn about nested if-statements, you wouldn't be using the ternary operator anyway.



oh ok
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hanna Roberts wrote:. . . Thanks . . .

That's a pleasure

I don't think I've learned that operator yet . . . .

Apart from the fact that expressions with multiple ?: can be quite illegible, I think many beginners are scared of ?: and ought to try it so they can see how it works, and how useful it can be. Stick to only one ?: per statement at first.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case && would not be allowed i would asume this could do as well?

 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Koen Ursem wrote:In the case && would not be allowed i would asume this could do as well?


That looks like it could be right. However, this illustrates why it's more difficult to understand the logic if you try to avoid using &&: it's a lot harder to follow the line of deductive reasoning because it becomes very fragmented because you have to make many more jumps across separate if-statements. By using &&, you can start with the three main cases:

1. if x is the smallest (or largest)
2. else if y is the smallest (or largest)
3. otherwise z must be the smallest (or largest)

Under each main case, you deal with the cases that deduce which of the other two are the middle and largest (or smallest). In the structure you show, this kind of logical progression is not as coherently structured.
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The structure I would have preferred
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:The structure I would have preferred



Let me annotate this code to demonstrate what I mean about how fragmented the line of deductive reasoning becomes with this structure. When reasoning through this code, my eyes have to constantly go up and down, back and forth from the current check to previously established facts to keep the logic together and coherent in my head. The cognitive weight is *huge*. I definitely would not regard this structure as something that is easy to analyze and digest. To me the reasoning is very scattered and not systematically deductive.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By contrast, using && appropriately allows you to make for a very clear and clean deductive line of reasoning:

The difference here is that with each if-statement, a fact is clearly established whereas without the &&, the initial if-statements have only partially eliminated possibilities and only the succeeding checks can establish certainty. The conditions that establish certainty are spread across multiple if-statements.
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm of the school of thought that code is for people to read and understand. Spending 5 minutes vs 2 minutes reading code far outweighs having the computer do 5 comparisons vs 2 comparisons. Every. Single. Time.
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, based on the original subject description for this topic: is the exercise all about deducing the logic needed to achieve the sorting outcome using only nested if's.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Judging from OP's code in the original post, it doesn't appear that using && was not allowed. But I'm done beating this dead horse now...
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!