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
The New option is found in the ...............tab.​
morpeh [17]

in your notes books and in your vopy

3 0
3 years ago
Read 2 more answers
How long does it take to design and program a character?
Kipish [7]
It takes about 5 days I believe 
6 0
3 years ago
Which of these is one of the primary concerns for protecting your family when online?
Lady_Fox [76]
Sharing personal information
7 0
3 years ago
Read 2 more answers
What is filter in image processing
Alla [95]

Answer:

Image processing operations implemented with filtering include smoothing, sharpening, and edge enhancement.

4 0
3 years ago
Vhat is output by the code below?<br><br> PLEASE HELP!! TIME LIMIT!!
marysya [2.9K]

Answer:

My best guess about this is C

Explanation:

6 0
3 years ago
Other questions:
  • Use a colon before a list and put one space after a colon. True False
    15·2 answers
  • You want to make it possible for your smartphone to share its internet access wirelessly with your friends device which of the f
    10·1 answer
  • A) Write 600 as the product of prime factors.<br> Give your answer in index form.
    7·2 answers
  • Kiara is using her software's graphic formats to create a line graph. As she
    9·2 answers
  • ________ consists of detailed, preprogrammed instructions that control and coordinate the computer hardware components in an inf
    11·1 answer
  • What makes these Pokémon special?
    9·2 answers
  • HI brainly friends....<br> Pease thanks my answers I will also thank your answers
    11·1 answer
  • 1 point
    5·2 answers
  • What is the descriptor for a filter that warns or blocks you from potentially fraudulent or suspicious websites?.
    5·1 answer
  • (lesson 7.9: acceptance-rejection --- continuous examples.) consider the constant . on average, how many iterations (trials) wil
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!