Click here to Skip to main content
15,867,762 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This is the coding 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.

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 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/output:

* shouldWakeUp (true, 1); → should return true

* shouldWakeUp (false, 2); → should return false since the dog is not barking.

* shouldWakeUp (true, 8); → should return false, since it's not before 8.

* shouldWakeUp (true, -1); → should return false since the hourOfDay parameter needs to be in a range 0-23.


TIP: Use the if else statement with multiple conditions.

NOTE: The shouldWakeUp method  needs to be defined as public static like we have been doing so far in the course.

NOTE: Do not add a  main method to solution code.


MY CODE:

public class BarkingDog {

    public static boolean ShouldWakeup(boolean barking, int hourOfDay) {


        if (barking) {
            int time = 24;

        } else if (hourOfDay > 8 || hourOfDay < 22) {
            return true;

        } else if (hourOfDay < 0 || hourOfDay > 23) {
            return false;

        }
        return false;

    }

}


What I have tried:

It's a coding challenge on Udemy. I submitted my solution but I keep getting an error and its saying that I'm missing a curly brace. Can't figure out what I am doing wrong. I DO NOT want the answer, I want to learn how to solve it. Can someone please help me out, would really appreciate the help.
Posted
Updated 28-Apr-21 16:42pm
Comments
wseng 8-Jul-19 22:46pm    
I don't see any errors from the code you posted.
Patrice T 8-Jul-19 22:51pm    
it tells you the position too.
and problem may not be at error position.
Member 14524731 8-Jul-19 23:02pm    
Patrice, what do you mean by a position?
Dave Kreskowiak 9-Jul-19 0:06am    
Line number and column in the file.
Member 14524731 9-Jul-19 10:48am    
Line 22. When I take the curly brace off, it shows an error and then I can not compile the code.

1 solution

Errors do not always show up exactly where they are indicated by the compiler - that is where there compiler detected the problem, not where it was caused.
In this case, there are two curly braces in a set: '{' and '}' - if you have an open brace, then the absence of the matching close brace will be detected at the end of the file, not at the location of the "spurious" open brace.

Since that isn't your complete code, I'd guess that your whole file contains more: at the very least a small set of using statements, and most likely a namespace line:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace MyNamespace{

    public class BarkingDog {
    
        public static boolean ShouldWakeup(boolean barking, int hourOfDay) {
    
    
            if (barking) {
                int time = 24;
    
            } else if (hourOfDay > 8 || hourOfDay < 22) {
                return true;
    
            } else if (hourOfDay < 0 || hourOfDay > 23) {
                return false;
    
            }
            return false;
    
        }
    
    }
}  <<<--- If this one is missing, you will get the error.

So start by looking at your whole code, and try putting the cursor on the last '}' in the file. Then press CTRL+} which will move the cursor to the matching '{'. Is there anythign above that?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900