Coding Bat – or35

This exercise requires that the program return the value of true if the given number is evenly divisible by either 3 or 5. The instructions encourage us to utilize modulus division to solve this problem.

As a reminder, modulus division is the “remainder” after you solve a division problem.

Here is the code that you start with:

public boolean or35(int n) {
 
}

As you can see, we have a single integer variable named “n”.

public boolean or35(int n) {
  return (n % 3 == 0) || (n % 5 == 0);
}

The provided solution does not utilize variables beyond the single provided n.

public boolean or35(int n) {
 double modFive = n % 5;
 double modThree = n % 3;
 if (modFive == 0 || modThree == 0){
 return true;
 }
 return false;
}

An alternative solution could use variables for the division and inside the conditional statements.

Coding Bat – Cluster 1

Today, while I was working with the FTC 11242 (ERROR 404) robotics team at Ferris Intermediate School for our community outreach efforts, my Computer Science class was assigned to complete 4 different Coding Bat assignments.

Coding Bat – helloName

This exercise requires that the program return an entered name with the word Hello appended to the front and an exclamation point following the name.

Here is the code that you start with:

public String helloName(String name) {
  
}

We start with a string variable named name.

public String helloName(String name) {
  return "Hello " + name + "!";
}

The solution above simply uses the concatenate function to append “Hello ” in front of the name variable and the “!” following it.

Coding Bat – makeAbba

This exercise requires that the program take two input strings of text and place them into the A-B-B-A order.

For example, let’s say that string A is “cat” and string B is “dog”. The output would be catdogdogcat.

Here is the code that you start with:

public String makeAbba(String a, String b) {
  
}

We start with two string variables named a and b.

public String makeAbba(String a, String b) {
  return a + b + b + a;
}

The solution above simply uses the concatenate function to append variable a to variable b to variable b to variable a.

Coding Bat – makeTags

This exercise requires that the program take two entered strings of which the first is an HTML tag and create a formatted HTML instruction with open and closing tags enclosing the second entered string.

Here is the code that you start with:

public String makeTags(String tag, String word) {
  
}

We start with two string variables named tag and word.

public String makeTags(String tag, String word) {
  return "<" + tag + ">" + word + "</" + tag + ">";
}

The solution above simply uses the concatenate function to append the correct text and variables in an HTML syntax structure.

Coding Bat – withoutX

This exercise requires that the program take an entered string that if it start with or ends with “x” the “x” is removed. If there is an “x” in the middle of the letter, it remains.

Here is the code that you start with:

public String withoutX(String str) {
 
}

As you can see, we have a string variable named str.

public String withoutX(String str) {
 int count = str.length();
 if (count == 0){
 return str;
 }
 int secondToLastLetter = count - 1;
 String firstLetter = str.substring(0, 1);
 String lastLetter = str.substring(secondToLastLetter, count);
 if (str.equals("x")){
 str = "";
 }
 else if (firstLetter.equals("x") && (lastLetter.equals("x"))){
 str = str.substring(1,secondToLastLetter);
 }
 else if (firstLetter.equals("x")){
 str = str.substring(1,count);
 }
 else if (lastLetter.equals("x")){
 str = str.substring(0,secondToLastLetter);
 }
 return str;
}

You can see that this problem was little bit more involved because the testing data set included an empty string and a string that was just a single letter x.

Lines 3 – 5 address the situation of a null or empty string.

Lines 6 – 8 create local variables to be used in the other cases presented.

Lines 9 – 11 address the situation of only the letter x.

Lines 12 – 14 address the situation of an x at the front AND an x at the end.

Lines 15 – 17 address the situation of an x only at the front.

Lines 18 – 20 address the situation of an x only at the end.

Line 21 returns what the string will look like with the necessary letter x’s removed.

Coding Bat – in3050

This exercise requires that the program return the value of true if both of two numbers entered are between 30 and 40 (inclusive) or between 40 and 50 inclusive.

Here is the code that you start with:

