Java Simple Christmas tree












1















I am new to java and I have to create a simple java programm that creates a christmas tree in this form:



10|       *       |15=7+1+7
9| *** |15=6+3+6
8| ***** |15=5+5+5
7| ******* |15=4+7+4
6| ********* |15=3+9+3
5| *********** |15=2+11+2
4| ************* |15=1+13+1
3|***************|15=0+15+0
2| *** |15=6+3+6
1| *** |15=6+3+6


The height (all natural positive numbers) and the material ("*" in this case is given by the user input).
This is what I already have, but I dont know how to get the "|15=7+1+7" at the end of each line and the Tree trunk in the bottom of the tree.



Here is my actual code, and what it creates:



public class Christmas{
public static void main(String args) {

int height = Integer.parseInt(args[0]);
String letters = (args[1]);
char firstLetter = letters.charAt(0);

//System.out.println(height+" "+firstLetter);

for (int i = 0; i < height; i++) {
// System.out.print((args.length)+"");
int row_number = height-i;
System.out.printf("%2d",row_number);
System.out.print("|");

for (int j = 1; j < height - i; j++){
System.out.print(" ");
}
for (int k = 0; k < (2 * i + 1); k++){
System.out.print(firstLetter+"");
}

System.out.println();

}
}
}


output:



C:UsersnameDesktopJavaFolder>javac Christmas.java && java Christmas 10 *OMEGALUL
10| *
9| ***
8| *****
7| *******
6| *********
5| ***********
4| *************
3| ***************
2| *****************
1|*******************


How can I add the Tree trunk which is always 3 letters long and 1/4 of each tree big.(rounded) and the |15=7+1+7 at the end of each lines which contains:
The width of the tree as the sum of the spaces left + the width of the tree in the respective row + spaces on the right (left-aligned).










share|improve this question

























  • Looks like you're on the right track. Maybe just have one loop for the height, then have variables for numLeftSpaces and numRightSpaces and you would decrement those each loop iteration. You could have another variable for numAsterisks, and that would increment by two during each loop iteration. If you get that working, you should be able to print the numbers to the right. For the trunk, it seems fairly simple so keep trying.

    – dcp
    Nov 14 '18 at 0:58











  • trunkHeight = treeHeight - (treeHeight * 4) / 5 then trunkHeight = treeHeight - trunkHeight in your example, if the total height was 10, then the first bit would calculate the leaves to be 8 and the trunk to be 2. I'm assuming you can take it from there.

    – Ali
    Nov 14 '18 at 1:02











  • Just right now I am really trying to get it working but i am sadly just not able to make it working and my deadline is in some hours.

    – FlazzerXP
    Nov 14 '18 at 1:15
















1















I am new to java and I have to create a simple java programm that creates a christmas tree in this form:



10|       *       |15=7+1+7
9| *** |15=6+3+6
8| ***** |15=5+5+5
7| ******* |15=4+7+4
6| ********* |15=3+9+3
5| *********** |15=2+11+2
4| ************* |15=1+13+1
3|***************|15=0+15+0
2| *** |15=6+3+6
1| *** |15=6+3+6


The height (all natural positive numbers) and the material ("*" in this case is given by the user input).
This is what I already have, but I dont know how to get the "|15=7+1+7" at the end of each line and the Tree trunk in the bottom of the tree.



Here is my actual code, and what it creates:



public class Christmas{
public static void main(String args) {

int height = Integer.parseInt(args[0]);
String letters = (args[1]);
char firstLetter = letters.charAt(0);

//System.out.println(height+" "+firstLetter);

for (int i = 0; i < height; i++) {
// System.out.print((args.length)+"");
int row_number = height-i;
System.out.printf("%2d",row_number);
System.out.print("|");

for (int j = 1; j < height - i; j++){
System.out.print(" ");
}
for (int k = 0; k < (2 * i + 1); k++){
System.out.print(firstLetter+"");
}

System.out.println();

}
}
}


output:



C:UsersnameDesktopJavaFolder>javac Christmas.java && java Christmas 10 *OMEGALUL
10| *
9| ***
8| *****
7| *******
6| *********
5| ***********
4| *************
3| ***************
2| *****************
1|*******************


How can I add the Tree trunk which is always 3 letters long and 1/4 of each tree big.(rounded) and the |15=7+1+7 at the end of each lines which contains:
The width of the tree as the sum of the spaces left + the width of the tree in the respective row + spaces on the right (left-aligned).










share|improve this question

























  • Looks like you're on the right track. Maybe just have one loop for the height, then have variables for numLeftSpaces and numRightSpaces and you would decrement those each loop iteration. You could have another variable for numAsterisks, and that would increment by two during each loop iteration. If you get that working, you should be able to print the numbers to the right. For the trunk, it seems fairly simple so keep trying.

    – dcp
    Nov 14 '18 at 0:58











  • trunkHeight = treeHeight - (treeHeight * 4) / 5 then trunkHeight = treeHeight - trunkHeight in your example, if the total height was 10, then the first bit would calculate the leaves to be 8 and the trunk to be 2. I'm assuming you can take it from there.

    – Ali
    Nov 14 '18 at 1:02











  • Just right now I am really trying to get it working but i am sadly just not able to make it working and my deadline is in some hours.

    – FlazzerXP
    Nov 14 '18 at 1:15














1












1








1


0






I am new to java and I have to create a simple java programm that creates a christmas tree in this form:



10|       *       |15=7+1+7
9| *** |15=6+3+6
8| ***** |15=5+5+5
7| ******* |15=4+7+4
6| ********* |15=3+9+3
5| *********** |15=2+11+2
4| ************* |15=1+13+1
3|***************|15=0+15+0
2| *** |15=6+3+6
1| *** |15=6+3+6


The height (all natural positive numbers) and the material ("*" in this case is given by the user input).
This is what I already have, but I dont know how to get the "|15=7+1+7" at the end of each line and the Tree trunk in the bottom of the tree.



Here is my actual code, and what it creates:



public class Christmas{
public static void main(String args) {

int height = Integer.parseInt(args[0]);
String letters = (args[1]);
char firstLetter = letters.charAt(0);

//System.out.println(height+" "+firstLetter);

for (int i = 0; i < height; i++) {
// System.out.print((args.length)+"");
int row_number = height-i;
System.out.printf("%2d",row_number);
System.out.print("|");

for (int j = 1; j < height - i; j++){
System.out.print(" ");
}
for (int k = 0; k < (2 * i + 1); k++){
System.out.print(firstLetter+"");
}

System.out.println();

}
}
}


output:



C:UsersnameDesktopJavaFolder>javac Christmas.java && java Christmas 10 *OMEGALUL
10| *
9| ***
8| *****
7| *******
6| *********
5| ***********
4| *************
3| ***************
2| *****************
1|*******************


How can I add the Tree trunk which is always 3 letters long and 1/4 of each tree big.(rounded) and the |15=7+1+7 at the end of each lines which contains:
The width of the tree as the sum of the spaces left + the width of the tree in the respective row + spaces on the right (left-aligned).










share|improve this question
















I am new to java and I have to create a simple java programm that creates a christmas tree in this form:



10|       *       |15=7+1+7
9| *** |15=6+3+6
8| ***** |15=5+5+5
7| ******* |15=4+7+4
6| ********* |15=3+9+3
5| *********** |15=2+11+2
4| ************* |15=1+13+1
3|***************|15=0+15+0
2| *** |15=6+3+6
1| *** |15=6+3+6


The height (all natural positive numbers) and the material ("*" in this case is given by the user input).
This is what I already have, but I dont know how to get the "|15=7+1+7" at the end of each line and the Tree trunk in the bottom of the tree.



Here is my actual code, and what it creates:



public class Christmas{
public static void main(String args) {

int height = Integer.parseInt(args[0]);
String letters = (args[1]);
char firstLetter = letters.charAt(0);

//System.out.println(height+" "+firstLetter);

for (int i = 0; i < height; i++) {
// System.out.print((args.length)+"");
int row_number = height-i;
System.out.printf("%2d",row_number);
System.out.print("|");

for (int j = 1; j < height - i; j++){
System.out.print(" ");
}
for (int k = 0; k < (2 * i + 1); k++){
System.out.print(firstLetter+"");
}

System.out.println();

}
}
}


output:



C:UsersnameDesktopJavaFolder>javac Christmas.java && java Christmas 10 *OMEGALUL
10| *
9| ***
8| *****
7| *******
6| *********
5| ***********
4| *************
3| ***************
2| *****************
1|*******************


How can I add the Tree trunk which is always 3 letters long and 1/4 of each tree big.(rounded) and the |15=7+1+7 at the end of each lines which contains:
The width of the tree as the sum of the spaces left + the width of the tree in the respective row + spaces on the right (left-aligned).







java for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 2:18







FlazzerXP

















asked Nov 14 '18 at 0:46









FlazzerXPFlazzerXP

206




206













  • Looks like you're on the right track. Maybe just have one loop for the height, then have variables for numLeftSpaces and numRightSpaces and you would decrement those each loop iteration. You could have another variable for numAsterisks, and that would increment by two during each loop iteration. If you get that working, you should be able to print the numbers to the right. For the trunk, it seems fairly simple so keep trying.

    – dcp
    Nov 14 '18 at 0:58











  • trunkHeight = treeHeight - (treeHeight * 4) / 5 then trunkHeight = treeHeight - trunkHeight in your example, if the total height was 10, then the first bit would calculate the leaves to be 8 and the trunk to be 2. I'm assuming you can take it from there.

    – Ali
    Nov 14 '18 at 1:02











  • Just right now I am really trying to get it working but i am sadly just not able to make it working and my deadline is in some hours.

    – FlazzerXP
    Nov 14 '18 at 1:15



















  • Looks like you're on the right track. Maybe just have one loop for the height, then have variables for numLeftSpaces and numRightSpaces and you would decrement those each loop iteration. You could have another variable for numAsterisks, and that would increment by two during each loop iteration. If you get that working, you should be able to print the numbers to the right. For the trunk, it seems fairly simple so keep trying.

    – dcp
    Nov 14 '18 at 0:58











  • trunkHeight = treeHeight - (treeHeight * 4) / 5 then trunkHeight = treeHeight - trunkHeight in your example, if the total height was 10, then the first bit would calculate the leaves to be 8 and the trunk to be 2. I'm assuming you can take it from there.

    – Ali
    Nov 14 '18 at 1:02











  • Just right now I am really trying to get it working but i am sadly just not able to make it working and my deadline is in some hours.

    – FlazzerXP
    Nov 14 '18 at 1:15

















Looks like you're on the right track. Maybe just have one loop for the height, then have variables for numLeftSpaces and numRightSpaces and you would decrement those each loop iteration. You could have another variable for numAsterisks, and that would increment by two during each loop iteration. If you get that working, you should be able to print the numbers to the right. For the trunk, it seems fairly simple so keep trying.

– dcp
Nov 14 '18 at 0:58





Looks like you're on the right track. Maybe just have one loop for the height, then have variables for numLeftSpaces and numRightSpaces and you would decrement those each loop iteration. You could have another variable for numAsterisks, and that would increment by two during each loop iteration. If you get that working, you should be able to print the numbers to the right. For the trunk, it seems fairly simple so keep trying.

– dcp
Nov 14 '18 at 0:58













trunkHeight = treeHeight - (treeHeight * 4) / 5 then trunkHeight = treeHeight - trunkHeight in your example, if the total height was 10, then the first bit would calculate the leaves to be 8 and the trunk to be 2. I'm assuming you can take it from there.

– Ali
Nov 14 '18 at 1:02





trunkHeight = treeHeight - (treeHeight * 4) / 5 then trunkHeight = treeHeight - trunkHeight in your example, if the total height was 10, then the first bit would calculate the leaves to be 8 and the trunk to be 2. I'm assuming you can take it from there.

– Ali
Nov 14 '18 at 1:02













Just right now I am really trying to get it working but i am sadly just not able to make it working and my deadline is in some hours.

– FlazzerXP
Nov 14 '18 at 1:15





Just right now I am really trying to get it working but i am sadly just not able to make it working and my deadline is in some hours.

– FlazzerXP
Nov 14 '18 at 1:15












1 Answer
1






active

oldest

votes


















3














Here is a different approach, which calculates the number of spaces and the number of fill characters needed on a line, then uses printf to print the lines.



public static void printChristmasTree(int height, char ch) {
if (height <= 4)
throw new IllegalArgumentException("Height must be 5 or higher");
for (int row = height; row > 0; row--) {
int spaces = (row > 2 ? row - 3 : height - 4);
int fill = (height - spaces) * 2 - 5;
System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,
repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),
spaces * 2 + fill, spaces, fill, spaces);
}
}
private static String repeat(int count, char ch) {
char buf = new char[count];
java.util.Arrays.fill(buf, ch);
return new String(buf);
}


Test



printChristmasTree(10, '*');




printChristmasTree(6, '#');


Output



10|       *       |15=7+1+7
9| *** |15=6+3+6
8| ***** |15=5+5+5
7| ******* |15=4+7+4
6| ********* |15=3+9+3
5| *********** |15=2+11+2
4| ************* |15=1+13+1
3|***************|15=0+15+0
2| *** |15=6+3+6
1| *** |15=6+3+6


 6|   #   |7=3+1+3
5| ### |7=2+3+2
4| ##### |7=1+5+1
3|#######|7=0+7+0
2| ### |7=2+3+2
1| ### |7=2+3+2




UPDATE



Here is logic for trunk height of height/4 (rounded), instead of the fixed height of 2 used by the code above. Trunk width is still fixed at 3.



public static void printChristmasTree(int height, char ch) {
final int trunkHeight = (height + 2) / 4; // rounded
final int treeWidth = (height - trunkHeight) * 2 - 1;
final int width = (treeWidth > 3 || trunkHeight == 0 ? treeWidth : 3);
for (int row = height; row > 0; row--) {
int fill = (row > trunkHeight ? (height - row) * 2 + 1 : 3);
int spaces = (width - fill) / 2;
System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,
repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),
spaces * 2 + fill, spaces, fill, spaces);
}
}


Test



printChristmasTree(5, '*');
printChristmasTree(4, '*');
printChristmasTree(3, '*');
printChristmasTree(2, '*');
printChristmasTree(1, '*');


Output



 5|   *   |7=3+1+3
4| *** |7=2+3+2
3| ***** |7=1+5+1
2|*******|7=0+7+0
1| *** |7=2+3+2


 4|  *  |5=2+1+2
3| *** |5=1+3+1
2|*****|5=0+5+0
1| *** |5=1+3+1


 3| * |3=1+1+1
2|***|3=0+3+0
1|***|3=0+3+0


 2| * |3=1+1+1
1|***|3=0+3+0


 1|*|1=0+1+0





share|improve this answer


























  • Thanks for your effort. Works perfect, I just need some minutes to fully understand the code but it looks much more efficient than mine. Thank you, i also upvoted but its not shown because I have less than 15 rep. One question: is there a way to also make it work for a tree with the height between 1 and 5 ?

    – FlazzerXP
    Nov 14 '18 at 1:43













  • @FlazzerXP Since the trunk of the tree is always 2x3, a tree with heights 1 or 2 would be trunk only, and heights 3 or 4 won't look like a tree, so any height <= 4 makes no sense to print.

    – Andreas
    Nov 14 '18 at 1:48













  • It's true that it dont look like a tree, but my task says it has to work for all natural positive numbers. And the trunk is 1/4 (rounded,but a trunk must always exist) of the height of the tree, not always 2. Just the width is always 3.

    – FlazzerXP
    Nov 14 '18 at 1:53








  • 1





    Doing homework for points?

    – Ali
    Nov 14 '18 at 2:09






  • 1





    @FlazzerXP You remove the if statement and change the calculation logic to correctly calculate spaces and fill for those values. --- FYI: (height/4) doesn't round, it truncates.

    – Andreas
    Nov 14 '18 at 2:35













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291594%2fjava-simple-christmas-tree%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Here is a different approach, which calculates the number of spaces and the number of fill characters needed on a line, then uses printf to print the lines.



public static void printChristmasTree(int height, char ch) {
if (height <= 4)
throw new IllegalArgumentException("Height must be 5 or higher");
for (int row = height; row > 0; row--) {
int spaces = (row > 2 ? row - 3 : height - 4);
int fill = (height - spaces) * 2 - 5;
System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,
repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),
spaces * 2 + fill, spaces, fill, spaces);
}
}
private static String repeat(int count, char ch) {
char buf = new char[count];
java.util.Arrays.fill(buf, ch);
return new String(buf);
}


Test



printChristmasTree(10, '*');




printChristmasTree(6, '#');


Output



10|       *       |15=7+1+7
9| *** |15=6+3+6
8| ***** |15=5+5+5
7| ******* |15=4+7+4
6| ********* |15=3+9+3
5| *********** |15=2+11+2
4| ************* |15=1+13+1
3|***************|15=0+15+0
2| *** |15=6+3+6
1| *** |15=6+3+6


 6|   #   |7=3+1+3
5| ### |7=2+3+2
4| ##### |7=1+5+1
3|#######|7=0+7+0
2| ### |7=2+3+2
1| ### |7=2+3+2




UPDATE



Here is logic for trunk height of height/4 (rounded), instead of the fixed height of 2 used by the code above. Trunk width is still fixed at 3.



public static void printChristmasTree(int height, char ch) {
final int trunkHeight = (height + 2) / 4; // rounded
final int treeWidth = (height - trunkHeight) * 2 - 1;
final int width = (treeWidth > 3 || trunkHeight == 0 ? treeWidth : 3);
for (int row = height; row > 0; row--) {
int fill = (row > trunkHeight ? (height - row) * 2 + 1 : 3);
int spaces = (width - fill) / 2;
System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,
repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),
spaces * 2 + fill, spaces, fill, spaces);
}
}


Test



printChristmasTree(5, '*');
printChristmasTree(4, '*');
printChristmasTree(3, '*');
printChristmasTree(2, '*');
printChristmasTree(1, '*');


Output



 5|   *   |7=3+1+3
4| *** |7=2+3+2
3| ***** |7=1+5+1
2|*******|7=0+7+0
1| *** |7=2+3+2


 4|  *  |5=2+1+2
3| *** |5=1+3+1
2|*****|5=0+5+0
1| *** |5=1+3+1


 3| * |3=1+1+1
2|***|3=0+3+0
1|***|3=0+3+0


 2| * |3=1+1+1
1|***|3=0+3+0


 1|*|1=0+1+0





share|improve this answer


























  • Thanks for your effort. Works perfect, I just need some minutes to fully understand the code but it looks much more efficient than mine. Thank you, i also upvoted but its not shown because I have less than 15 rep. One question: is there a way to also make it work for a tree with the height between 1 and 5 ?

    – FlazzerXP
    Nov 14 '18 at 1:43













  • @FlazzerXP Since the trunk of the tree is always 2x3, a tree with heights 1 or 2 would be trunk only, and heights 3 or 4 won't look like a tree, so any height <= 4 makes no sense to print.

    – Andreas
    Nov 14 '18 at 1:48













  • It's true that it dont look like a tree, but my task says it has to work for all natural positive numbers. And the trunk is 1/4 (rounded,but a trunk must always exist) of the height of the tree, not always 2. Just the width is always 3.

    – FlazzerXP
    Nov 14 '18 at 1:53








  • 1





    Doing homework for points?

    – Ali
    Nov 14 '18 at 2:09






  • 1





    @FlazzerXP You remove the if statement and change the calculation logic to correctly calculate spaces and fill for those values. --- FYI: (height/4) doesn't round, it truncates.

    – Andreas
    Nov 14 '18 at 2:35


















3














Here is a different approach, which calculates the number of spaces and the number of fill characters needed on a line, then uses printf to print the lines.



public static void printChristmasTree(int height, char ch) {
if (height <= 4)
throw new IllegalArgumentException("Height must be 5 or higher");
for (int row = height; row > 0; row--) {
int spaces = (row > 2 ? row - 3 : height - 4);
int fill = (height - spaces) * 2 - 5;
System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,
repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),
spaces * 2 + fill, spaces, fill, spaces);
}
}
private static String repeat(int count, char ch) {
char buf = new char[count];
java.util.Arrays.fill(buf, ch);
return new String(buf);
}


Test



printChristmasTree(10, '*');




printChristmasTree(6, '#');


Output



10|       *       |15=7+1+7
9| *** |15=6+3+6
8| ***** |15=5+5+5
7| ******* |15=4+7+4
6| ********* |15=3+9+3
5| *********** |15=2+11+2
4| ************* |15=1+13+1
3|***************|15=0+15+0
2| *** |15=6+3+6
1| *** |15=6+3+6


 6|   #   |7=3+1+3
5| ### |7=2+3+2
4| ##### |7=1+5+1
3|#######|7=0+7+0
2| ### |7=2+3+2
1| ### |7=2+3+2




UPDATE



Here is logic for trunk height of height/4 (rounded), instead of the fixed height of 2 used by the code above. Trunk width is still fixed at 3.



public static void printChristmasTree(int height, char ch) {
final int trunkHeight = (height + 2) / 4; // rounded
final int treeWidth = (height - trunkHeight) * 2 - 1;
final int width = (treeWidth > 3 || trunkHeight == 0 ? treeWidth : 3);
for (int row = height; row > 0; row--) {
int fill = (row > trunkHeight ? (height - row) * 2 + 1 : 3);
int spaces = (width - fill) / 2;
System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,
repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),
spaces * 2 + fill, spaces, fill, spaces);
}
}


Test



printChristmasTree(5, '*');
printChristmasTree(4, '*');
printChristmasTree(3, '*');
printChristmasTree(2, '*');
printChristmasTree(1, '*');


Output



 5|   *   |7=3+1+3
4| *** |7=2+3+2
3| ***** |7=1+5+1
2|*******|7=0+7+0
1| *** |7=2+3+2


 4|  *  |5=2+1+2
3| *** |5=1+3+1
2|*****|5=0+5+0
1| *** |5=1+3+1


 3| * |3=1+1+1
2|***|3=0+3+0
1|***|3=0+3+0


 2| * |3=1+1+1
1|***|3=0+3+0


 1|*|1=0+1+0





share|improve this answer


























  • Thanks for your effort. Works perfect, I just need some minutes to fully understand the code but it looks much more efficient than mine. Thank you, i also upvoted but its not shown because I have less than 15 rep. One question: is there a way to also make it work for a tree with the height between 1 and 5 ?

    – FlazzerXP
    Nov 14 '18 at 1:43













  • @FlazzerXP Since the trunk of the tree is always 2x3, a tree with heights 1 or 2 would be trunk only, and heights 3 or 4 won't look like a tree, so any height <= 4 makes no sense to print.

    – Andreas
    Nov 14 '18 at 1:48













  • It's true that it dont look like a tree, but my task says it has to work for all natural positive numbers. And the trunk is 1/4 (rounded,but a trunk must always exist) of the height of the tree, not always 2. Just the width is always 3.

    – FlazzerXP
    Nov 14 '18 at 1:53








  • 1





    Doing homework for points?

    – Ali
    Nov 14 '18 at 2:09






  • 1





    @FlazzerXP You remove the if statement and change the calculation logic to correctly calculate spaces and fill for those values. --- FYI: (height/4) doesn't round, it truncates.

    – Andreas
    Nov 14 '18 at 2:35
















3












3








3







Here is a different approach, which calculates the number of spaces and the number of fill characters needed on a line, then uses printf to print the lines.



public static void printChristmasTree(int height, char ch) {
if (height <= 4)
throw new IllegalArgumentException("Height must be 5 or higher");
for (int row = height; row > 0; row--) {
int spaces = (row > 2 ? row - 3 : height - 4);
int fill = (height - spaces) * 2 - 5;
System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,
repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),
spaces * 2 + fill, spaces, fill, spaces);
}
}
private static String repeat(int count, char ch) {
char buf = new char[count];
java.util.Arrays.fill(buf, ch);
return new String(buf);
}


Test



printChristmasTree(10, '*');




printChristmasTree(6, '#');


Output



10|       *       |15=7+1+7
9| *** |15=6+3+6
8| ***** |15=5+5+5
7| ******* |15=4+7+4
6| ********* |15=3+9+3
5| *********** |15=2+11+2
4| ************* |15=1+13+1
3|***************|15=0+15+0
2| *** |15=6+3+6
1| *** |15=6+3+6


 6|   #   |7=3+1+3
5| ### |7=2+3+2
4| ##### |7=1+5+1
3|#######|7=0+7+0
2| ### |7=2+3+2
1| ### |7=2+3+2




UPDATE



Here is logic for trunk height of height/4 (rounded), instead of the fixed height of 2 used by the code above. Trunk width is still fixed at 3.



public static void printChristmasTree(int height, char ch) {
final int trunkHeight = (height + 2) / 4; // rounded
final int treeWidth = (height - trunkHeight) * 2 - 1;
final int width = (treeWidth > 3 || trunkHeight == 0 ? treeWidth : 3);
for (int row = height; row > 0; row--) {
int fill = (row > trunkHeight ? (height - row) * 2 + 1 : 3);
int spaces = (width - fill) / 2;
System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,
repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),
spaces * 2 + fill, spaces, fill, spaces);
}
}


Test



printChristmasTree(5, '*');
printChristmasTree(4, '*');
printChristmasTree(3, '*');
printChristmasTree(2, '*');
printChristmasTree(1, '*');


Output



 5|   *   |7=3+1+3
4| *** |7=2+3+2
3| ***** |7=1+5+1
2|*******|7=0+7+0
1| *** |7=2+3+2


 4|  *  |5=2+1+2
3| *** |5=1+3+1
2|*****|5=0+5+0
1| *** |5=1+3+1


 3| * |3=1+1+1
2|***|3=0+3+0
1|***|3=0+3+0


 2| * |3=1+1+1
1|***|3=0+3+0


 1|*|1=0+1+0





share|improve this answer















Here is a different approach, which calculates the number of spaces and the number of fill characters needed on a line, then uses printf to print the lines.



public static void printChristmasTree(int height, char ch) {
if (height <= 4)
throw new IllegalArgumentException("Height must be 5 or higher");
for (int row = height; row > 0; row--) {
int spaces = (row > 2 ? row - 3 : height - 4);
int fill = (height - spaces) * 2 - 5;
System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,
repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),
spaces * 2 + fill, spaces, fill, spaces);
}
}
private static String repeat(int count, char ch) {
char buf = new char[count];
java.util.Arrays.fill(buf, ch);
return new String(buf);
}


Test



printChristmasTree(10, '*');




printChristmasTree(6, '#');


Output



10|       *       |15=7+1+7
9| *** |15=6+3+6
8| ***** |15=5+5+5
7| ******* |15=4+7+4
6| ********* |15=3+9+3
5| *********** |15=2+11+2
4| ************* |15=1+13+1
3|***************|15=0+15+0
2| *** |15=6+3+6
1| *** |15=6+3+6


 6|   #   |7=3+1+3
5| ### |7=2+3+2
4| ##### |7=1+5+1
3|#######|7=0+7+0
2| ### |7=2+3+2
1| ### |7=2+3+2




UPDATE



Here is logic for trunk height of height/4 (rounded), instead of the fixed height of 2 used by the code above. Trunk width is still fixed at 3.



public static void printChristmasTree(int height, char ch) {
final int trunkHeight = (height + 2) / 4; // rounded
final int treeWidth = (height - trunkHeight) * 2 - 1;
final int width = (treeWidth > 3 || trunkHeight == 0 ? treeWidth : 3);
for (int row = height; row > 0; row--) {
int fill = (row > trunkHeight ? (height - row) * 2 + 1 : 3);
int spaces = (width - fill) / 2;
System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,
repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),
spaces * 2 + fill, spaces, fill, spaces);
}
}


Test



printChristmasTree(5, '*');
printChristmasTree(4, '*');
printChristmasTree(3, '*');
printChristmasTree(2, '*');
printChristmasTree(1, '*');


Output



 5|   *   |7=3+1+3
4| *** |7=2+3+2
3| ***** |7=1+5+1
2|*******|7=0+7+0
1| *** |7=2+3+2


 4|  *  |5=2+1+2
3| *** |5=1+3+1
2|*****|5=0+5+0
1| *** |5=1+3+1


 3| * |3=1+1+1
2|***|3=0+3+0
1|***|3=0+3+0


 2| * |3=1+1+1
1|***|3=0+3+0


 1|*|1=0+1+0






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 2:55

























answered Nov 14 '18 at 1:29









AndreasAndreas

76.3k463123




