Skip to content

Instantly share code, notes, and snippets.

@0ryant
Created August 1, 2019 09:40
Show Gist options
  • Save 0ryant/ba8a845221d8b69b6d20750d882732d9 to your computer and use it in GitHub Desktop.
Save 0ryant/ba8a845221d8b69b6d20750d882732d9 to your computer and use it in GitHub Desktop.
Java - Coding Challenge 3 - Barking dog
public class BarkingDog {
public static boolean shouldWakeUp(boolean barking,int hourOfDay){
if (hourOfDay <0 || hourOfDay >23){
return false;
}
if (barking == true && hourOfDay <8 || hourOfDay >22){
return true;
}else{
return false;
}
}
}
@RaynNaj
Copy link

RaynNaj commented Jul 24, 2021

package com.rayn.project;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("What time it is: - ");
        int hourOfDay = sc.nextInt();

        System.out.println("Is the dog barking? - ");
        boolean barking = sc.nextBoolean();

	    boolean response = shouldWakeUp(barking, hourOfDay);
        System.out.println("Have to wake up ? " +
                "Ans= "+ response);


    }
    public static boolean shouldWakeUp(boolean barking, int hourOfDay){

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

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

```This is my shared code. 

@travelonether
Copy link

All the different code layout examples are great to see to improve. Thanks for the share.

@ilialloyd
Copy link

ilialloyd commented Aug 30, 2022

public class BarkingDog {
    public static boolean shouldWakeUp(boolean barking , int hourOfDay){
       if (hourOfDay >= 0 && hourOfDay <= 23) {        // to check right hour number
            if (hourOfDay <= 7 || hourOfDay >= 23) {       // to check barking hours
                return barking;      //flag to true if its during bark hours
            }
        }else{
            return false;       //out barking hours entry flag false
        }
        return false;          // flag false if invalid hour 
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment