Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone simplify this method?/

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can anyone simplify this methid please?/



Thanks.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
start again and get your curly braces to match
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was asked to me in an Interview and I freaked out and answered something. I don't rem tho what I put in. Just wanted to know how you guys would solve it.

Thanks.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
solve what?

look at this block

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Dunn:
solve what?

look at this block



Yes I got confused there too. Guess the answer would be like we can not simplify the method and state the thing you said above as the reason.

Thanks.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. Got posted twice.
[ December 26, 2008: Message edited by: Arjun Reddy ]
 
Steve Fahlbusch
Bartender
Posts: 605
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code inside the method can be reduced to:

 
Juva Yuva
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Fahlbusch:
The code inside the method can be reduced to:



To be more precise



 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Juva Yuva:
To be more precise



To be even more precise:

No need for the else here. I even set up my Eclipse to warn me if I use unnecessary else blocks.
[ December 26, 2008: Message edited by: Rob Prime ]
 
Juva Yuva
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
To be even more precise:

No need for the else here. I even set up my Eclipse to warn me if I use unnecessary else blocks.



Yeah . But If the code inside the if statement is not "return" statement then if , else if makes difference
 
Campbell Ritchie
Sheriff
Posts: 48923
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob is correct to omit the "else" after a return.

I am sure Rob has got it right; you can reduce that to a single return statement, something like this:

return fruminous && !uffish && manxome ? BANDERSNATCH : JUBJUB_BIRD;
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it's
 
Steve Fahlbusch
Bartender
Posts: 605
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would concur, but then again if this was for an interview i would shy away from the ? : as many folks don't like using this construct.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell:

return fruminous && !uffish && manxome ? BANDERSNATCH : JUBJUB_BIRD;

I think the solution provided by Ritchie will fail for the case:
fruminous = TRUE && uffish = FALSE && manxome = FALSE
as it would give JUBJUB_BIRD whereas it should give BANDERSNATCH.

I agree with Rob's solution. That's cool Rob. Please tell me. How did you get the solution? How should we approach such questions? :roll: After looking at your solution it seems to be correct. But really i had no clue when i first looked at the question.
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Steve]: i would concur, but then again if this was for an interview i would shy away from the ? : as many folks don't like using this construct.

Whereas many others people prefer it; that can go either way. For an interview I would offer both solutions, and make clear that I was willing to code in either style if there was a local style guide to follow. Left to my own devices though, I'd definitely favor the ternary operator.
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Harvinder]: I agree with Rob's solution. That's cool Rob. Please tell me. How did you get the solution? How should we approach such questions? After looking at your solution it seems to be correct. But really i had no clue when i first looked at the question.

Obviously I'm not Rob, but one way to approach this would be to notice that since there are only three boolean inputs, there are only eight possible combinations of values. Make a table and see what happens for each value combination:

Once you complete this, it may well be possible to see a simple pattern in the results which is easy to describe with a formula like what Rob gave.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike for the prompt answer. Earlier i had thought that it was some trick question. But after some time i did end up following the approach suggested by you. I wonder if Rob did it that way or not?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did but with a little different layout I learned during an electro course in University. It uses some form of matrix, but it's kinda hard to explain without the book that I learned it from.

The only advantage that layout adds is the ease of grouping "cells" together. For instance, if every case where frumious is false yields true, it's very easy to see.

It gets hard at 5 or more booleans though.
 
Campbell Ritchie
Sheriff
Posts: 48923
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just having a quick look at the code; I obviously should have done it formally.
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob: I assume you're thinking of a Karnaugh map (or alternately a Veitch diagram, or KV map). Those were fun.
[ December 26, 2008: Message edited by: Mike Simmons ]
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those were the ones yes. Funny how the name gets completely forgotten but the technique stays, even after 10 years.

Jeez, I started university 10 years ago already... I'm getting old...
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Past your Prime?
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob need a slight clarification.
In this code there is a condition :

annuling it with your suggestion


In this case , taking manxome value as false it should return JUBJUB_BIRD
But with your suggestion it will return : BANDERSNATCH
Have you noticed it.
Regards.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But have you checked the values for frumious and uffish?

At that point you already know that frumious is true (the entire first if statement returns JUBJUB_BIRD). You also know that either uffish is true or manxone is true (second if statement).

Given that manxone is false (otherwise you won't get inside that if statement), uffish must be true. And therefore !frumious || uffish yields true and JUBJUB_BIRD is returned.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Thinking Rob & now I've understood.
Thanks & keep going
My Best Regards!
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rob and Mark once again for your valuable inputs
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you mean Mike? I know Mark V has a lot of useful posts, but not in this thread
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOps...it is Mike indeed...Thanks again for correcting me
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic