Skip to content
Snippets Groups Projects
Commit 5056f162 authored by Goik Martin's avatar Goik Martin
Browse files

Enhanced X-Mas tree solution

parent 40250967
No related branches found
No related tags found
No related merge requests found
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hdm_stuttgart.mi.sd1</groupId>
<artifactId>morefunxmastree</artifactId>
<version>0.9</version>
<packaging>jar</packaging>
<name>morefunxmastree</name>
<url>https://freedocs.mi.hdm-stuttgart.de/sd1_sect_mavenCli.html</url>
<description>Exercise »More fun with Xmas trees«.</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<linksource>true</linksource>
<additionalOptions>
<additionalOption>-html5</additionalOption>
</additionalOptions>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>de.hdm_stuttgart.mi.sd1.App</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
</plugins>
</build>
</project>
package de.hdm_stuttgart.mi.sd1;
public class Dummy {
public static void main(String[] args) {
System.out.print("\""); // Print a double quote "
System.out.print("Hello\n"); // Print 'Hello' followed by a new line.
System.out.print("\t Hello ␣"); // Print an indented 'Hello' string.
System.out.println("\u2B95"); // Print a unicode right arrow ⮕
final int numberOfRowGroups = 5;
System.out.format("%"+ numberOfRowGroups + "s ", "\\ /");
System.out.println();
final int repetitions = 3;
System.out.println("/_".repeat(repetitions));
System.out.println();
final int numberOfSpaces = 6;
System.out.format("%"+ numberOfSpaces + "s ", "");
System.out.print(":");
}
}
package de.hdm_stuttgart.mi.sd1;
/**
* Beginner's way of implementing the »More fun with X-mas trees« exercise.
*/
public class Xmas {
public static void main(String[] args) {
// Example: 5 row groups, tree body's loop index ranging from 0 to 4
// \ / Part 1: Printing tree's top.
// -->*<--
// /_\
// Row group index 0 /_\_\ Part 2: 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.
final int numberOfRowGroups = 5; // You may easily change this
// parameter.
// Printing tree's top
//
// Tree's top first line
for (int x = 0; x < numberOfRowGroups + 1; x++) { // Printing the tree's "\ /"
// top. We need numberOfRows+1
System.out.print(' '); // preceding spaces before
} // eventually printing the
System.out.println("\\ /"); // "\ /" String.
// Tree's top second line
for (int x = 0; x < numberOfRowGroups - 1; x++) { // Printing the tree's top '-->*<--'
// We need numberOfRows-1
System.out.print(' '); // preceding spaces
} // before printing the
System.out.println("-->*<--"); // "-->*<--" string.
// Tree's top third line
for (int x = 0; x < numberOfRowGroups + 1; x++) { // The tree's lower top "/ \" line.
System.out.print(' '); // Again we need numberOfRowGroups+1
} // preceding spaces.
System.out.println("/_\\");
// Part two: The tree's body
//
for (int rowGroup = 0; // Outer loop printing the
rowGroup < numberOfRowGroups; rowGroup++) { // tree's body.
// First body line of current group
//
for (int x = 0; // Starting first line
x < numberOfRowGroups - rowGroup;x++) { // of row group with
// (numberOfRows - row)
System.out.print(' '); // space characters ...
}
System.out.print("/"); // Start of current row group's
for (int x = 0; x < rowGroup + 2;x++) { // first line tree body content
System.out.print("_\\"); // finishing.
}
System.out.println();
// Second body line of current group
//
for (int x = 0; // Starting second line of row
x < numberOfRowGroups - rowGroup - 1; x++) { // group with (numberOfRows -
System.out.print(' '); // row - 1) space characters ...
}
for (int x = 0; x < rowGroup + 3;x++) { // tree body content
System.out.print("/_");
}
System.out.println("\\"); // finishing.
}
// Part three: The tree's bottom trunk
//
for (int x = 0; x < numberOfRowGroups; x++) { // Indenting the bottom trunk ...
System.out.print(' ');
}
System.out.println("[___]"); // printing the trunk.
}
}
package de.hdm_stuttgart.mi.sd1;
/**
* Simplified implementation replacing loops by {@link java.io.PrintStream#format(String, Object...)} and
* {@link String#repeat(int)}.
*/
public class XmasUsingFormat {
public static void main(String[] args) {
// Example: 5 row groups, tree body's loop index ranging from 0 to 4
// \ / Part 1: Printing tree's top.
// -->*<--
// /_\
// Row group index 0 /_\_\ Part 2: 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.
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 body
// Loop printing the tree's body row group wise.
for (int rowGroup = 0; rowGroup < numberOfRowGroups; rowGroup++) {
// 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));
}
// Printing the tree's trunk
System.out.format("%"+ (numberOfRowGroups + 6) + "s\n", "[___]");
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment