First Summer 2004

Lab Notes Six: Nested loops, other loops, loops and a half, scalable letters.
Let's practice a bit with for loops. Can you print the numbers from 0 to 9?

Easy:
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	for (int i = 0; i < 10; i++) {
	    System.out.println(i); 
	} 
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
0
1
2
3
4
5
6
7
8
9
frilled.cs.indiana.edu%
Well, what if you want to print the numbers on the same line?

Just use print in the loop,
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	for (int i = 0; i < 10; i++) {
	    System.out.print(i); 
	} 
	System.out.println(); 
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
0123456789
frilled.cs.indiana.edu%
... and one (empty) println outside of it:

Very good. But don't you want to space them out a bit?

OK, I will print each number in parentheses.
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	for (int i = 0; i < 10; i++) {
	    System.out.print(" (" + i + ")"); 
	} 
	System.out.println(); 
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
 (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
frilled.cs.indiana.edu%
Looks good. Can you write 10 such lines?

Let me first highlight the code that prints a line.
class One {
    public static void main(String[] args) {
	for (int i = 0; i < 10; i++) {
	    System.out.print(" (" + i + ")"); 
	} 
	System.out.println(); 
    } 
}
That's the part that you have in blue.

Exactly. Now let's do that 10 times. Use a for loop, with a different index, j.

Why j, when I can call it line? Calling it line would be just fine with me.
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	for (int line = 0; line < 10; line++) {
	    for (int i = 0; i < 10; i++) {
		System.out.print(" (" + i + ")");
	    }
	    System.out.println();
	}
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
 (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
 (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
 (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
 (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
 (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
 (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
 (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
 (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
 (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
 (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)
frilled.cs.indiana.edu%
Although that doesn't influence in the very least the way the program actually works.

Looks good, doesn't it? Relax. I marked two of the (4)'s in your output with red and blue. Can you tell me what the difference is between them?

They appear on different lines. Indeed, for the first one line is 0 (zero), while for the second one line has a value of 3 (three).

Let me change the output to include information about this second dimension. Good idea.
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	for (int line = 0; line < 10; line++) {
	    for (int i = 0; i < 10; i++) {
		System.out.print(" (" + line + ", " + i + ")");
	    }
	    System.out.println();
	}
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
 (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (0, 6) (0, 7) (0, 8) (0, 9)
 (1, 0) (1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7) (1, 8) (1, 9)
 (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (2, 7) (2, 8) (2, 9)
 (3, 0) (3, 1) (3, 2) (3, 3) (3, 4) (3, 5) (3, 6) (3, 7) (3, 8) (3, 9)
 (4, 0) (4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6) (4, 7) (4, 8) (4, 9)
 (5, 0) (5, 1) (5, 2) (5, 3) (5, 4) (5, 5) (5, 6) (5, 7) (5, 8) (5, 9)
 (6, 0) (6, 1) (6, 2) (6, 3) (6, 4) (6, 5) (6, 6) (6, 7) (6, 8) (6, 9)
 (7, 0) (7, 1) (7, 2) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7) (7, 8) (7, 9)
 (8, 0) (8, 1) (8, 2) (8, 3) (8, 4) (8, 5) (8, 6) (8, 7) (8, 8) (8, 9)
 (9, 0) (9, 1) (9, 2) (9, 3) (9, 4) (9, 5) (9, 6) (9, 7) (9, 8) (9, 9)
frilled.cs.indiana.edu%

It was an easy change. I see, the part in blue is new.

Now I have 100 cells in the output, and I have a name for each one of them. Yes, line and i, as a pair of numbers.

Might as well rename i as something more meaningful. Such as column.

And let's ask the user to specify the size of the square (number of lines and columns). Use ConsoleReader for that.
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
	System.out.print("What size? "); 
	int size = c.readInt(); 
	for (int line = 0; line < size; line++) {
	    for (int column = 0; column < size; column++) {
		System.out.print(" (" + line + ", " + column + ")");
	    }
	    System.out.println();
	}
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
What size? 4
 (0, 0) (0, 1) (0, 2) (0, 3)
 (1, 0) (1, 1) (1, 2) (1, 3)
 (2, 0) (2, 1) (2, 2) (2, 3)
 (3, 0) (3, 1) (3, 2) (3, 3)
frilled.cs.indiana.edu%java One
What size? 6
 (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5)
 (1, 0) (1, 1) (1, 2) (1, 3) (1, 4) (1, 5)
 (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (2, 5)
 (3, 0) (3, 1) (3, 2) (3, 3) (3, 4) (3, 5)
 (4, 0) (4, 1) (4, 2) (4, 3) (4, 4) (4, 5)
 (5, 0) (5, 1) (5, 2) (5, 3) (5, 4) (5, 5)
frilled.cs.indiana.edu%

Looks good, doesn't it? Relax. What's new is in red and blue, isn't it?

Yes. It looks really good. I have 6 characters for each cell, and everything looks nice and tidy. Can you highlight the second column?

You mean the set of cells for which column has a value of 1 (one)? You got it.
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
	System.out.print("What size? "); 
	int size = c.readInt(); 
	for (int line = 0; line < size; line++) {
	    for (int column = 0; column < size; column++) {
		if (column == 1) {
		    System.out.print("   **  "); 
		} else { 
		    System.out.print(" (" + line + ", " + column + ")");
		}
	    }
	    System.out.println();
	}
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
What size? 6
 (0, 0)   **   (0, 2) (0, 3) (0, 4) (0, 5)
 (1, 0)   **   (1, 2) (1, 3) (1, 4) (1, 5)
 (2, 0)   **   (2, 2) (2, 3) (2, 4) (2, 5)
 (3, 0)   **   (3, 2) (3, 3) (3, 4) (3, 5)
 (4, 0)   **   (4, 2) (4, 3) (4, 4) (4, 5)
 (5, 0)   **   (5, 2) (5, 3) (5, 4) (5, 5)
frilled.cs.indiana.edu%

I can do the first diagonal now. I know, the change is minor.
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
	System.out.print("What size? "); 
	int size = c.readInt(); 
	for (int line = 0; line < size; line++) {
	    for (int column = 0; column < size; column++) {
		if (column == line) {
		    System.out.print("   **  "); 
		} else { 
		    System.out.print(" (" + line + ", " + column + ")");
		}
	    }
	    System.out.println();
	}
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
What size? 6
   **   (0, 1) (0, 2) (0, 3) (0, 4) (0, 5)
 (1, 0)   **   (1, 2) (1, 3) (1, 4) (1, 5)
 (2, 0) (2, 1)   **   (2, 3) (2, 4) (2, 5)
 (3, 0) (3, 1) (3, 2)   **   (3, 4) (3, 5)
 (4, 0) (4, 1) (4, 2) (4, 3)   **   (4, 5)
 (5, 0) (5, 1) (5, 2) (5, 3) (5, 4)   **  
frilled.cs.indiana.edu%

What if you want to see both the last column and first diagonal? I don't know, you tell me.

Well, here's what I think: I go through all the cells anyway. Exactly.

I check their names. And if you can tell by their name

... that they belong to either the first diagonal or to the last column, ... you turn them on.

That's it. This would turn on all of the cells that appear on the first diagonal and all of those that appear on the last column. It's just a union of sets.

Easy for you to say that, but here's the program:
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
	System.out.print("What size? "); 
	int size = c.readInt(); 
	for (int line = 0; line < size; line++) {
	    for (int column = 0; column < size; column++) {
		if (column == line || column == (size - 1)) {
		    System.out.print("   **  "); 
		} else { 
		    System.out.print(" (" + line + ", " + column + ")");
		}
	    }
	    System.out.println();
	}
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
What size? 6
   **   (0, 1) (0, 2) (0, 3) (0, 4)   **  
 (1, 0)   **   (1, 2) (1, 3) (1, 4)   **  
 (2, 0) (2, 1)   **   (2, 3) (2, 4)   **  
 (3, 0) (3, 1) (3, 2)   **   (3, 4)   **  
 (4, 0) (4, 1) (4, 2) (4, 3)   **     **  
 (5, 0) (5, 1) (5, 2) (5, 3) (5, 4)   **  
frilled.cs.indiana.edu%
Looks good.

And I was the first to say that. How can we make this look more like a square?

Maybe change the output a bit. How about this:
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
	System.out.print("What size? "); 
	int size = c.readInt(); 
	for (int line = 0; line < size; line++) {
	    for (int column = 0; column < size; column++) {
		if (column == line || column == (size - 1)) {
		    System.out.print("* "); 
		} else { 
		    System.out.print("  ");
		}
	    }
	    System.out.println();
	}
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
What size? 8
*             * 
  *           * 
    *         * 
      *       * 
        *     * 
          *   * 
            * * 
              * 
frilled.cs.indiana.edu%
Did you catch that?
You bet...

Now the names are only implicit. Only in our program's mind.

We can draw patterns, scalable patterns. Here's code for a Z:
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
	System.out.print("What size? "); 
	int size = c.readInt(); 
	for (int line = 0; line < size; line++) {
	    for (int column = 0; column < size; column++) {
		if (column == (size - 1 - line) ||
                    line   == 0                 || 
                    line   == (size - 1))
                {
		    System.out.print("* "); 
		} else { 
		    System.out.print("  ");
		}
	    }
	    System.out.println();
	}
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
What size? 7
* * * * * * * 
          *   
        *     
      *       
    *         
  *           
* * * * * * * 
frilled.cs.indiana.edu%java One
What size? 4
* * * * 
    *   
  *     
* * * * 
frilled.cs.indiana.edu%

Can you draw a circle? A circle?

Sure, why not? Of course:
frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
	System.out.print("What size? "); 
	int size = c.readInt(); 
	for (int line = 0; line < size; line++) {
	    for (int column = 0; column < size; column++) {
		if (
 		      Math.abs( (line   - size / 2) * (line   - size / 2) +
 		                (column - size / 2) * (column - size / 2) - 
  		                (size -1) * (size - 1) / 4
			      ) <= (0.15 * size) 

                   ) 
                {
		    System.out.print("* "); 
		} else { 
		    System.out.print("  ");
		}
	    }
	    System.out.println();
	}
    } 
} 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
What size? 20
                                        
              *           *             
          *                   *         
                                        
                                        
    *                               *   
                                        
  *                                   * 
                                        
                                        
                                        
                                        
                                        
  *                                   * 
                                        
    *                               *   
                                        
                                        
          *                   *         
              *           *             
frilled.cs.indiana.edu%
Hey, now wait a minute!
I hope you understand approximations.

And the equation of a circle. If you don't, don't worry...

Don't worry, be happy! Well, then it's time to get ready for...

A201/A597 LAB ASSIGNMENT SIX

Here's the grid:
public class Patterns {
  public static void main(String[] args) {
    ConsoleReader console = new ConsoleReader(System.in); 
    System.out.print("Enter the size: "); 
    int size = console.readInt(), i, j;
    for (i = 0; i < size; i++) {
      for (j = 0; j < size; j++) {
        if (false) {
          System.out.print("* ");
        } else {
          System.out.print("  ");
        }
      }
      System.out.println();
    }
  }     
}


Now, let's look at all the patterns.

Pattern 1:

Pattern 2:

(j == 0)
(i == 0)


Pattern 3:

Pattern 4:

(j == (size - 1))
(i == (size - 1))

Pattern 5:

Pattern 6:

((j == 0) && (i <= size/2))
((j == 0) && (i > size/2))

If you see that (in places) we're off by 1 (one), ... then you're on the right track.

But keep in mind we are interested here in the overall understanding of inequalities that determine (or define) the patterns, ... so we trade (where appropriate) absolute accuracy for a shorter (simpler, yet still reasonably exact) formula.

Pattern 7:

Pattern 8:

((i == 0) && (j <= size/2))
((i == 0) && (j > size/2))

Pattern 9: ((j == (size - 1)) && (i <= size/2))


Pattern 10: ((j == (size - 1) && (i > size/2))


Pattern 11: ((i == (size - 1)) && (j < size/2))


Pattern 12: ((i == (size - 1)) && (j > size/2)


Pattern 13:

Pattern 14:

(j == size/2)
(i == size/2)

Pattern 15:

Pattern 16:

15: ((j == size/2) && (i <= size/2))
16: ((j == size/2) && (i > size/2))


Pattern 17:

Pattern 18:

17: ((i == size/2) && (j <= size/2))
18: ((i == size/2) && (j > size/2))


Pattern 19:

Pattern 20:

(i == j)
(i + j == (size - 1))

Pattern 21:

Pattern 22:

21: ((i == j) && (i <= size/2) && (j <= size/2))
22: ((i == j) && (i > size/2) && (j > size/2))

Can either one of these conditions be simplified at all?


Pattern 23:

Pattern 24:

23: ((i + j == (size - 1)) && (i <= size/2) && (j > size/2))
24: ((i + j == (size - 1)) && (i > size/2) && (j <= size/2))

Can either one of these be simplified?


If we have these "atomic" patterns (described above), how we can combine them to obtain more complicated patterns (such as the ones illustrated below):

Uppercase T:
A cell should be turned ON if it appears in the group described by pattern 13 OR if it appears in the group that is described by pattern 2, otherwise the cell is OFF (blank)
Uppercase L:
A cell should be turned ON if it appears in the group described by pattern 13 OR if it appears in the group described by pattern 12
Uppercase E:
If cell is in pattern 1 OR in pattern 2 OR in pattern 4 or in pattern 17 then the cell should be turned ON otherwise leave the cell blank (print a space)
Uppercase W:
Cell to be turned ON if pattern 1 matches OR if pattern 24 matches OR if pattern 22 matches OR if pattern 3 matches, otherwise leave cell blank
Uppercase A:
(20) OR (3) OR (18)
Uppercase R:
(22) || (1) || (14) || (2) || (9)


Your suggested task is to write a program that produces a scalable 4 (four):

If you can do that you're in good shape (and you can try a K).

Perhaps you can even try scalable words like: "ONE".

Have fun.


Last updated: May 16, 2004 by Adrian German for A201