# how is the logic this program???

• 03-12-2012, 05:52 AM
riris
how is the logic this program???
hi all, i am new in java. i want to make a looping, when i input 1, it show this
Code:

```  _ / \ \_/```
and, when i input 2, it show this
Code:

```              _         _ /    \_         /  \ _ /  \         \ _/    \ _/             \ _ /```
how can i solve this??
• 03-13-2012, 01:50 AM
ozzyman
Re: how is the logic this program???

what happens if you input 3?

is it this:
Code:

```    _/  \__   /  \__/  \    \__/  \__/        /  \__/  \    \__/  \__/            \__/```
or this:
Code:

```    _/  \__  __/  \__/  \__  /  \__/  \__/  \      \__/  \__/  \__/    \__/  \__/            \__/```
or this:
Code:

```    _/  \__  __/  \__/  \__  /  \__/  \__/  \      \__/  \__/  \__/        /  \              \__/```
or something else? what is the pattern?
• 03-13-2012, 05:17 AM
riris
Re: how is the logic this program???
Quote:

Originally Posted by ozzyman

it will show this
Code:

```        _     _/  \__  __/  \__/  \__  /  \__/  \__/  \      \__/  \__/  \__/    \__/  \__/            \__/```

so, what is the logic of this??? how can i make it with java???
• 03-14-2012, 12:41 AM
ozzyman
Re: how is the logic this program???
So the number of hexagons sequence would be 1,4,9,16,25... at line n, total = n^2.
The extra hexagons on each new line would be n^2 - (n-1)^2, example:
at line 3 we have 3*3 = 9 hexagons in total,
but 9-(2*2) = 5 hexagons on that line

Using this info, we can make sure we draw the right amount of hexagons per line
Code:

```for (int i=0; i<n; i++) {     int numberHexagons = ( (n*n) - ((n-1)*(n-1)) );     //draw hexagons     for (int j=0; j<numberHexagons; j++) {         ...     } }```
To be able to draw each hexagon, we should have a Hexagon class with a method that is able to draw it.

Code:

```class Hexagon {     //default hexagon     static final String[] hexagon = { " __ ",                                       "/  \",                                       "\__/" };     //keep a copy of the number of hexagons     static int numberOfInstances = 0;     static public int getNumberOfHexagons() {         return numberOfInstances;     }     Hexagon() {         numberOfInstances++;         drawNext();     }     // draws the next hexagon depending on the number of instances     private void drawNext() {         ...     } }```
For this to work, drawNext() method will need to convert the hexagon array into a char[][] array and then be able to add it to a char[][] grid.

The output of this program so far would be something like this:
Code:

```        __         /  \     __ \__/ __     /  \/  \/  \  __ \__/\__/\__/ __ /  \/  \/  \/  \/  \ \__/\__/\__/\__/\__/```
So adjustments will need to be made to the positions of each hexagon when they are layed out onto the grid of char[][].

Code:

```        __         /  \__     __ \__/  \__     /  \/  \__/  \  __ \__/\__/  \__/ /  \/  \/  \__/ \__/\__/\__/```
• 03-16-2012, 06:38 PM
riris
Re: how is the logic this program???
here is my script, but i can not complete it. would anyone here help me????
Code:

```import javax.swing.JOptionPane; public class TestImages {     public static void main(String args[]) {                 int n;         n=Integer.valueOf(JOptionPane.showInputDialog("Input Diameter!"));                 TestImages test = new TestImages();         test.setInput(n);         String[][] Temp = test.building();         for (int i=0; i <= test.getZ(); i++) {             for (int j=0; j < test.getX(); j++) {                 if (Temp[i][j] != null) {                     System.out.print(Temp[i][j]);                 }                 else {                     System.out.print(" ");                 }             }             System.out.println();         }                 for(int i=0; i<n; i++){             for(int j=0; j<i; j++){                 System.out.print("  ");             }             for(int j=n; j>i; j--){                 System.out.print("\\_/ ");             }             System.out.print("\n");         }     }     private String[][] map;     private int input=0;     private int X;     private int Y;     private int Z;     private int midX;     private int midY;         public TestImages() {             }     public int getInput() {         return input;     }     public void setInput(int input) {         this.input = input;         this.X = (input*4)-1;         this.Y = (input*2)+1;         this.Z = input;         this.midX = (getX()/2);         this.midY = (getY()/2);         this.map = new String[getY()][getX()];     }         public String[][] building() {         write(midX, 0, "_");         write(0, midY, "/");         write(X-1, midY, "\\");         if(input>1){             for(int a=1,b=0,c=1; a<input; a++){                 write(midX-(a+c),a,"_");                 write(midX-(a+b),a,"/");                 write(midX+(a+b),a,"\\");                 write(midX+(a+c),a,"_");                 b++;                 c++;             }                           for(int d=2, e=1, f=0, g=2, h=0; d<=input; d++){                 write((input*2)-e+f, d, "_");                 write((input*2)-g+f, d, "\\");                 write((input*2)-h+f, d, "/");                 e+=2;                 f+=4;                 g+=2;                 h+=2;                 }                     }         return map;     }         private void write(int x, int y, String Value) {         map[y][x] = Value;     }     public int getX() {         return X;     }     public int getY() {         return Y;     }         public int getZ() {         return Z;     } }```