A table of square numbers
No. 69
A basic square number table
Q: |
Write an application for creating the following table of integer square numbers ( ): n | n * n --+------ 0 | 0 1 | 1 2 | 4 3 | 9 4 | 16 5 | 25 6 | 36 7 | 49 8 | 64 9 | 81 Your solution is expected to be flexible: Sometimes a different limit of e.g. 12 rather than 9 in the above example is being desired. This rules an obvious «solution»:
TipUse a loop printing the table's body values. |
A: |
The table head my be copied from the code snippet. In addition we require a loop printing the table's body:
|
No. 70
Tidy up the mess!
Q: |
Running the previous code for values beyond 9 suffers from ugly formatting: n | n * n --+------ 0 | 0 1 | 1 ... 8 | 64 9 | 81 10 | 100 11 | 121 12 | 144 ... 19 | 361 20 | 400 Modify your code by:
Your application's output shall look like: n | n * n ---+------ 0| 0 1| 1 ... 8| 64 9| 81 10| 100 11| 121 ... 19| 361 20| 400 TipRead the track Formatting
Numeric Print Output and learn how to style numeric
output: Use |
||||
A: |
The $...d format specifier serves two purposes:
Example:
The format string
|
No. 71
HTML-ify me
Q: |
Modify the previous code to generate HTML output instead of pure text and watch the result using a web browser. The expected output reads:
|
A: |
A complete solution is being provided by App.java. This solution uses both Java™ 15 multi line blocks and the String.indent(..) method. |
No. 72
Auxiliary Example, part 1: A multiplication table
Q: |
In order to prepare a more sophisticated square number table we supply a slightly different exercise producing a multiplication table: * | 1 2 3 4 5 6 7 8 9 10 ---+-------------------------------------------------- 1| 1 2 3 4 5 6 7 8 9 10 2| 2 4 6 8 10 12 14 16 18 20 3| 3 6 9 12 15 18 21 24 27 30 4| 4 8 12 16 20 24 28 32 36 40 5| 5 10 15 20 25 30 35 40 45 50 6| 6 12 18 24 30 36 42 48 54 60 7| 7 14 21 28 35 42 49 56 63 70 8| 8 16 24 32 40 48 56 64 72 80 9| 9 18 27 36 45 54 63 72 81 90 10| 10 20 30 40 50 60 70 80 90 100 The number of rows and columns are equal. Provide an
appropriate parameter i.e Tip
|
||||
A: |
|
No. 73
Auxiliary Example, part 2: Avoiding redundant entries
Q: |
It does not make sense to supply both results like e.g. 3 * 4 and 4 * 3. Modify your application to generate: 1| 1 2| 2 4 3| 3 6 9 4| 4 8 12 16 5| 5 10 15 20 25 6| 6 12 18 24 30 36 7| 7 14 21 28 35 42 49 8| 8 16 24 32 40 48 56 64 9| 9 18 27 36 45 54 63 72 81 10| 10 20 30 40 50 60 70 80 90 100 ---+-------------------------------------------------- * | 1 2 3 4 5 6 7 8 9 10 |
A: |
We need two changes with respect to the previous exercise:
|
No. 74
Creating a “real” square table
Q: |
The last square number table solution is only appropriate for smaller amounts of data. Growing numbers of elements require rearranging values in blocks avoiding waste of space: n | n² n | n² n | n² n | n² n | n² ----+--------------+--------------+--------------+--------------+---------- 0 | 0 20 | 400 40 | 1600 60 | 3600 80 | 6400 1 | 1 21 | 441 41 | 1681 61 | 3721 81 | 6561 2 | 4 22 | 484 42 | 1764 62 | 3844 82 | 6724 3 | 9 23 | 529 43 | 1849 63 | 3969 83 | 6889 4 | 16 24 | 576 44 | 1936 64 | 4096 84 | 7056 5 | 25 25 | 625 45 | 2025 65 | 4225 85 | 7225 6 | 36 26 | 676 46 | 2116 66 | 4356 86 | 7396 7 | 49 27 | 729 47 | 2209 67 | 4489 87 | 7569 8 | 64 28 | 784 48 | 2304 68 | 4624 88 | 7744 9 | 81 29 | 841 49 | 2401 69 | 4761 89 | 7921 ----+--------------+--------------+--------------+--------------+---------- 10 | 100 30 | 900 50 | 2500 70 | 4900 90 | 8100 11 | 121 31 | 961 51 | 2601 71 | 5041 91 | 8281 12 | 144 32 | 1024 52 | 2704 72 | 5184 92 | 8464 13 | 169 33 | 1089 53 | 2809 73 | 5329 93 | 8649 14 | 196 34 | 1156 54 | 2916 74 | 5476 94 | 8836 15 | 225 35 | 1225 55 | 3025 75 | 5625 95 | 9025 16 | 256 36 | 1296 56 | 3136 76 | 5776 96 | 9216 17 | 289 37 | 1369 57 | 3249 77 | 5929 97 | 9409 18 | 324 38 | 1444 58 | 3364 78 | 6084 98 | 9604 19 | 361 39 | 1521 59 | 3481 79 | 6241 99 | 9801 Building a table this way requires additional parameters:
You require three nested loops. |
A: |
|
No. 75
Creating a sophisticated HTML version of your square table
Q: |
On top of the previous square number table we may also create a HTML version.Modify your console output from the previous exercise accordingly. The expected outcome looks like: Tip
|
A: |
The following solution uses CSS style definitions in the generated HTML's header section being referenced from the document's body.
|