File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can someone simplify this method?/ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone simplify this method?/" Watch "Can someone simplify this method?/" New topic
Author

Can someone simplify this method?/

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi All,

Can anyone simplify this methid please?/



Thanks.


Be Humble... Be Nice.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
start again and get your curly braces to match
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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

Joined: Jun 09, 2003
Posts: 4632
solve what?

look at this block

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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

Joined: Nov 10, 2007
Posts: 627
Sorry. Got posted twice.
[ December 26, 2008: Message edited by: Arjun Reddy ]
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 562
    
    7

The code inside the method can be reduced to:

Juva Yuva
Greenhorn

Joined: Dec 18, 2008
Posts: 17
Originally posted by Steve Fahlbusch:
The code inside the method can be reduced to:



To be more precise



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Juva Yuva
Greenhorn

Joined: Dec 18, 2008
Posts: 17
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

Joined: Oct 13, 2005
Posts: 39044
    
  23
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

Actually it's
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 562
    
    7

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

Joined: Jun 10, 2008
Posts: 231
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.


thanks
Harvinder
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
[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

Joined: Mar 05, 2008
Posts: 3016
    
  10
[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

Joined: Jun 10, 2008
Posts: 231
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Oct 13, 2005
Posts: 39044
    
  23
I was just having a quick look at the code; I obviously should have done it formally.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Mar 05, 2008
Posts: 3016
    
  10
Past your Prime?
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Oct 06, 2008
Posts: 338
Great Thinking Rob & now I've understood.
Thanks & keep going
My Best Regards!
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
thanks Rob and Mark once again for your valuable inputs
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Don't you mean Mike? I know Mark V has a lot of useful posts, but not in this thread
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
OOps...it is Mike indeed...Thanks again for correcting me
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Thanks for replying guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone simplify this method?/