Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we simplify it any more?

 
krishna bethanabhotla
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5.Simplify the following method

public static int jabberwock(boolean frumious, boolean manxome,
boolean uffish){
if (!frumious){
if (manxome) {
if (uffish){
return JUBJUB_BIRD;
}
else if (!manxome) {
return BANDERSNATCH;
}
else{
return JUBJUB_BIRD;
}
}
else {
return JUBJUB_BIRD;
}
}
else if (!uffish){
if (!manxome){
return BANDERSNATCH;
}
}
else if(!manxome){
return JUBJUB_BIRD;
}
if (!(uffish || !manxome)) {
return BANDERSNATCH;
}
return JUBJUB_BIRD;
}
Answer:
public static int jabberwock(
boolean frumious,
boolean manxome,
boolean uffish) {

if (!frumious && (manxome && uffish)) {
return JUBJUB_BIRD;
} else if (!frumious && (manxome && !manxome)) {
return BANDERSNATCH;
} else if (!frumious && !uffish) {
return JUBJUB_BIRD;
} else if (!uffish && !manxome) {
return BANDERSNATCH;
} else if (!manxome) {
return JUBJUB_BIRD;
}
return JUBJUB_BIRD;
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

haven't looked at the original code, just your answer

get rid of all the "return JUBJUB_BIRD; if blocks" and leave the final as
the default
combine the two return BANDERSNATCH; if blocks
no idea if your code (answer) is accurate

 
krishna bethanabhotla
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael,
That helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic