1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
nataly862011 [7]
3 years ago
8

Write a program that displays the following pattern: ..\.\* .\.\*** \.\***** ******* \.\***** .\.\*** ..\.\* That is, seven line

s of output as follows: The first consists of 3 spaces followed by a star. The second line consists of 2 spaces followed by a 3 stars. The third consists of one space followed by 5 stars, and the fourth consists just of 7 stars. The fifth line is identical to third, the sixth to the second and the seventh to the first. Your program class should be called StarPattern
Computers and Technology
1 answer:
nalin [4]3 years ago
3 0

Answer:

public class StarPattern {

   

   public static final int MAX_ROWS = 7;

   

    public static void main(String []args){

       

       for (int row = 1; row <= MAX_ROWS; row++) {

           int numOfSpaces = getNumberOfSpaces(row);

           int numOfStars = MAX_ROWS - (getNumberOfSpaces(row) * 2);

           String spaces = printSpaces(numOfSpaces);

           String stars = printStars(numOfStars);

           System.out.println(spaces + stars);

       }

       

    }

    public static int getNumberOfSpaces(int row) {

        int rowOffset = (MAX_ROWS / 2) + 1;

        return Math.abs(row-rowOffset);

    }

   

    public static String printSpaces(int num) {

        String result = "";

        for (int i = 0; i < num; i++) {

            result += " ";

        }

        return result;

    }

   

    public static String printStars(int num) {

        String result = "";

        for (int i = 0; i < num; i++) {

            result += "*";

        }

        return result;

    }

}

Explanation:

So it sounds we need to make a diamond shape out of asterisks like this:

  *

 ***

*****

*******

*****

 ***

  *

There are 7 rows and each row has up to 7 characters. The pattern is also symmetrical which makes it easier. Before writing any code, let's figure out how we are going to determine the correct amount of spaces we need to print. There are a lot of ways to do this, but I'll just show one. Let's call the 4th row y=0. Above that we have row 3 which would be y=-1, and below is row 5 which is y=1. With 7 rows, we have y=-3 through y=3. The absolute value of the y value is how many spaces we need.

To determine the number of stars, we just double the number of spaces, then subtract this number from 7. This is because we can imagine that the same amount of spaces that are printed in front of the stars are also after the stars. We don't actually have to print the second set of spaces since it is just white space, but the maximum number of characters in a row is 7, and this formula will always make sure we have 7.

I hope this helps. If you need help understanding any part of the code, jsut let me know.

You might be interested in
What should you consider when developing your website content?​
Law Incorporation [45]

Answer:

what your customers are looking for.

your latest promotions and discounts.

your brand values.

whether the content will go viral or not

3 0
3 years ago
Read 2 more answers
One way to increase savings is to _____ cash inflows
Arlecino [84]

I beleive anything to do with manage

4 0
3 years ago
I cant see the answers because the stars are blocking them how can this be fixed
viva [34]

I'm sorry whats your question?

7 0
3 years ago
Bloodborne pathogens travel by way of blood cells and can only be spread by person-to-person contact with infected blood
Eva8 [605]
That is incorrect. It is only contagious if blood is injected into person who is not infected.
5 0
3 years ago
Read 2 more answers
The type of materials used in insulators is classified as what
olganol [36]

the answer is Class H

5 0
4 years ago
Other questions:
  • A(n) __________ employs a method called "tunneling" in which each packet from the sending computer is encapsulated within anothe
    8·1 answer
  • A(n) _____ is a computer program that can damage files and programs on your computer.
    6·2 answers
  • The traditional UNIX scheduler enforces an inverse relationship between priority numbers and priorities: the higher the numbe1~
    6·1 answer
  • The blue bar across the top of the screen informs you of the Screen Title, or what step you are on.
    5·1 answer
  • TWO QUICK QUESTIONS
    12·1 answer
  • Assume user_name equals "Tom" and user_age equals 22. What is printed on the console when the following statement is executed? c
    14·1 answer
  • What should you do if your computer is running slower
    12·1 answer
  • Where does change management play a major role in transforming a client
    11·1 answer
  • If you have limited means, you...?
    9·1 answer
  • What would be the most important data to access in the HRIS for the units and divisions of the MNE to determine feasible HR prog
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!