Tabs Don’t Mean Spit – at Least in JAVA

You would think I could remember this, but I can’t. Tabs don’t mean spit – at least in JAVA!

My UIL Computer Science team was working on the following question recently:

What is the output of the following code?

a) north
b) northKorea
c) china
d) south
e) There is no output due to an error

String count="Korea";
boolean host=true;
if(count.equalsIgnoreCase("KoReA")) {
    if(host)
      out.print("north");
    else
      out.print("south");
  }
  else
    out.print("china");
    out.print(count);

The answer key shows the correct answer as being “B”. When you compile the program, you find that the answer is in-fact “B”.

We had originally reasoned that the answer would be “A” since the else condition that starts online 11 is never met. We interpreted that lines 12 and 13 would run as part of that else statement.

I have been working with Python programming in my Computer Science 1 class and anyone who has worked with Python knows that tabs are the end-all of everything.

For reference, here is what the program looks like with all of the conditionals enclosed in curly braces and tabs adjusted for readability…

class Main {
  public static void main(String[] args) {
    String count="Korea";
    boolean host=true;
    if(count.equalsIgnoreCase("KoReA")) {
      if(host){
        System.out.print("north");
      }else{
        System.out.print("south");
      }
     }else{
       System.out.print("china");
    }
    System.out.print(count);
  }
}

As you can see below, you get the same solution with the code in this configuration:

It was my mistake for not catching it, but it’s a lesson learned. As you can see, line 14 is NOT part of the else statement that starts on line 11.

Lesson Plans – Week of 2018-04-01

Lesson Plans – Week of 2018-04-01

  • (1 A/B) Principles of Applied Engineering
  • (2A) Computer Science 1
  • (2B) Computer Science 2
  • (3A) Robotics 1 / Robotics 2 / FTC 11242
  • (3B) Robotics 1 / Robotics 2 / FTC 12645
  • (4A) Computer Science 1
  • (5 A/B) Principles of Applied Engineering

Eric Evans – Planboard Week – 2018-04-01

Lesson Plans – Week of 2018-03-25

Lesson Plans – Week of 2018-03-25

  • (1 A/B) Principles of Applied Engineering
  • (2A) Computer Science 1
  • (2B) Computer Science 2
  • (3A) Robotics 1 / Robotics 2 / FTC 11242
  • (3B) Robotics 1 / Robotics 2 / FTC 12645
  • (4A) Computer Science 1
  • (5 A/B) Principles of Applied Engineering

Eric Evans – Planboard Week – 2018-03-25

Lesson Plans – Week of 2018-03-18

Lesson Plans – Week of 2018-03-18

  • (1 A/B) Principles of Applied Engineering
  • (2A) Computer Science 1
  • (2B) Computer Science 2
  • (3A) Robotics 1 / Robotics 2 / FTC 11242
  • (3B) Robotics 1 / Robotics 2 / FTC 12645
  • (4A) Computer Science 1
  • (5 A/B) Principles of Applied Engineering

Eric Evans – Planboard Week – 2018-03-18

Q3 Grade Distribution 2017/2018

Today, we are closing the 3Q grading period for the 2017/2018 school year. I have a total of 134 students with 50 of them enrolled in either Robotics 1 or Robotics 2.

Through my career, I have seen many students slip with their grades during this grading cycle. Regretfully, this trend has not abated much this year.

Robotics 1, Robotics 2, and Computer Science 2 have 100% of students passing. But the more curriculum-based courses have failures.

As you can see, Computer Science 1 has an overall failure rate of 13.33% (4 of 30 students). 75% of those failures come from one class period as a result of failure to complete a major multi-part project involving 2-dimensional arrays in Python.

The vast majority of the class (73%) received an A. The majority of those (63%) came from a single class period.

All-in-all, I am happy with the performance of both sections as we had some difficult concepts to cover during this grading period.

As you can see, Principles of Applied Engineering has an overall failure rate of 4.26% (2 of 47 students). The failures are evenly distributed between both sections of this course.

One section comprised 60% of the A’s, 42% of the B’s and 55% of the C’s. The other section was 40%, 58%, and 45% respectively.

This grading period, we covered mechanical, materials, and electrical engineering. Many students struggled with mechanical engineering and their grades reflected those challenges.

Coding Bat Recursive Problems – Day 1

Today, we are looking at a primer for recursion using problems in CodingBat in Computer Science 2. We looked at 3 different problems from CodingBat.

Bunny Ears

http://codingbat.com/prob/p183649

Here is the setup of the problem:

We have a number of bunnies and each bunny has two big floppy ears. We want to compute the total number of ears across all the bunnies recursively (without loops or multiplication).

public int bunnyEars(int bunnies) {
  if (bunnies == 0){
    return 0;
  } else {
    return 2 + bunnyEars(bunnies-1);
  }
}

Let’s use an input of 6. With this, we would anticipate the output to be 12, since each bunny has 2 ears.

Line 3 will not be run since the condition set in line 2 is FALSE. Currently, bunnies is set to 6.

So, we move on to line 4, which is the start of the default case, and is executed on line 5. So with bunnies set to 6, we return a 2 and then add that to the number of bunny ears from the next bunny (bunnies – 1), which is bunny #5.

Now, bunnies = 5, so line 2 is still FALSE, so we run the default case on line 6. We return a 2 and add that to the number of bunny ears from the next bunny (bunnies – 1), which is bunny #4.

Now, bunnies = 4, so line 2 is still FALSE, so we run the default case on line 6. We return a 2 and add that to the number of bunny ears from the next bunny (bunnies – 1), which is bunny #3.

Now, bunnies = 3, so line 2 is still FALSE, so we run the default case on line 6. We return a 2 and add that to the number of bunny ears from the next bunny (bunnies – 1), which is bunny #2.

Now, bunnies = 2, so line 2 is still FALSE, so we run the default case on line 6. We return a 2 and add that to the number of bunny ears from the next bunny (bunnies – 1), which is bunny #1.

Now, bunnies = 1, so line 2 is still FALSE, so we run the default case on line 6. We return a 2 and add that to the number of bunny ears from the next bunny (bunnies – 1), which is 0.

Now, bunnies = 0, so line 2 is now TRUE, so we return a 0 and exit the loop.

We now add the following together:

  • 0 from when bunnies = 0 (cumulative total = 0)
  • 2 from when bunnies = 1 (cumulative total = 2)
  • 2 from when bunnies = 2 (cumulative total = 4)
  • 2 from when bunnies = 3 (cumulative total = 6)
  • 2 from when bunnies = 4 (cumulative total = 8)
  • 2 from when bunnies = 5 (cumulative total = 10)
  • 2 from when bunnies = 6 (cumulative total = 12)

The total is returned as 12 ears.

Bunny Ears 2

http://codingbat.com/prob/p107330

Here is the setup of the problem:

We have bunnies standing in a line, numbered 1, 2, … The odd bunnies (1, 3, ..) have the normal 2 ears. The even bunnies (2, 4, ..) we’ll say have 3 ears, because they each have a raised foot. Recursively return the number of “ears” in the bunny line 1, 2, … n (without loops or multiplication).

public int bunnyEars2(int bunnies) {
  if (bunnies == 0){
    return 0;
  } else if (bunnies % 2 != 0){
    return 2 + bunnyEars2(bunnies-1);
  } else{
    return 3 + bunnyEars2(bunnies-1);
  }
}

For this one, let’s use an input of 5. We would expect the output to be 12 since bunnies 1, 3, & 5 have a total 6 ears and bunnies 2 & 4 have a total of 6 ears.

Line 3 will not be run since the condition set in line 2 is FALSE. Currently, bunnies is set to 5.

So, we move on to line 4. This condition is looking to see if we are working with an even numbered or odd numbered bunny. We have set the condition to use the modulus of 2 of the integer of bunnies. If the modulus is NOT 0, then the statement is TRUE and therefore it is an ODD numbered bunny.