public boolean in3050(int a, int b) {
  
}

As you can see, we have two integer variables named “a” and “b”.

public boolean in3050(int a, int b) {
  if (a >= 30 && a <= 40 && b >= 30 && b <= 40) {
    return true;
  }
  if (a >= 40 && a <= 50 && b >= 40 && b <= 50) {
    return true;
  }
  return false;
}

In the solution provided, we have a Boolean that returns true if A is any of the following values:

30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40

AND if B is any of the following values:

30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40

We have a second conditional statement that returns true if A is any of the following values:

40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50

AND if B is any of the following values:

40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50

Otherwise, a false is returned.

An alternate solution could also be:

public boolean in3050(int a, int b) {
  if ((a >= 30 && a <= 40 && b >= 30 && b <= 40) || (a >= 40 && a <= 50 && b >= 40 && b <= 50)) {
    return true;
  }
  return false;
}

Here, we have combined the else if portion of the original solution into a BOOLEAN or statement.

Coding Bat – loneTeen

This exercise requires that the program return the value of true if either of two numbers entered is a teen (13 through 19). If both are teens, the program is to return the value of false.

Here is the code that you start with:

public boolean loneTeen(int a, int b) {

}

As you can see, we have two integer variables named “a” and “b”.

public boolean loneTeen(int a, int b) {
 boolean aNotTeen = (a < 13 || a > 19);
 boolean bNotTeen = (b < 13 || b > 19);
 
 return (aNotTeen && !bNotTeen) || (!aNotTeen && bNotTeen); 
}

In the solution, we first created a boolean named “aNotTeen” which returns a value of true if the value of integer “a” is less than 13 OR greater than 19.

We continued by creating a second boolean named “bNotTeen” which returns a value of true if the value of integer “b” is less than 13 OR greater than 19.

Finally, we have the application return the boolean of aNotTeen AND NOT bNotTeen OR the boolean of NOT aNotTeen AND bNotTeen.

Let’s look at what happens if “a” is 12 and “b” is 17:

  • aNotTeen = TRUE
  • bNotTeen = FALSE

So, in the return we have (TRUE && NOT FALSE) || (NOT TRUE && FALSE). As TRUE and NOT FALSE are one and the same and NOT TRUE and FALSE are one and the same, this returns as TRUE.

Let’s look at what happens if “a” is 13 and “b” is 17:

  • aNotTeen = FALSE
  • bNotTeen = FALSE

On the return, we get (FALSE && NOT FALSE) || (NOT FALSE && FALSE). As FALSE and NOT FALSE are opposites, this returns as FALSE.

Coding Bat provides a slightly different solution which is built around whether the integer is a teen.

public boolean loneTeen(int a, int b) {
  boolean aTeen = (a >= 13 && a <= 19);
  boolean bTeen = (b >= 13 && b <= 19);
  
  return (aTeen && !bTeen) || (!aTeen && bTeen);
}

As you can see, their solution is just as elegant as the solution provided above, but just tackles the problem in a different manner.

Using the examples from above of a = 12 and b = 17:

  • aTeen = FALSE
  • bTeen = TRUE

On the return, we get (FALSE && NOT TRUE) OR (NOT FALSE and TRUE).

As both of those are the same, it returns as TRUE. You’ll notice that it works backwards from my return since my boolean is working opposite to their solution.

Coding Bat – icyHot

This exercise requires that the program return the value of true if one of two temperatures entered is less than 0 and the other is greater than 100.

Here is the code that you start with:

public boolean icyHot(int temp1, int temp2) {
 
}

As you can see, we have two integer variables named “temp1” and “temp2”.

public boolean icyHot(int temp1, int temp2) {
 return ((temp1 < 0 && temp2 > 100) || (temp1 > 100 && temp2 < 0));
}

In the solution, we return a boolean for “temp1” is less than 0 AND “temp2” greater than 100 OR “temp1” is greater than 100 AND “temp2” is less than 0.

