isFlobbyBird
public boolean isFlobbyBird(boolean red, boolean spotted){
if (red && spotted) {
return true;
} else {
return false;
}
}
This is fine but the if
statement is superfluous.
red && spotted
is already a boolean
value. Just return it.
public boolean isFlobbyBird (boolean a, boolean b){
return a && b;
}
This also works and is nice and concise.
However the variable names are not very meaningful.
I can’t read the line return a && b;
and get
any sense of what it means to be a Flobby bird.
public boolean isFlobbyBird(boolean red, boolean spotted) {
return red && spotted;
}
Lovely! Concise and meaningful.
isBloggyBird
public boolean isBloggyBird(boolean red, boolean spotted) {
return red == true && spotted == false;
}
Don’t compare to boolean literals.
red == true
(red == true) == true
((red == true) == true) == true
public boolean isBloggyBird(boolean red, boolean spotted) {
return red && !spotted == true;
}
Good job using !
but then gave into temptation to
compare to a literal.
public boolean isBloggyBird(boolean red, boolean spotted) {
return red && !spotted;
}
eatsWorms
public boolean eatsWorms(boolean red, boolean spotted){
return (red && spotted) || (red && !spotted) || (!red && spotted);
}
This is basically combining the three expressions that identify the birds that do eat worms.
Which is perfectly logical.
(r && s) || (r && !s) || (!r && s)
(r && s || r && !s) || !r && s
(r && (s || !s)) || !r && s
(r && true) || !r && s
r || !r && s
r || s
public boolean eatsWorms(boolean red, boolean spotted){
return red || spotted;
}
Can also get here by looking at the descriptions.
All the red birds eat worms.
All the spotted birds eat worms.
Therefore if a bird is red or spotted it eats worms.
public boolean eatsWorms(boolean red, boolean spotted) {
return !isGlobbyBird(red, spotted);
}
public boolean eatsWorms(boolean red, boolean spotted){
return !(!red && !spotted);
}
This person wrote isGlobbyBird
as:
public boolean isGlobbyBird(boolean red, boolean spotted){
return !red && !spotted;
}
So they clearly negated that expression.
!(!red && !spotted)
!!red || !!spotted
red || spotted
isRed
public boolean isRed(String birdie) {
if (birdie.equals("Flobby") || birdie.equals("Bloggy")) {
return true;
} else {
return false;
}
}
What’s could be improved?
public static boolean isRed(String name) {
return name.equals("Flobby") || name.equals("Bloggy");
}
public static boolean isRed(String name) {
return name.equals("Flobby" || "Bloggy");
}
equals
?
If you forgot equals
you could build it from parts.
public boolean isRed(String bird) {
return bird.indexOf("Flobby") >= 0 || bird.indexOf("Bloggy") >= 0;
}
This depends on the fact that the argument is guaranteed to be one of just four strings.
Which is fine in this case.