Spacing before Christmas tree trunk - java

I have an exercise where I have to print a christmas tree.
public class ChristmasTree {
public static void main(String[] args) {
int size = 6;
printChristmasTree(size);
}
public static void printChristmasTree(int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < (2 * i + 1); j++) {
System.out.print("*");
}
System.out.println();
}
for (int k = 0; k < 1; k++) {
System.out.print("**");
System.out.println();
}
System.out.println();
}
}
What I get is:
*
***
*****
*******
*********
***********
**
How can I fix it? How should I change this loop to move that trunk to the center of that tree?

Change
for (int k = 0; k < 1; k++) {
System.out.print("**");
System.out.println();
}
to
for (int k = 0; k < size; k++) {
System.out.print(" ");
}
System.out.print("**");
You need to loop to find number of spaces from size, and place it accordingly. Adjust the loop variable k if it is off a bit.

You said christmass tree so I guessed that you want the tree trunk to be in different lines. If it's what you want it's the code here:
for (int k = 0; k < 2; k++) {
for (int l = 0; l <= size; l++) {
if (l != size)
System.out.print(" ");
else
System.out.println("*");
}
System.out.println();
}
If you want in the same line here you are:
for (int l = 0; l <= size; l++) {
if (l != size)
System.out.print(" ");
else
System.out.print("**");
}
System.out.println();

Related

How do I print the same output multiple times, horizontally and vertically, in a grid?

I can print a single pine tree of varying sizes, but how do I print an exact replica of it next to it and underneath it, in a grid, using only for loops? This project utilizes a scanner so that the User can vary the size and number of section each tree has. The last portion of the project allows the user to pick how many trees to print, horizontally(next to each other) and vertically(underneath it). The exact wording and an example: Program makes use of a third input to print a number of trees. Ex: When NUM_TREES is three, a 3x3 grid of trees is printed. Any tips or hints are helpful, thank you.
Here is the code I have to get a single tree:
import java.util.Scanner;
public class PineTreeUpgrade {
static int numSections;
static int sectionSize;
public static void main(String[] args) {
askTheUserForInput();
System.out.println();
System.out.println("Here is your amazing creation! How artistic!");
printTree();
printStem();
}
public static void askTheUserForInput() {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello, user!");
System.out.println("How many section would you like in your pine tree?");
numSections = userInput.nextInt();
System.out.println("Great! How big would you like each section to be?");
sectionSize = userInput.nextInt();
}
public static void printTree() {
for (int k = 0; k < numSections; k++) {
//prints the next section, each one increasing in size
for(int i = 0; i < sectionSize; i++) {
//prints the spaces and number of stars in each section
for (int j = i; j < (sectionSize - 1) + (numSections - 1) - k; j++){
System.out.print(" ");
}
System.out.print("/");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("*");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.println("\\");
}
}
}
public static void printStem() {
for(int i = 0; i < 2; i++) {
for (int j = 0; j < (sectionSize - 1) + (numSections -1) + 1; j++) {
System.out.print(" ");
}
System.out.println("|");
}
for (int i = 0; i < 1; i++) {
for (int j = 0; j < (sectionSize - 1) + (numSections -1); j++) {
System.out.print(" ");
}
System.out.print("___");
}
}
}
import java.util.Scanner;
public class PineTreeUpgrade {
static int numSections;
static int sectionSize;
static int gridSize;
public static void main(String[] args) {
askTheUserForInput();
System.out.println();
System.out.println("Here is your amazing creation! How artistic!");
for(int x = 0; x < gridSize; x++){
printTree();
printStem();
System.out.println();
}
}
public static void askTheUserForInput() {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello, user!");
System.out.println("How many section would you like in your pine tree?");
numSections = userInput.nextInt();
System.out.println("Great! How big would you like each section to be?");
sectionSize = userInput.nextInt();
System.out.println("What grid size do you want?");
gridSize = userInput.nextInt();
}
public static void printTree() {
for (int k = 0; k < numSections; k++) {
//prints the next section, each one increasing in size
for(int i = 0; i < sectionSize; i++) {
//prints the spaces and number of stars in each section
for(int x = 0; x < gridSize; x++){
for (int j = i; j < (sectionSize - 1) + (numSections - 1) - k; j++){
System.out.print(" ");
}
System.out.print("/");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("*");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("\\");
int width = 2 * (sectionSize + numSections - 1) + 1;
if(x != gridSize - 1){
for(int y = sectionSize + numSections + k + i; y < width; y++)
System.out.print(" ");
System.out.print(" ");
}
}
System.out.println();
}
}
}
public static void printStem() {
for(int i = 0; i < 2; i++) {
for(int x = 0; x < gridSize; x++){
for (int j = 0; j < (sectionSize - 1) + (numSections -1) + 1; j++)
System.out.print(" ");
System.out.print("|");
if(x != gridSize - 1)
for (int j = 0; j <= sectionSize + numSections; j++)
System.out.print(" ");
}
System.out.println();
}
for (int i = 0; i < 1; i++) {
for(int x = 0; x < gridSize; x++){
for (int j = 0; j < (sectionSize - 1) + (numSections -1); j++)
System.out.print(" ");
System.out.print("___");
if(x != gridSize - 1)
for (int j = 0; j < sectionSize + numSections; j++)
System.out.print(" ");
}
}
}
}
width_of_double_pine_tree = 2 * width_of_single_pine_tree + buffer_between_trees
For loops don't have to define a variable, they can use an existing one.
int j = 0;
for(; j < (width_of_double_pine_tree - (width_of_single_pine_tree + buffer_between_trees)); j++)
//this is your first tree
for(; j < (width_of_double_pine_tree - width_of_single_pine_tree); j++)
//this is your buffer (S.o.p spaces)
for(; j < (width_of_double_pine_tree); j++)
//this is your second tree
Your canvas is always a rectangle, you subtract from it, the edge spaces you aren't using, void space with spaces (tabs ruin everything, because they aren't uniform, use spaces in everything!)
Use your i variable to keep track of the height that you're currently painting on your canvas so you can math in your pine needles and trunk. For the second tree, in the math portion, offset your j by the buffer_between_trees and width_of_single_tree if(i < (j + buffer + width)), etc.
Make sure you do the assignment before you tackle extra stuff.