76.3k463123













  • Thanks for your effort. Works perfect, I just need some minutes to fully understand the code but it looks much more efficient than mine. Thank you, i also upvoted but its not shown because I have less than 15 rep. One question: is there a way to also make it work for a tree with the height between 1 and 5 ?

    – FlazzerXP
    Nov 14 '18 at 1:43













  • @FlazzerXP Since the trunk of the tree is always 2x3, a tree with heights 1 or 2 would be trunk only, and heights 3 or 4 won't look like a tree, so any height <= 4 makes no sense to print.

    – Andreas
    Nov 14 '18 at 1:48













  • It's true that it dont look like a tree, but my task says it has to work for all natural positive numbers. And the trunk is 1/4 (rounded,but a trunk must always exist) of the height of the tree, not always 2. Just the width is always 3.

    – FlazzerXP
    Nov 14 '18 at 1:53








  • 1





    Doing homework for points?

    – Ali
    Nov 14 '18 at 2:09






  • 1





    @FlazzerXP You remove the if statement and change the calculation logic to correctly calculate spaces and fill for those values. --- FYI: (height/4) doesn't round, it truncates.

    – Andreas
    Nov 14 '18 at 2:35





















  • Thanks for your effort. Works perfect, I just need some minutes to fully understand the code but it looks much more efficient than mine. Thank you, i also upvoted but its not shown because I have less than 15 rep. One question: is there a way to also make it work for a tree with the height between 1 and 5 ?

    – FlazzerXP
    Nov 14 '18 at 1:43













  • @FlazzerXP Since the trunk of the tree is always 2x3, a tree with heights 1 or 2 would be trunk only, and heights 3 or 4 won't look like a tree, so any height <= 4 makes no sense to print.

    – Andreas
    Nov 14 '18 at 1:48













  • It's true that it dont look like a tree, but my task says it has to work for all natural positive numbers. And the trunk is 1/4 (rounded,but a trunk must always exist) of the height of the tree, not always 2. Just the width is always 3.

    – FlazzerXP
    Nov 14 '18 at 1:53








  • 1





    Doing homework for points?

    – Ali
    Nov 14 '18 at 2:09






  • 1





    @FlazzerXP You remove the if statement and change the calculation logic to correctly calculate spaces and fill for those values. --- FYI: (height/4) doesn't round, it truncates.

    – Andreas
    Nov 14 '18 at 2:35



















Thanks for your effort. Works perfect, I just need some minutes to fully understand the code but it looks much more efficient than mine. Thank you, i also upvoted but its not shown because I have less than 15 rep. One question: is there a way to also make it work for a tree with the height between 1 and 5 ?

– FlazzerXP
Nov 14 '18 at 1:43







Thanks for your effort. Works perfect, I just need some minutes to fully understand the code but it looks much more efficient than mine. Thank you, i also upvoted but its not shown because I have less than 15 rep. One question: is there a way to also make it work for a tree with the height between 1 and 5 ?

– FlazzerXP
Nov 14 '18 at 1:43















@FlazzerXP Since the trunk of the tree is always 2x3, a tree with heights 1 or 2 would be trunk only, and heights 3 or 4 won't look like a tree, so any height <= 4 makes no sense to print.

– Andreas
Nov 14 '18 at 1:48







@FlazzerXP Since the trunk of the tree is always 2x3, a tree with heights 1 or 2 would be trunk only, and heights 3 or 4 won't look like a tree, so any height <= 4 makes no sense to print.

– Andreas
Nov 14 '18 at 1:48















It's true that it dont look like a tree, but my task says it has to work for all natural positive numbers. And the trunk is 1/4 (rounded,but a trunk must always exist) of the height of the tree, not always 2. Just the width is always 3.

– FlazzerXP
Nov 14 '18 at 1:53







It's true that it dont look like a tree, but my task says it has to work for all natural positive numbers. And the trunk is 1/4 (rounded,but a trunk must always exist) of the height of the tree, not always 2. Just the width is always 3.

– FlazzerXP
Nov 14 '18 at 1:53






1




1





Doing homework for points?

– Ali
Nov 14 '18 at 2:09





Doing homework for points?

– Ali
Nov 14 '18 at 2:09




1




1





@FlazzerXP You remove the if statement and change the calculation logic to correctly calculate spaces and fill for those values. --- FYI: (height/4) doesn't round, it truncates.

– Andreas
Nov 14 '18 at 2:35







@FlazzerXP You remove the if statement and change the calculation logic to correctly calculate spaces and fill for those values. --- FYI: (height/4) doesn't round, it truncates.

– Andreas
Nov 14 '18 at 2:35




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291594%2fjava-simple-christmas-tree%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python