From c46bc8612a622881691c36c1e6a6f8bcff8953fd Mon Sep 17 00:00:00 2001 From: Martin Goik Date: Sun, 27 Sep 2020 12:28:52 +0200 Subject: [PATCH] Java 15 version of Ascii Xmas tree --- .../Ref/Statements/MoreFunXmasTree/pom.xml | 4 +- .../java/de/hdm_stuttgart/mi/sd1/Xmas.java | 3 +- .../hdm_stuttgart/mi/sd1/XmasUsingFormat.java | 21 ++- Doc/Sd1/statements.xml | 128 ++++++++---------- 4 files changed, 70 insertions(+), 86 deletions(-) diff --git a/Doc/Sd1/Ref/Statements/MoreFunXmasTree/pom.xml b/Doc/Sd1/Ref/Statements/MoreFunXmasTree/pom.xml index af1a514e..6ec7b1c9 100644 --- a/Doc/Sd1/Ref/Statements/MoreFunXmasTree/pom.xml +++ b/Doc/Sd1/Ref/Statements/MoreFunXmasTree/pom.xml @@ -25,8 +25,8 @@ maven-compiler-plugin 3.8.1 - 11 - 11 + 15 + 15 diff --git a/Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/Xmas.java b/Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/Xmas.java index b8da2388..b55f3185 100644 --- a/Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/Xmas.java +++ b/Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/Xmas.java @@ -1,7 +1,8 @@ package de.hdm_stuttgart.mi.sd1; /** - * Beginner's way of implementing the »More fun with X-mas trees« exercise. + * Beginner's way of implementing the »More fun with X-mas trees« exercise. This solution just uses + * »bare-bone« print statements. */ public class Xmas { diff --git a/Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/XmasUsingFormat.java b/Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/XmasUsingFormat.java index de2222ce..62dddd3d 100644 --- a/Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/XmasUsingFormat.java +++ b/Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/XmasUsingFormat.java @@ -2,7 +2,7 @@ package de.hdm_stuttgart.mi.sd1; /** - * Simplified implementation replacing loops by {@link java.io.PrintStream#format(String, Object...)} and + * Simplified implementation replacing loops by {@link String#indent(int)} and * {@link String#repeat(int)}. */ public class XmasUsingFormat { @@ -28,25 +28,24 @@ public class XmasUsingFormat { final int numberOfRowGroups = 5; // You may easily change this parameter. - // Printing the tree's top - System.out.format("%"+ (numberOfRowGroups + 5) + "s\n", "\\ /"); - System.out.format("%"+ (numberOfRowGroups + 7) + "s\n", "-->*<--"); - System.out.format("%"+ (numberOfRowGroups + 5) + "s\n", "/_\\"); + // Printing the tree's top. We have to escape the backslash using \\. + System.out.print(""" + \\ / + -->*<-- + /_\\""".indent(numberOfRowGroups)); // Printing the tree's body // Loop printing the tree's body row group wise. for (int rowGroup = 0; rowGroup < numberOfRowGroups; rowGroup++) { - + final int indentation = numberOfRowGroups - rowGroup + 1; // First body line of current group - System.out.format("%"+ (numberOfRowGroups + rowGroup + 6) + "s\n", "/" + "_\\".repeat(rowGroup + 2)); + System.out.print(('/' + "_\\".repeat(rowGroup + 2)).indent(indentation)); // Second body line of current group - System.out.format - ("%"+ (numberOfRowGroups + rowGroup + 6) + "s\\\n", - "/_".repeat(rowGroup + 3)); + System.out.print(("/_".repeat(rowGroup + 2) + '\\').indent(indentation)); } // Printing the tree's trunk - System.out.format("%"+ (numberOfRowGroups + 6) + "s\n", "[___]"); + System.out.print("[___]".indent(numberOfRowGroups + 1)); } } diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index cf33dcf3..58b62380 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -3045,7 +3045,8 @@ for (int row = 0; row < numberOfRows; row++) { We start coding the proposed helper exercise. Each line has to be indented using space ' ' characters being - represented by in the right diagram: + represented by + in the right diagram: @@ -3071,12 +3072,12 @@ for (int row = 0; row < numberOfRows; row++) { ⟹ - ␣␣␣␣␣␣* -␣␣␣␣␣** -␣␣␣␣*** -␣␣␣**** -␣␣***** -␣****** + ␣␣␣␣␣␣* +␣␣␣␣␣** +␣␣␣␣*** +␣␣␣**** +␣␣***** +****** ******* @@ -3102,23 +3103,23 @@ Row index 5 *********** 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 X + spaces by . In + the »bigger« tree's first line we need e.g. 5 + spaces before actually printing the tree's very top + 'X' character. - A tree with A tree with + A tree with A tree with 6 body rows 2 row groups -␣␣␣␣␣X ␣␣␣X -␣␣␣␣␣* ␣␣␣* -␣␣␣␣*** ␣␣*** -␣␣␣***** ␣***** -␣␣******* ******* -␣********* ␣␣### +␣␣␣␣␣X ␣␣␣X +␣␣␣␣␣* ␣␣␣* +␣␣␣␣*** ␣␣*** +␣␣␣***** ***** +␣␣******* ******* +********* ␣␣### *********** -␣␣␣␣### -␣␣␣␣### +␣␣␣␣### +␣␣␣␣### The precise amounts of these indentations depend on the tree's size. Printing larger trees requires larger indentation @@ -3129,7 +3130,7 @@ Row index 5 *********** We start printing the tree's top. This requires numberOfRows space characters followed by - the top's X character: + the top's 'X' character: @@ -3147,11 +3148,11 @@ Row index 5 *********** role="red">X" top. We need System.out.print(' '); // numberOfRowGroups preceding spaces (␣) } // before eventually printing the -System.out.println("X"); // "X'); // "X" String followed by a newline (println). - ␣␣␣␣␣X ( + ␣␣␣␣␣X( denoting space) @@ -3182,7 +3183,7 @@ System.out.println("X"); System.out.print(' '); // (numberOfRows - row) } // space (␣) characters ... - for (int x = 0; x < 2 * row + 1; x ++) { // .. then printing (2*row+1) + for (int x = 0; x < 2 * row + 1; x ++) { // .. then printing (2 * row + 1) // asterisk('*') characters ... System.out.print('*'); // (May try 'X"); System.out.print("\n"); // ... and finally terminating the } // current body row. - ␣␣␣␣␣X -␣␣␣␣␣* -␣␣␣␣*** -␣␣␣***** -␣␣******* -␣********* -*********** + ␣␣␣␣␣X +␣␣␣␣␣* +␣␣␣␣*** +␣␣␣***** +␣␣******* +********* +*********** @@ -3426,11 +3412,11 @@ System.out.format("%"+ (numberOfRows + 1) + "s\n", "###"); - A complete solution is available at the MI - Gitlab repository. We start again from a version being - fully covered by our current knowledge. This time we try - printing the following lines: + A complete solution is provided by Xmas.java. + You may want to import the whole project. We start again from a + version being fully covered by our current knowledge. This time + we try printing the following lines: Looping through \ / Part 1: The tree's top. @@ -3724,40 +3710,38 @@ System.out.println("[___]"); - Like in the previous exercise we now replace all inner loops - by - System.out.format() + Similar to the previous exercise this time we replace all inner + loops by using String.indent() and String.repeat(). - This variants full source code is on offer at XmasUsingFormat.java. - final int numberOfRowGroups = 5; // You may easily change this parameter. + final int numberOfRowGroups = 5; // You may easily change this parameter. -// Printing the tree's top -System.out.format("%"+ (numberOfRowGroups + 5) + "s\n", "\\ /"); -System.out.format("%"+ (numberOfRowGroups + 7) + "s\n", "-->*<--"); -System.out.format("%"+ (numberOfRowGroups + 5) + "s\n", "/_\\"); +// Printing the tree's top. We have to escape the backslash using \\. +System.out.print(""" + \\ / + -->*<-- + /_\\""".indent(numberOfRowGroups)); // Printing the tree's body // Loop printing the tree's body row group wise. for (int rowGroup = 0; rowGroup < numberOfRowGroups; rowGroup++) { + final int indentation = numberOfRowGroups - rowGroup + 1; + // First body line of current group + System.out.print(('/' + "_\\".repeat(rowGroup + 2)).indent(indentation)); - // First body line of current group - System.out.format("%"+ (numberOfRowGroups + rowGroup + 6) + "s\n", "/" + "_\\".repeat(rowGroup + 2)); - - // Second body line of current group - System.out.format - ("%"+ (numberOfRowGroups + rowGroup + 6) + "s\\\n", - "/_".repeat(rowGroup + 3)); - } + // Second body line of current group + System.out.print(("/_".repeat(rowGroup + 2) + '\\').indent(indentation)); +} // Printing the tree's trunk -System.out.format("%"+ (numberOfRowGroups + 6) + "s\n", "[___]"); - +System.out.print("[___]".indent(numberOfRowGroups + 1)); Both implementation variants allow for setting e.g. final int numberOfRowGroups = 8 creating -- GitLab