for
|
|
|
|
|
|
Observation: for (...)
is more
general than while(...)
.
No. 66
Printing numbers
Q: |
Write code for printing the first 10 numbers. The expected output is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Keep the value of 10 flexible i.e. easy to change. |
A: |
A simple approach is:
This may be enhanced becoming more flexible:
|
No. 67
Printing just even numbers
Q: |
Consider:
This code effectively filters odd numbers and prints only even ones: 0 2 4 6 8 10 12 Modify this code:
|
A: |
Rather than filtering unwanted values we use a
The original code could have been modified in this fashion as well:
|
|
(1|1) (1|2) (1|3) (2|1) (2|2) (2|3) |
|
1 2 3 3 4 5 4 5 6 7 5 6 7 8 9 |
// What do i and j actually represent? for (int i = 0; i < 6; i++) { for (int j = 0; j < i; j++) { System.out.print(i + j + " "); } System.out.println(); } |
// Improved code comprehension. for (int row = 0; row < 6; row++) { for (int column = 0; column < row; column++) { System.out.print( row + column + " "); } System.out.println(); } |
No. 68
Merry Xmas
Q: |
Write an application to print an ASCII art Xmas tree.
Provide a configurable parameter X * *** ***** ******* ********* *********** ### ### Tip
|
|||||||||||||||||||||||||||
A: |
We start coding the proposed helper exercise. Each line
has to be indented using space
A complete solution is available at the MI Gitlab repository. We start dissecting the problem in a version being fully covered by our current knowledge. Our aim is printing: X Part 1: Tree's top. Row index 0 * Part 2: Tree's body Row index 1 *** Row index 2 ***** Row index 3 ******* Row index 4 ********* Row index 5 *********** ### Part 3: Bottom trunk lines. ### We require the precise indentation values when
e.g. starting the tree's body. The following
sketch shows two trees of different sizes representing invisible
spaces by “␣”. In
the »bigger« tree's first line we need e.g. 5
spaces before actually printing the tree's very top
A tree with A tree with 6 body rows 2 row groups ␣␣␣␣␣X ␣␣␣X ␣␣␣␣␣* ␣␣␣* ␣␣␣␣*** ␣␣*** ␣␣␣***** ␣***** ␣␣******* ******* ␣********* ␣␣### *********** ␣␣␣␣### ␣␣␣␣### The precise amounts of these indentations depend on the
tree's size. Printing larger trees requires larger indentation
values. The tree's size is being controlled by the parameter
We start printing the tree's top. This requires
Next we focus on the tree's body requiring
We finally print the tree's two trunk lines both requiring
an indent of
So far quite an amount of energy has been invested into
printing fixed numbers of space (
Moreover starting with Java™ 11 the
Combining both methods completely obsoletes all “inner” loops thereby considerably enhancing our code's readability. You'll see a re-implementation in XmasUsingFormat.java:
Both implementation variants allow for setting e.g. X * *** ***** ******* ********* *********** ************* *************** ***************** ******************* ### ### |
No. 69
More fun with Xmas trees
Q: |
The following ASCII art for configurable Xmas tree sizes is slightly more challenging : \ / -->*<-- /_\ /_\_\ /_/_/_\ /_\_\_\ /_/_/_/_\ /_\_\_\_\ /_/_/_/_/_\ /_\_\_\_\_\ /_/_/_/_/_/_\ /_\_\_\_\_\_\ /_/_/_/_/_/_/_\ [___] Tip
|
||||||||||||||||||||||||
A: |
Looping through \ / Part 1: The tree's top. groups of 2 lines -->*<-- /_\ Row group index 0 /_\_\ Part 2: The tree's body, printing /_/_/_\ two lines per group index Row group index 1 /_\_\_\ loop iteration. /_/_/_/_\ Row group index 2 /_\_\_\_\ /_/_/_/_/_\ Row group index 3 /_\_\_\_\_\ /_/_/_/_/_/_\ Row group index 4 /_\_\_\_\_\_\ End of tree's body /_/_/_/_/_/_/_\ [___] Part 3: Bottom trunk line. For each row group (albeit having different length) the
two patterns A tree with A tree with 5 row groups 2 row groups ␣␣␣␣␣␣\ / ␣␣␣\ / ␣␣␣␣-->*<-- ␣-->*<-- ␣␣␣␣␣␣/_\ ␣␣␣/_\ ␣␣␣␣␣/_\_\ ␣␣/_\_\ ␣␣␣␣/_/_/_\ ␣/_/_/_\ ␣␣␣␣/_\_\_\ ␣/_\_\_\ ␣␣␣/_/_/_/_\ /_/_/_/_\ ␣␣␣/_\_\_\_\ ␣␣[___] ␣␣/_/_/_/_/_\ ␣␣/_\_\_\_\_\ ␣/_/_/_/_/_/_\ ␣/_\_\_\_\_\_\ /_/_/_/_/_/_/_\ ␣␣␣␣␣[___] The precise amounts of these indentations again depend on
the tree's size. Printing larger trees requires larger
indentation values. The tree's size is being controlled by the
parameter
Now we start printing the tree's top
The above snippet shows a solution: Within a given string
the backslash character must itself be escaped by a second one
to get a single backslash character on output. In addition we
need preceding spaces ( Printing our tree's very top thus requires:
The expression
We now turn to the tree's body. Following the idea of row groups each consisting of two lines we require:
We now show the loop's gory details:
Finally printing the tree's trunk:
Similar to the previous exercise Merry Xmas this time we replace all inner
loops by
Both implementation variants allow for setting e.g. \ / -->*<-- /_\ /_\_\ /_/_/_\ /_\_\_\ /_/_/_/_\ /_\_\_\_\ /_/_/_/_/_\ /_\_\_\_\_\ /_/_/_/_/_/_\ /_\_\_\_\_\_\ /_/_/_/_/_/_/_\ /_\_\_\_\_\_\_\ /_/_/_/_/_/_/_/_\ /_\_\_\_\_\_\_\_\ /_/_/_/_/_/_/_/_/_\ /_\_\_\_\_\_\_\_\_\ /_/_/_/_/_/_/_/_/_/_\ [___] |