Coding Bat – Cluster 3

Today, while wrapping-up our conversation over arrays, we worked a series of 3 different basic array problems from Coding Bat.

CODING BAT – CIGARPARTY

This exercise requires that the program return true if number of cigars is between 40 and 60 on weekdays and at least 40 on weekends.

Here is the code that you start with:

public boolean cigarParty(int cigars, boolean isWeekend) {

}

We start with a single integer named cigars and a boolean named isWeekend.

public boolean cigarParty(int cigars, boolean isWeekend) {
  if (isWeekend){
    if (cigars >= 40){
      return true;
    }else{
      return false;
    }
  }else{
    if (cigars >=40 && cigars <=60){
      return true;
    }else{
      return false;
    }
  }
}

The solution above starts with a Boolean conditional to check to see if it is a weekend (line 2). If it is not a weekend, then the code jumps to line 8. However, if it is a weekend, the code continues on to line 3.

Line 3 looks to see if the number of cigars is at least 40. If it is, then a TRUE is returned, otherwise a FALSE is returned.

If it is not a weekend, the code jumped to line 8. Starting at line 9, we look to see if the number of cigars is between 40 and 60. If it is, then a TRUE is returned, otherwise a FALSE is returned.

CODING BAT – DATEFASHION

This exercise requires that the program calculate the probability of you and your date getting a table at a restaurant based upon how stylish the clothes you and your date are wearing.

Here is the code that you start with:

public int dateFashion(int you, int date) {

}

We start with two integer variables named “you” and “date”.

public int dateFashion(int you, int date) {
  if (you <= 2 || date <= 2){
    return 0;
  }else if(you >= 8 || date >= 8){
    return 2;
  }else{
    return 1;
  }
}

Our first Boolean conditional statement is located on line 2 where we check to see if either “you” or your “date” are wearing anything that is a 2 or lower. If so, then a “0” is returned.

If the first Boolean is FALSE, then we move to the second Boolean conditional statement on line 4 which is checking to see if either “you” or your “date” are wearing anything that is an 8 or higher. If so, then a “2” is returned.

Finally, if second Boolean is also FALSE, then we automatically return a “1” as the numbers for “you” and your “date” are between 2 and 8 exclusive of those numbers.

CODING BAT – SQUIRRELPLAY

This exercise requires that the program state whether the squirrels will come out to play. The squirrels like to play when the temperature is between 60 and 90 degrees, unless it is summer. In the summer, they will stay out up to 100 degrees.

Here is the code that you start with:

public boolean squirrelPlay(int temp, boolean isSummer) {

}

We start with one integer variable named “temp” and a boolean variable named isSummer.

public boolean squirrelPlay(int temp, boolean isSummer) {
  if (isSummer){
    if (temp >= 60 && temp <= 100){
      return true;
    }else{
      return false;
    }
  }else{
    if (temp >= 60 && temp <= 90){
      return true;
    }else{
      return false;
    }
  }
}

The solution above starts with a Boolean conditional to check to see if it is summer (line 2). If it is not summer, then the code jumps to line 8. However, if it is summer, the code continues on to line 3.

Line 3 looks to see if the temperature is between 60 and 100 degrees. If it is, then a TRUE is returned, otherwise a FALSE is returned.

If it is not summer, the code jumped to line 8. Starting at line 9, we look to see if the temperature is between 60 and 90 degrees. If it is, then a TRUE is returned, otherwise a FALSE is returned.

CODING BAT – ALARMCLOCK

This exercise requires that the program return what time the alarm clock with sound with the given criteria:

  • If it is a vacation weekday, then the alarm will sound at 10:00.
  • If it is a vacation weekend, then the alarm is off.
  • If it a weekday, then the alarm will sound at 7:00.
  • If it is a weekend, then the alarm will sound at 10:00.

Here is the code that you start with:

public String alarmClock(int day, boolean vacation) {

}

As you can see, we have an integer variable named “day” and a Boolean variable named “vacation”.

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

Staring at line 2, we have a Boolean conditional which looks to see if it is a vacation. If it is not a vacation day, then the code skips to line 8. If it is a vacation day, then we proceed to line 3 where we look to see if it is a weekday (days 1 through 5). If it is a weekday, the application will return the string “10:00” otherwise, the application returns the string “off”.

Looking at line 9, we are processing for a non-vacation day and look to see if it is a weekday (days 1 through 5) and then return the string “7:00” if it is a weekday. If it is a weekend, then we return the string “10:00”.

Leave a Reply

Your email address will not be published. Required fields are marked *