Question

In: Computer Science

In the following Java program replace conditions in while loops to produce Christmas tree output. import...

In the following Java program replace conditions in while loops to produce Christmas tree output.

import java.util.Scanner;

public class Tree

{

    public static void main(String[] args)

    {

        int size;

        Scanner scan = new Scanner(System.in);

        System.out.print("Enter the size: ");

        size = scan.nextInt();

        int count = 0;

        while (__________) //??? condition

        {

            int len = 0;

            // print blanks

            while (____________)//??? condition

            {

                System.out.print(' ');

                len++;

            }

            len = 0;

            // print stars

            while (____________)//??? condition

            {

                System.out.print('*');

                len++;

            }

            System.out.println(); // go to next line

            count++;

        }

    }

}

Generated output for the size of 7:

Enter the size: 7

      *

     ***

    *****

   *******

*********

***********

*************

Solutions

Expert Solution

Code:

import java.util.Scanner;
public class Tree
{
public static void main(String[] args)
{
int size;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the size: ");
size = scan.nextInt();
int count = 0;
while (count < size) //to print size of rows.
{
int len = 0;
// print blanks
while (len < size-count)//to print blanks before stars
{
System.out.print(' ');
len++;
}
len = 0;
// print stars
while (len < (2 * count + 1))//to print stars
{
System.out.print('*');
len++;
}
System.out.println(); // go to next line
count++;
}
}
}

output:

Note: my friend if you have any questions or queries comment below. i am happy to answer your all questions. i will sort out your queries. Thank you my friend.


Related Solutions

Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Using the IntelliJ IDEA tool, write a program that will print a Christmas tree with the...
Using the IntelliJ IDEA tool, write a program that will print a Christmas tree with the "*". The tree should have at least 10 lines. Look back at the birthday cake lab for examples of how to do this lab. Submit a screen shot of your output in the console (i.e. non REPL tool).
Java Program. !ONLY USING WHILE LOOPS! (Completed but with errors, wanted to revise could anyone re-create...
Java Program. !ONLY USING WHILE LOOPS! (Completed but with errors, wanted to revise could anyone re-create this?) Write a program to keep track of the number of miles you have driven during an extended vacation and the number of gallons of gasoline you used during this time, recorded at weekly intervals. This vacation will last over several weeks (the precise number of weeks while making the program is unknown to the coder). Ask the user to enter the number of...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In 2020 the average cost of living/month (excluding housing) for a family of 4 in Pittsburgh was $3850 per month. Write a program to print the first year in which the cost of living/month is over $4450 given that it will rise at a rate of 2.1% per year. (Note:  this program requires no input). Program 2: discount A discount store is having a sale where...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public class Number{ public static void main(String[] args) {    Scanner scan = new Scanner(System.in); System.out.println("Enter 20 integers ranging from -999 to 999 : "); //print statement int[] array = new int[20]; //array of size 20 for(int i=0;i<20;i++){ array[i] = scan.nextInt(); //user input if(array[i]<-999 || array[i]>999){ //check if value is inside the range System.out.println("Please enter a number between -999 to 999"); i--; } } //...
Use nested for loops statements to generate the following output. (Java) -----1 ---22 --333 4444 Ignore...
Use nested for loops statements to generate the following output. (Java) -----1 ---22 --333 4444 Ignore the dashes formatting wouldnt allow for extra spaces behind the numbers
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
Write a program in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
JAVA SHOW YOUR OUTPUT package exampletodo; import java.util.Arrays; import stdlib.*; /** * Edit the sections marked...
JAVA SHOW YOUR OUTPUT package exampletodo; import java.util.Arrays; import stdlib.*; /** * Edit the sections marked TODO * * Unless specified otherwise, you must not change the declaration of any * method. */ public class example {        /**        * valRange returns the difference between the maximum and minimum values in the        * array; Max-Min. Precondition: the array is nonempty. Your solution must go        * through the array at most once.        *        * Here are...
Edit the given program to produce the following output in c++ mode: - Take in the...
Edit the given program to produce the following output in c++ mode: - Take in the name of a superhero & tell him how many villains he/she has to defeat today - The number of villains is randomly generated and should be a number between 11 and 42. - Use a seed of 7. Hint: Compile the program first before making edits What is your name? Hello Captain America There are 42 villains you need to defeat today Oops! one...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT