Code Challenge #4: Barking Dog Method with Java

Code Challenge #4: Barking Dog Method with Java

Learning new programming languages is fun, here's my go at Java!

Welcome back!

I'm currently learning Java, so this exercise is from the course I'm taking. This one is simple, but I thought it was worth writing about since I'm learning a new language!

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 shouldWakeUp that has 2 parameters:

  • barking of type boolean, representing whether our dog is currently barking
  • hourOfDay of type int and has a valid range of 0-23

We have to wake up if the dog is barking before 8 or after 22 hours so in that case return true.

In all other cases return false.

If the hourOfDay parameter is less than 0 or greater than 23 return false.

Examples of input and output

public class BarkingDog {
    public static void main(String[] args) {
        System.out.println(shouldWakeUp(true, 1));
        // returns true since the dog is barking during sleeping hours

        System.out.println(shouldWakeUp(false, 2));
        // returns false since the dog is not barking

        System.out.println(shouldWakeUp(true, 8));
        // returns false, since it's not before 8

        System.out.println(shouldWakeUp(true, -1));
        // returns false since the hourOfDay parameter needs to be in a range of 0-23
    }

    // shouldWakeUp method
}

STOP READING if you want to try this on your own!

My Solution

public class BarkingDog {
    public static void main(String[] args) {
        System.out.println(shouldWakeUp(true, 1)); // returns true
        System.out.println(shouldWakeUp(false, 2)); // returns false
        System.out.println(shouldWakeUp(true, 8)); // returns false
        System.out.println(shouldWakeUp(true, -1)); // returns false
    }

    public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
        boolean sleepingHours = (hourOfDay >= 0 && hourOfDay < 8) || hourOfDay == 23;

        return barking && sleepingHours;
    }
}

Sleeping Hours

This was tricky.

I was going to use multiple if-else statements but I thought it was simpler to make a boolean variable called sleepingHours.

The sleeping hours are from >= 11:00 PM (23) and < 8:00 AM (8). You just have to keep in mind that the hour resets to 0 after the 23rd hour with military time.

The variable sleepingHours holds the boolean value of the expression:

boolean sleepingHours = (hourOfDay >= 0 && hourOfDay < 8) || hourOfDay == 23;

The logical OR operator || evaluates whether hourOfDay is either:

  • greater than or equal to 0 AND less than 8
  • equal to 23.

sleepingHours will be true if one of those conditions is met.

The expression also validates whether hourOfDay is within 0-23 because it will return false if it isn't.

Return Statement

return barking && sleepingHours;

The return statement is a boolean expression with barking and sleepingHours. The method shouldWakeUp will return true only if both are true, otherwise it will return false.

It's Your Turn

I hope you had fun with this!

Here are some questions for you:

  • Did you do this differently?
  • Are you learning any new languages?
  • Which programming language is your favorite?

Let me know in the comments!