Let’s look at the following test data:

  • temp1 = -5
  • temp2 = 105

On the return, we have (TRUE && TRUE) OR (FALSE && FALSE). This will return as TRUE.

Let’s now look at another set of test data:

  • temp1 = 5
  • temp2 = 105

On the return, we have (FALSE && TRUE) OR (FALSE && FALSE). This will return as FALSE.

Finally, let’s look the this final set of test data:

  • temp1 = 5
  • temp2 = 95

On the return, we have (FALSE && FALSE) OR (FALSE && FALSE). This will return as FALSE.

Coding Bat – mixStart

This exercise requires that the program return the value of true if what has been entered is a 3-letter word with the 2nd and 3rd letters being “ix”.

Here is the code that you start with:

public boolean mixStart(String str) {
  
}

As you can see, we have a String variable named “str”.

public boolean mixStart(String str) {
  if (str.length() < 3) return false;
  String two = str.substring(1, 3);
  if (two.equals("ix")) {
    return true;
  } else {
    return false;
  }
}

In the solution, first check the length of the statement using the length function of the String class. If the length is less than 3 characters long, the program stops running and returns “false”.

Assuming that the value of “str” is at least 3 characters long we move to using the substring function of the String class to create the value to be assigned to variable “two”. The substring starts at index 1 and stops at index 3. This means that the variable “two” will only have the 2nd and 3rd letters of the variable “str”.

We now begin the analysis of the variable “two”. If it is “ix” then the program returns true. However, if it is NOT “ix” then the program returns false.

You will notice that the comparison is being made using the following:

if (two.equals("ix")) {

Some have asked if we could use something similar to the following:

if (two == "ix") {

While this looks like this would work in comparing a string variable to an existing string, we must use the equals function of the String class to determine if two strings are equivalent.

Traditional boolean operators such as == and != do not work when comparing strings.

Coding Bat – parrotTrouble

This exercise requires that the program return the value of true if you are in “trouble” and a value of false if you are not in “trouble”. You are in trouble if the parrots are talking before 07:00am or after 08:00pm.

Here is the code that you start with:

public boolean parrotTrouble(boolean talking, int hour) {
  
}

As you can see, we have a boolean named talking and an integer named hour.

public boolean parrotTrouble(boolean talking, int hour) {
  return (talking && (hour < 7 || hour > 20));
}

The solution uses a nested boolean to see if the time is before 7 OR after 20. It is simultaneously looking to see if the parrots are talking.

We are using an AND to bridge these two boolean statements together because if the parrots are NOT talking, we can’t be in trouble regardless of the time of day.

Coding Bat – sumDouble

This exercise requires that the program return the sum of two provided numbers. If the two provided numbers are the same, the sum is to be doubled.

Here is the code that you start with:

public int sumDouble(int a, int b) {
  
}

As you can see, we have two integers (a and b) that have been declared.

public int sumDouble(int a, int b) {
  int sum = a + b;
  if (a == b) {
    sum = sum * 2;
  } 
  return sum;
}

In this solution, we have created an integer variable named “sum” which has the value of “a” added to “b”.

We then move to a conditional statement. If “a” equals “b”, the variable “sum” is multiplied by 2. We only enter this instruction if “a” and “b” are equal to each other.

Following either entry of the IF statement or bypassing it, we display the value of the variable “sum”.

Coding Bat – sleepIn

This exercise requires that the program return “true” if it is OK to sleep in on a given day. The rules are that you can sleep-in on a day that is not a weekday or on a vacation day.

Here is the code that you start with:

public boolean sleepIn(boolean weekday, boolean vacation) {
 
}

As you can see, we have a boolean named weekday and a boolean named vacation.

public boolean sleepIn(boolean weekday, boolean vacation) {
  if (!weekday || vacation) {
    return true;
  }  
  return false;
}

The solution uses a boolean conditional of if the value is NOT a weekday OR a vacation, it will return true, otherwise it will return false.