Created
August 1, 2019 09:40
-
-
Save 0ryant/ba8a845221d8b69b6d20750d882732d9 to your computer and use it in GitHub Desktop.
Java - Coding Challenge 3 - Barking dog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
`package com.company;
public class Main {
public static void main(String[] args)
{
boolean response = shouldWakeUp(false, 4);
System.out.println(response);
}
public static boolean shouldWakeUp(boolean barking, int hourOfDay)
{
if (hourOfDay >= 1 && hourOfDay <= 23)
{
if ((barking == true) && (hourOfDay < 8 || hourOfDay > 22))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
`
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.
All the different code layout examples are great to see to improve. Thanks for the share.
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
wow! this is great, it