views:

232

answers:

3

How to print the follwoing pattern in JAVA

1.    *   
    * * *
  * * * * *
* * * * * * *

2.        *
        *   *
      *       *
    * * * * * * *

3.          *
          *   *
        *   *   *
      *   *   *   *
   *    *   *   *   *

4. B L U E J
   B L U E
   B L U 
   B L 
   B

5. B
   L B
   U L B
   E U L B
   J E U L B
+4  A: 

For the triangular patterns, you can visualize them as a grid, like so:

 0  1  2  3  4  5  6  7  8  9 ...
[ ][ ][ ][ ][ ][ ][*]
[ ][ ][ ][ ][*][ ][*][ ][*]
[ ][ ][*][ ][*][ ][*][ ][*][ ][*]
[*][ ][*][ ][*][ ][*][ ][*][ ][*][ ][*]

Notice there is a pattern on the positions that will have an space or an asterisk. Translate that into code.

On the last two, you can easily solve that using String.substring(), but chances are you won't be able to use that.

In that case, think of each new line as adding a new character to the previous one. There are several ways to know which character you should add next; one way would be using a string like BLUEJ and get a new character every iteration, which can be done using String.charAt()

NullUserException
A: 

You'll need two for loops, an outer loop for the rows, an inner loop for the columns. Next, you need a function, that takes a row and column value and calculates from those values whether you have to plot a star or not. The basic alogrithm goes like this (for the stars):

Plotter plotter = new FilledTreePlotter();

for (row = 0; row < maxRows; row++) {
  for (column = 0; column < maxColumns; column++ {
      System.out.print(plotter.getCellContent(row,column));
  }
  System.out.print("\n");
}

I've added some OO falvour to the basic algorithm. You'll need an additional interface

public interface Plotter {
  public char getCellContent(int row, int column);
}

and now you can implement a class that, for instance, can plot the filled triangle:

public FilledTreePlotter implements Plotter {
  public char getCellContent(int row, int column) {

    if ( ... ) {
      // your magic says, that this is a star
      return ('*');
    } else {
      return (' ');
    }

  }
}

Here is a demo implementation for another task, which will just plot a diagonal line:

public DiagonalPlotter implements Plotter {
  public char getCellContent(int row, int column) {

    if ( row == column ) {
      return ('*');
    } else {
      return (' ');
    }

  }
}
Andreas_D
If someone has problems with basic stuff like this, introducing interfaces is IMO not the best way to learn. Besides, printing/logic exercises as these don't really lend themselves for OO-design: put it all just inside a main-method (all IMO, of course!).
Bart Kiers
@Bart K - agree, but there is already an answer covering basic stuff ;) The advantage (for the experiences implementor of print/logic stuff) is, that you can reuse a lot of code and reduce the five homeworks to implementing a 'core' class and five subclasses that just concentrate on the challenging part ;) (and impress your teacher!)
Andreas_D
A: 

You need to study the patterns and try to work out how the number of spaces and stars changes with each line.

Let's look at the first one:

   *         (line 0)
  ***        (line 1)
 *****       (line 2)
*******      (line 3)

line 0: 3 spaces, 1 star
line 1: 2 spaces, 3 stars
line 2: 1 space , 5 stars
line 3: 0 spaces, 7 stars

Can you spot any patterns?

With each line, the number of spaces decrease by 1 and the number of stars increase by 2.

You can also see that for each line the:

line number + number of spaces = 3

and

number of stars = (2 * line number) + 1

This tells us how the number of spaces and stars in each line varies with line number. We can code it up like this:

int numLines = 4 ;
for(int i = 0 ; i < numLines ; i++){

    //print spaces
    for(int j = 0 ; j < 3-i ; j++){
        System.out.print(' ');
    }

    //print stars
    for(int j = 0 ; j < 2*i + 1; j++ ){
        System.out.print('*');
    }

    //print a new line
    System.out.println();
}

See if you can work out the rest yourself.

dogbane