Java > Logic-1 > alarmClock (CodingBat Solution)

Problem:

Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, return a string of the form "7:00" indicating when the alarm clock should ring. Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".

alarmClock(1, false) → "7:00"
alarmClock(5, false) → "7:00"
alarmClock(0, false) → "10:00"


Solution:

public String alarmClock(int day, boolean vacation) {
  if (vacation) {
    if(day > 0 && day != 6)
      return "10:00";
    else
      return "off";
  } else if (day > 0 && day != 6)
      return "7:00";
    else
      return "10:00";
    
}

12 comments:

  1. public String alarmClock(int day, boolean vacation) {
    boolean weekday = day < 6 && day > 0;
    if (weekday && !vacation)
    return "7:00";
    else if ((weekday && vacation) || (!weekday && !vacation))
    return "10:00";
    return "off";
    }

    ReplyDelete
    Replies
    1. but after running its not showing me output , main method is not there

      Delete
  2. return (vacation ? (day == 0 || day == 6 || day == 7 ? "off" : "10:00") :
    (day >= 1 && day <= 5 ? "7:00" : "10:00"));

    ReplyDelete
  3. These clocks are shaped in the form of grenades and they have extremely loud alarm sounds. kids alarm clock

    ReplyDelete
  4. //Imdad Momin
    public String alarmClock(int day, boolean vacation) {
    if(vacation != true) {
    return((day>=1 && day<=5)? "7:00" : "10:00" );
    }
    return((day>=1 && day<=5)? "10:00" : "off" );
    }

    ReplyDelete
  5. public String alarmClock(int day, boolean isVacation) {
    final boolean isWeekend = (day == 0) != (day == 6);
    if (isVacation && isWeekend){
    return "off";
    }
    return (isVacation != isWeekend)? "10:00": "7:00";
    }

    ReplyDelete
  6. public String alarmClock(int day, boolean vacation) {
    if((day>0 && day<6) && !vacation){
    return new String("7:00");
    }else if((day>0 && day<6) && vacation){
    return new String("10:00");
    }if(!(day>0 && day<6) && !vacation){
    return new String("10:00");
    }
    return new String("off");

    }

    ReplyDelete
  7. boolean weekday = day>0&&day<6;
    if(weekday&&!vacation) return "7:00";
    else if((weekday&&vacation)||(!weekday&&!vacation)) return "10:00";
    else return "off";

    ReplyDelete
    Replies
    1. boolean weekday = day>0&&day<6;
      if(weekday&&!vacation) return "7:00";
      else if((weekday&&vacation)||(!weekday&&!vacation)) return "10:00";
      else return "off";

      Delete
  8. public String alarmClock(int day, boolean vacation) {
    if(!vacation) {
    if(day >=1 && day <=5) {
    return "7:00";
    }
    }else if(day == 0 || day == 6){
    return "off";
    }

    return "10:00";
    }

    ReplyDelete
  9. public String alarmClock(int day, boolean vacation) {
    if(vacation){
    return day==0 || day==6 ? "off" : "10:00";
    }
    else{
    return day==0 || day==6 ? "10:00" : "7:00";
    }
    }

    ReplyDelete
  10. public String alarmClock(int day, boolean vacation) {
    if(!vacation){
    if(day > 0 && day <6){
    return "7:00";
    } else return "10:00";
    } else if(day > 0 && day <6){
    return "10:00";
    } else return "off";
    }

    ReplyDelete