Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBenji Beck
648 PointsBarking Dog Troubleshooting in Java
I'm trying to solve this challenge for fun. I've been working on it over the past few days an haven't been making progress and have a few syntax errors that are hiding.
Instructions for the challenge :
We have a dog that likes to bark. We need to wake up if the dog is barking at night!
Write a method bark that has 2 parameters.
1st parameter should be of type boolean and be named barking it represents if our dog is currently barking.
2nd parameter represents the hour of the day and is of type int with the name hourOfDay and has a valid range of 0-23.
We have to wake up if the dog is barking before 8:00 or after 22:00 which would return true.
In all other cases return false. If the hourOfDay parameter is less than 0 or greater than 23 return false.
TIP: Use an if else statement with multiple conditions.
package com.javaprojects;
public class Main {
public static boolean main(String[] args) {
public static boolean bark(boolean barking, int hourOfDay){
boolean status = false;
if (barking) {
if ((hourOfDay < 0) || (hourOfDay > 23)) {
status = false;
} else if ((hourOfDay < 8) || (hourOfDay > 22)) {
status = true;
}
return status;
}
}
}
}
1 Answer
Steve Hunter
57,712 PointsHi Benji,
What platform is this challenge set on - it isn't a Treehouse question that I'm aware of. There's a, pretty much, identical Reddit post about this same problem. Interesting that both snippets make this method static
- why is that?
One thing that I saw at a glance, and I haven't started my IDE, I'm afraid - you are retesting your conditional using else if
. I think that'll ignore the outcome of the first if
condition? So, if hourOfDay
is, say, 32, the first condition will fire but (I think) the second one will too as 32 is greater than 22 - using else if
removes the mutual exclusivity of just using else
(I think!!).
Try changing the else if
to just else
, that way you build in mutual exclusivity, which is what you're expecting it to do.
Let me know how you get on - I'll fire up IntelliJ next time!
Steve.