Results 1 to 5 of 5
  1. #1
    riris is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default 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
    PHP Code:
      _
    / \
    \_/
    and, when i input 2, it show this
    PHP Code:
                  _
             _ /    \_
            /  \ _ /   \
            \ _/    \ _/
                \ _ /
    how can i solve this??
    Last edited by riris; 03-12-2012 at 06:14 AM.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: how is the logic this program???

    it is hard to tell the sequence without some more info.

    what happens if you input 3?

    is it this:
    Java Code:
         _/  \__ 
       /  \__/  \   
       \__/  \__/      
       /  \__/  \   
       \__/  \__/      
          \__/
    or this:
    Java Code:
         _/  \__ 
     __/  \__/  \__   
    /  \__/  \__/  \      
    \__/  \__/  \__/   
       \__/  \__/      
          \__/
    or this:
    Java Code:
         _/  \__ 
     __/  \__/  \__   
    /  \__/  \__/  \      
    \__/  \__/  \__/   
          /  \         
          \__/
    or something else? what is the pattern?

  3. #3
    riris is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: how is the logic this program???

    Quote Originally Posted by ozzyman View Post

    it will show this
    Java Code:
            _
         _/  \__ 
     __/  \__/  \__   
    /  \__/  \__/  \      
    \__/  \__/  \__/   
       \__/  \__/      
          \__/
    so, what is the logic of this??? how can i make it with java???

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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
    Java 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.

    Java 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:
    Java Code:
             __
            /  \
         __ \__/ __
        /  \/  \/  \
     __ \__/\__/\__/ __
    /  \/  \/  \/  \/  \
    \__/\__/\__/\__/\__/
    So adjustments will need to be made to the positions of each hexagon when they are layed out onto the grid of char[][].

    Java Code:
             __
            /  \__
         __ \__/  \__
        /  \/  \__/  \
     __ \__/\__/  \__/
    /  \/  \/  \__/
    \__/\__/\__/

  5. #5
    riris is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: how is the logic this program???

    here is my script, but i can not complete it. would anyone here help me????
    Java 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;
        }
    }

Similar Threads

  1. Logic Help
    By Barbados in forum New To Java
    Replies: 6
    Last Post: 01-23-2012, 12:24 AM
  2. Need help on this program logic
    By cotemaria in forum New To Java
    Replies: 1
    Last Post: 11-12-2011, 10:14 PM
  3. need help in merging GUI and logic main program
    By lavender88 in forum NetBeans
    Replies: 2
    Last Post: 04-02-2011, 02:28 PM
  4. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 12:06 PM
  5. Separating program logic and GUI
    By loopingman in forum AWT / Swing
    Replies: 0
    Last Post: 12-08-2008, 03:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •