Using easy assignments to introduce deep concepts

In this early part of the semester, the students don't know all that much. We haven't covered that many language constructs let along algorithms, development techniques and all that other good stuff.

In terms of the "Java" part of APCS we've talked about basic types, Strings, basic classes, loops and conditionals. We're limited in the tools we have but we can still get the classes thinking in the right way.

Last week, when we introduced loops we spent some time drawing shapes.

Starting with things like

Rectangle:
*****
*****
*****

and

Triangle1
*
**
***
****
*****

Simple nested loop stuff.

String s = "";
for (int r = 0; r < height; r++) {
  for (int c=0; c<=r; c++ ){
    s=s+"*";"
  }
  s=s+"\n";
}
return s;

Things get interesting when we looked at the next one:

Triangle2:
    *
   **
  ***
 ****
*****

It seems like a simple little problem with a simple solution:

What I love about it is that you can look at it in a couple of interesting ways.

First, you can look at those spaces as a triangle in and of themselves (replaced with # below):

####*
###**
##***
#****
*****

Well, that's almost the same as the Triangle1 from above. Also, if you take away that triangle of spaces, you're pretty much left with Triangle1.

The structure of the code can look something like:

String s="";
for (int r = 0; r < height; r++){
  // loop over the number of spaces we need for this line

  // loop over the number of stars we need for this line

  s=s+"\n";
}
return s;

We can also look at the problem just as a series of lines. How many spaces and how many stars:

Let's say height = 5

| row | spaces | stars |
|

Comments

Comments powered by Disqus



Enter your email address:

Delivered by FeedBurner

Google Analytics Alternative