Java Pattern Program Error

I want to make a program which gives this output:
*
**
***
****
***
**
*
where the maximum number of stars is 'n'. however, something is wrong with my code here. help will be appreciated.
class test
{
public static void main(int n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
for(int x=n;x>=1;x--)
{
System.out.print("*");
}
System.out.println();
}
}
}
You're almost there - think of it as an upright triangle followed by an upside down triangle. In the first one, there are N rows, each row having an increasing number of asterisks, from one up to N. In the second one, each row has a decreasing number of asterisks, from N-1 down to 1:
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
System.out.print('*');
}
System.out.println();
}
for (int i = n - 1; i >= 1; --i) {
for (int j = 0; j < i; ++j) {
System.out.print('*');
}
System.out.println();
}
This could, of course, be made much more elegant by extracting the inner for loop which creates each row to its own method:
private static void printRow (int i) {
for (int j = 0; j < i; ++j) {
System.out.print('*');
}
System.out.println();
}
private static void main (String[] args) {
for (int i = 1; i <= n; ++i) {
printRow(i);
}
for (int i = n - 1; i >= 1; --i) {
printRow(i);
}
}

How can i create these shapes with java

package assignment.pkg3;
import java.util.Scanner;
public class Assignment3 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("What integer length do you want: ");
int length = stdIn.nextInt();
for (int i = 0; i < length; i++) {
for (int j = ((length - 1) - i); j > 0; j--) {
System.out.print(" ");
}
for (int a = 0; a <= i; a++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 0; i < length; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int a = length; a > i; a--) {
System.out.print("*");
}
for (int b = (length - 1); b > i; b--) {
System.out.print("*");
}
System.out.println();
}
for (int i = 0; i < length; i++) {
for (int j = 0; j <= i; j++) {
for (int k = 0; k < i; k++) {
System.out.print(" ");
}
for (int a = 0; a <= i; a++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
Hi, I need help making this 3rd shape.. I don't even know how to start it. I did the other 3 pretty well. (except I have the spaces wrong on shape D). Can anyone help me find somewhere to start. (BTW I'm in a beginner java class so I can't put anything in there that we haven't learned. So sticking to for loops is basically all we can essentially use lol)
Here is link to pic
Try to execute this one & check -
for (int i = 0; i < length/2; i++){
for (int j = 0; j < length; j++){
System.out.print("*" + " ");
}
System.out.println("");
for (int k = 0; k < length-1; k++){
System.out.print(" " + "*");
}
System.out.println("");
}
Hope this will solve your problem.

Using for loops to create an upside-down triangle [duplicate]

This question already has answers here:
Creating a triangle with for loops
(13 answers)
Closed 8 years ago.
Im a java student, and i'm have quite the hard time, trying to use for loops to create an upside down triangle.
This is what my code looks like now, a straight forward triangle. How can i make another one just like it, but upside down?
for (int i=1; i<20; i += 2)
{
for (int k=10; k < (0 - i / 2); k++)
{
System.out.print(" ");
}
for (int j=0; j<i; j++)
{
System.out.print("*");
}
System.out.println("");
}
Is that tricky?
Just change
for (int i=1; i<20; i += 2)
To
for (int i = 19; i >0; i -= 2)
Code.
for (int i = 19; i > 0; i -= 2) {
for (int k = 10; k < (0 - i / 2); k++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
Out put:
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
int c = 2*lines;
for (int i = lines-1; i>=0; i--)
{
for (int j = i; j < lines; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= c; k++)
{
if (k % 2 == 0)
{
System.out.print(" ");
}
else
{
System.out.print(symbol);
}
}
System.out.print("\n");
c -= 2;
}
All you have to do is to change your 3rd for loop
for (int j=0; j<i; j++)
{
System.out.print("*");
}
In your code, you print 1 star, 3 stars, 5 stars and so on... (i stars actually)
To make it upside down, start the j at the max value and decrement it so you print n - i stars
for (int j = 20 - i; j > 0; j--)
{
System.out.print("*");
}
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
for (int i = 1; i < 20; i += 2) {
for (int k = 10; k < (0 - i / 2); k++) {
sb.append(" ");
}
for (int j = 0; j < i; j++) {
sb.append("*");
}
sb.append("\n");
}
System.out.println(sb.reverse());
}
However you are probably supposed to learn something about loops and the algorithm.

I need help writing a program that prints out two shapes on one line using nested loops

Here is what the shapes should look like :
Here is my code so far:
public class Diamonds {
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
I am having trouble getting the second shape
In order to not spoil your fun with this problem, I will explain what you need to do without writing any code.
To get the second shape in there, you would need to add two additional nested for loops into each of the two "outer" loops that you already have.
Loops number three will produce a fixed number of spaces. Note that the distance between the right edge of the first shape and the left edge of the second shape is constant, so your third loops will be easy to code up.
Loops number four will loop like your first loop, but they would change places: the first inner loop from the first outer loop will be the forth inner loop in the second outer loop, and vice versa.
By examining the shape on the right, we can notice that for each N asterisks on the line in the left shape, the right one has 10 - N, so, taking your code and extending it, we can get:
public class Diamonds {
public static final String SPACE = " ";
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print(SPACE);
for (int j = 0; j < 10 - i; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print(SPACE);
for (int j = 0; j < 10 - i; j++)
System.out.print("*");
System.out.print("\n");
}
}
}
And if we extract some common code:
public class Diamonds {
public static final String SPACE = " ";
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
drawLine(i);
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
drawLine(i);
System.out.print("\n");
}
}
private static void drawLine(int i) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print(SPACE);
for (int j = 0; j < 10 - i; j++)
System.out.print("*");
}
}
public class ReverseDiamond {
public static void main(String[] ar) {
int rows = 10;
ReverseDiamond diamond = new ReverseDiamond();
for(int i = 0; i < rows; i++)
diamond.printLine(rows, i);
for(int i = rows - 2; i >= 0; i--)
diamond.printLine(rows, i);
}
private void printLine(int rows, int currRow) {
for(int space = 1; space <= currRow; space++)
System.out.print(" ");
for(int star = 1; star < 2 * (rows - currRow); star++)
System.out.print("*");
System.out.println();
}
}
You may like that :
public class Diamonds {
public static void main(String[] args) {
int totalStars = 9;
int rows = 9;
for (int r = 0,stars=-1,gap=totalStars; r < rows; r++ ) {
stars+= (r<=rows/2) ?2:-2;
gap=totalStars-stars;
printChars(' ', gap);
printChars('*', stars);
printChars(' ', gap);
int gap2=stars+1;
int stars2=gap+1;
printChars(' ', gap2);
printChars('*', stars2);
printChars(' ', gap2);
System.out.println();
}
}
private static void printChars(char c ,int times) {
for (int i = 0; i < times; i++) {
System.out.print(c);
}
}
}
try this :
public static void main(String[] args) {
for (int i = 9; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 2; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j <= i; j++)
System.out.print("*");
System.out.print("\n");
}
}
output :
*********
*******
*****
***
*
***
*****
*******
*********

Categories