Commit c46bc861 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Java 15 version of Ascii Xmas tree

parent 30b37a97
......@@ -25,8 +25,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<source>15</source>
<target>15</target>
</configuration>
</plugin>
......
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 {
......
......@@ -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));
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment