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 is the internet​
ZanzabumX [31]
<h2>♨ANSWER♥</h2>

The Internet, sometimes called simply "the Net," is a worldwide system of computer networks -- a network of networks in which users at any one computer can, if they have permission, get information from any other computer

<u>☆</u><u>.</u><u>.</u><u>.</u><u>hope this helps</u><u>.</u><u>.</u><u>.</u><u>☆</u>

_♡_<em>mashi</em>_♡_

5 0
2 years ago
Read 2 more answers
Writing a function that implements a loop to collect a set amount of data. The function should use the serial object, the record
Assoli18 [71]
You can still go on a date with you if I get a text from my friend that is in a relationship and you don’t know why
4 0
3 years ago
What is the name for software designed to find and open Web documents?
Liono4ka [1.6K]
It's C.spider software purpose is to find/view web documents.
7 0
3 years ago
Hey, how do we get our warnings removed? I got one and I didn't realize that what I did was wrong.
vodka [1.7K]

Sorry we can't remove our warnings

5 0
2 years ago
Read 2 more answers
What is the purpose of the Excel Function Reference?
Citrus2011 [14]

Answer:

To look up functions and their purposes

Explanation:

Edg

3 0
3 years ago
Other questions:
  • How can vlan hopping attacks be prevented on a network?
    10·1 answer
  • We want to construct a memory with 256 bytes in capacity. Assume that each byte has a unique address. (a) How many address lines
    14·1 answer
  • How is it possible to find encyclopedias and reference texts on the internet
    11·2 answers
  • What is hyper transport
    10·1 answer
  • What does lurch mean
    14·2 answers
  • Although some families have more resources than other families, there is always a limited amount of resources that families have
    15·2 answers
  • He flow of electric charges through a material describes an electric _______.
    10·1 answer
  • What are your thoughts on copyright?<br><br> (Write 2 or more sentences)
    9·2 answers
  • Who designed the apple i computer in 1976?
    13·1 answer
  • Beginning in cell B21, complete the series of substitution values ranging from $35 to $45 in increments of $2.50 vertically down
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!