Line 6 is the start of the default case which is used if we are working with an EVEN numbered bunny.

Now, bunnies = 5, so line 2 is FALSE, and line 4 is TRUE. We return a 2 and add that to the number of bunny ears from the next bunny (bunnies – 1), which is bunny #4.

Now, bunnies = 4, so line 2 is FALSE, line 4 is FALSE, so we run the default case at line 6. We return a 3 and add that to the number of bunny ears from the next bunny (bunnies -1), which is bunny #3.

Now, bunnies = 3, so line 2 is FALSE, and line 4 is TRUE. We return a 2 and add that to the number of bunny ears from the next bunny (bunnies – 1), which is bunny #2.

Now, bunnies = 2, so line 2 is FALSE, line 4 is FALSE, so we run the default case at line 6. We return a 3 and add that to the number of bunny ears from the next bunny (bunnies -1), which is bunny #1.

Now, bunnies = 1, so line 2 is FALSE, and line 4 is TRUE. We return a 2 and add that to the number of bunny ears from the next bunny (bunnies – 1), which is 0.

Now, bunnies = 0, so line 2 is now TRUE, so we return a 0 and exit the loop.

We now add the following together:

  • 0 from when bunnies = 0 (cumulative total = 0)
  • 2 from when bunnies = 1 (cumulative total = 2)
  • 3 from when bunnies = 2 (cumulative total = 5)
  • 2 from when bunnies = 3 (cumulative total = 2)
  • 3 from when bunnies = 4 (cumulative total = 10)
  • 2 from when bunnies = 5 (cumulative total = 12)

The total is returned as 12 ears.

Triangle

http://codingbat.com/prob/p194781

Here is the setup of the problem:

We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows.

public int triangle(int rows) {
 if (rows == 0){
   return 0;
 } else {
   return rows + triangle(rows - 1);
 }
}

Here, we are building something that looks like this:

Like the cases before it, line 2 is a condition looking for when we are to row 0. If line 2 is FALSE, we will move to line 4. For this example, let’s use rows = 4. With this, the output should be 10 as there are 10 cubes needed to complete a 4 row triangle.

So, we start with rows = 4. Line 2 is FALSE, so we move to line 4 to execute line 5, where we return the row number and add it to the number of cubes on the next row (rows – 1), which is row #3.

We are returning the row number as that is equal to the number of cubes of each row (e.g. row 7 = 7 cubes, row 4 = 4 cubes, etc…).

We now move to rows = 3. Line 2 if FALSE, so we move to line 4 to execute line 5, where we return the row number and add it to the number of cubes on the next row (rows – 1), which is row #2.

We now move to rows = 2. Line 2 if FALSE, so we move to line 4 to execute line 5, where we return the row number and add it to the number of cubes on the next row (rows – 1), which is row #1.

We now move to rows = 1. Line 2 if FALSE, so we move to line 4 to execute line 5, where we return the row number and add it to the number of cubes on the next row (rows – 1), which is 0.

Now, rows = 0, so line 2 is now TRUE, so we return a 0 and exit the loop.

We now add the following together:

  • 0 from when rows = 0 (cumulative total = 0)
  • 1 from when rows = 1 (cumulative total = 1)
  • 2 from when rows = 2 (cumulative total = 3)
  • 3 from when rows = 3 (cumulative total = 6)
  • 4 from when rows = 4 (cumulative total = 10)

Lesson Plans – Week of 2018-03-04

Lesson Plans – Week of 2018-03-04

  • (1 A/B) Principles of Applied Engineering
  • (2A) Computer Science 1
  • (2B) Computer Science 2
  • (3A) Robotics 1 / Robotics 2 / FTC 11242
  • (3B) Robotics 1 / Robotics 2 / FTC 12645
  • (4A) Computer Science 1
  • (5 A/B) Principles of Applied Engineering

Eric Evans – Planboard Week – 2018-03-04