Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Not sure what the Logic Error is? [help]

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Not sure what the Logic Error is? [help]

    Hi everyone,

    my friend Carissa is having problems with her computer science program, so i researched everything i could about it and i found this forum. I am not familiar with the "technical terms" of computer science, but i will give you all that she sent me. Any help/hints would be great.

    So here's what she said.

    " I have completed my assignment and I have no idea why the "Ants" in my program won't move.

    My assignment is: GOAL: Create a simple 2D simulation of ants crawling around. This is the first half of a predator-prey simulation described in the textbook as Project #4, p.519 (at the end of Chapter 8). The second assignment this semester will be to add the doodlebugs.
    The ants live in a world composed of a 20 x 20 grid of cells. Only one ant may occupy a cell at a time. The grid is enclosed, so an ant is not allowed to move off the edges of the grid. Time is simulated in time steps. Each ant performs some action every time step.
    The ants behave according to the following model:

    Move. Every time step, randomly try to move up, down, left, or right. If the cell in the selected direction is occupied or would move the ant off the grid, then the ant stays in the current cell.
    Breed. If an ant survives for three time steps (which all our ants will do in this first assignment since there are no doodlebugs to eat them), then at the end of the third time step (i.e. after moving), the ant will breed. This is simulated by creating a new ant in an adjacent (up, down, left, or right) cell that is empty. If there is no empty cell available, no breeding occurs. Once an offspring is produced, the ant cannot produce an offspring until three more time steps have elapsed.

    Write a program to implement this simulation and draw the world using ASCII characters (say "A" for an ant and "." for an empty cell).
    Initialize the world with 20 ants in random starting positions. After each time step, draw the world and prompt the user to press Enter to move to the next time step (like I did in HorseRacing).
    Hand-in: Submit your Java code and a sample run showing 5 or 6 time steps of a simulation using the dropbox on the class D2L site. Be sure your code contains comments saying who wrote it and why.


    Java Scripts:

    File: Ant

     /**
     * @author Carissa Roberts
     * @version 1.23.2013 
     * Assignment 1
     * 
     * Represents an 'Ant' and extends the Organism class
     * Overrides the breed, move, and toString methods from the Organism class.
     */
    public class Ant extends Organism
    {
        private int row;
        private int col;
        private int stepsSurvived;
     
        /**
         * Constructors call the super constructors (i.e. the constructor from the Organism class)
         * and initialize the stepsSurvived variable to zero
         */
        public Ant()
        {
            super();
            stepsSurvived = 0;
        }
     
        public Ant(World grid, int r, int c)
        {
            super(grid, r, c);
            stepsSurvived = 0;
        }
     
        /**
         * Spawns another Ant in an adjacent location if the stepsSurvived
         * is three and if the new placed location is null.
         * Then the stepsSurvived counter is reset to 0.
         */
        public void breed()
        {
            if(stepsSurvived == 3)
            {
                if((col > 0) && (world.getOrgAt(row, col-1) == null))
                {
                    Ant a = new Ant(world, row, col-1);
                    stepsSurvived = 0;
                }
                else if ((col < world.SIZE-1) && (world.getOrgAt(row, col+1) == null))
                {
                    Ant a = new Ant(world, row, col+1);
                    stepsSurvived = 0;
                }
                else if((row > 0) && (world.getOrgAt(row-1, col) == null))
                {
                    Ant a = new Ant(world, row - 1, col);
                    stepsSurvived = 0;
                }
                else if((row < world.SIZE-1) && world.getOrgAt(row+1, col) == null)
                {
                    Ant a = new Ant(world, row + 1, col);
                    stepsSurvived = 0;
                }
            }
            stepsSurvived = 0;
        }
     
        /**
         * returns a printable symbol to represent an Ant
         */
        public String toString()
        {
            return "A";
        }
     
        /**
         * Uses Math.random to randomly decide which direction an Ant will move.
         * If the cell in the selected direction is occupied or would move the ant off the grid, then the ant stays in the current cell. 
         */
        public void move()
        {
            if(row < world.SIZE)
            {
                if(col < world.SIZE)
                {
                    //generates a random double number to decide which direction to move
                    double x = Math.random();
                    //move up
                    if(x < 0.25)
                    {
                        if((row > 0) && (world.getOrgAt(row -1, col)== null))
                        {
                            world.setOrgAt(row-1, col, world.getOrgAt(row, col));
                            world.setOrgAt(row, col, null);
                            row--;
                        }
                    }
                    //move right
                    else if(x < 0.5)
                    {
                        if ((col < world.SIZE -1) && (world.getOrgAt(row, col + 1) == null))
                        {
                            world.setOrgAt(row, col + 1, world.getOrgAt(row, col));
                            world.setOrgAt(row, col, null);
                            col++;
                        }
                    }
                    //move down
                    else if ((x < 0.75) && (row < world.SIZE -1)&& (world.getOrgAt(row + 1, col)== null) )
                    {
                            world.setOrgAt(row+1, col, world.getOrgAt(row, col));
                            world.setOrgAt(row, col, null);
                            row++;
                    }
                    //move left
                    else 
                    {
                        if((col > 0)&& (row < world.SIZE - 1) && (world.getOrgAt(row, col -1) == null))
                        { 
                            world.setOrgAt(row, col-1, world.getOrgAt(row, col));
                            world.setOrgAt(row, col, null);
                            col--;
                        }
                    }
                }
                stepsSurvived++;
            }  
        }
     
        }

    File: World

     /**
     * @author Carissa Roberts
     * @version 1.23.2013 
     * Assignment 1
     * 
     * Represents the 'World" or the 'Grid' in which the Ants live, move, and breed.
     * 
     */
    import java.util.Scanner;
    import java.util.Random;
    public class World
    {
        /**
         * creates a grid and sets all of the spaces to null.
         */
        protected final static int SIZE = 20;    
        protected static Organism [][] land = new Organism[SIZE][SIZE];
        public World()
        {
            for (int r = 0; r < land.length; r++)
            {
                for(int c = 0; c < land[r].length; c++)
                {
                    land[r][c] = null;
                }
            }
        }
     
        /**
         * returns the Organism in the sent (parameter) location.
         * Returns null if the location is empty.
         */
        public Organism getOrgAt(int r, int c)
        {
            if((r >= 0) && (r < World.SIZE) && (c >= 0) && (c < World.SIZE))
            {
                if(land[r][c] == null)
                {
                    return null;
                }
                else
                {
                    return land[r][c];
                }
            }
            return null;
        }
     
        /**
         * places the the organism in the given location
         */
     
        public void setOrgAt(int r, int c, Organism organism)
        {
            if((r >= 0) && (r < World.SIZE) && (c >= 0) && (c < World.SIZE))
            {
                land[r][c] = organism;
            }
        }
     
        /**
         * Uses a for loop to display the grid. If there is nothing in the location a . is shown,
         * otherwise the correct toString() method is called.
         */
        public void display()
        {
            System.out.println("\n");
            for (int r = 0; r < land.length; r++)
            {
                for(int c = 0; c < land[r].length; c++)
                {
                    if(land[r][c] == null)
                    {
                        System.out.print(".");
                    }
                    else
                    {
                        System.out.print(land[r][c].toString());
                    }
                }
                System.out.println("");
            }
        }
     
        /**
         * runs through one step simulation by checking to move the ants and breed them
         */
        public void moveOneStep()
        {
            for (int r = 0; r < land.length; r++)
            {
                for(int c = 0; c < land[r].length; c++)
                {
                    if(land[r][c] != null)
                    {
                        land[r][c].setHasMoved(false);
                    }
                }
            }
     
            //check for ants and make them move if the boolean variable is false
            for(int r = 0; r < land.length; r++)
            {
                for(int c = 0; c < land[r].length; c++)
                {
                    if((land[r][c] != null) && (land[r][c].getHasMoved() == false))
                    {
                        land[r][c].move();
                        land[r][c].setHasMoved(true);
                    }
                }
            }
     
            //check the breed method, and breed after the ants have moved.
            for(int r =0; r < land.length; r++)
            {
                for(int c = 0; c < land[r].length; c++)
                {
                    if((land[r][c] != null) && (land[r][c].getHasMoved() == true))
                    {
                        land[r][c].breed();
                    }
                }
            }
        }
     
        public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
            //creates a new World and Random object
            World grid = new World();
            Random random = new Random();
            System.out.println("Ant Simulation. Press Enter to simulate one time step. ");
            grid.display();
     
            /** 
             * Uses a for loop to initialize all ants and place them in the grid and in the Ant array
             */
            int tempRow;
            int tempCol; 
            Ant[] ants = new Ant[20];
            for(int numAntsAdded = 0; numAntsAdded <= 19; numAntsAdded++)
            {
                tempRow = random.nextInt(20);
                tempCol = random.nextInt(20);
                if(land[tempRow][tempCol] == null)
                {
                    ants[numAntsAdded] = new Ant(grid, tempRow, tempCol);
                }
                else
                {
                    //does not add an Ant or increment the counter if the location is already filled
                    numAntsAdded--;
                }
            }
     
            /**
             * Run through a couple simulations
             */
            String temp = keyboard.nextLine();
            grid.display();
     
            temp = keyboard.nextLine();
            grid.moveOneStep();
            grid.display();
     
            temp = keyboard.nextLine();
            grid.moveOneStep();
            grid.display();
     
            temp = keyboard.nextLine();
            grid.moveOneStep();
            grid.display();
     
            temp = keyboard.nextLine();
            grid.moveOneStep();
            grid.display();
     
            temp = keyboard.nextLine();
            grid.moveOneStep();
            grid.display();
     
            temp = keyboard.nextLine();
            grid.moveOneStep();
            grid.display();
        }
    }

    File: Organism

     /**
     * @author Carissa Roberts
     * @version 1.23.2013 
     * Assignment 1
     * 
     * An abstract class to represent an 'Organism' in the created grid 'World'.
     * 
     */
    public abstract class Organism
    {
        private int row;
        private int col;
        protected int stepsSurvived;
        protected boolean hasMoved;
        protected World world;
     
        /**
         * Organsim constructors initialize the variables.
         * The second constructor 
         * accepts a World object, a row, and a column for varaibles.
         */
        public Organism()
        {
            hasMoved = false;
            stepsSurvived = 0;
            row = 0;
            col = 0;
        }
     
        public Organism(World world, int row, int col)
        {
            this.world = world;
            hasMoved = false;
            stepsSurvived = 0;
            this.row = row;
            this.col = col;
            world.setOrgAt(row, col, this);
        }
     
        /**
         * accessor and mutator methods get and set the value of the hasMoved variable
         */
        public boolean getHasMoved()
        {
            return hasMoved;
        }
     
        public void setHasMoved(boolean hasMoved)
        {
            this.hasMoved = hasMoved;
        }
     
        /**
         * returns a printable symbol to represent an Organism
         */
        public String toString()
        {
            return "O";
        }
     
        /**
         * abstract methods to be overrided in extended classes
         */
        public abstract void move();
     
        public abstract void breed();
    }


    The programs associated with this is either Blue J or Eclipse.

    If someone could please take the time to help figure out what's wrong with her code, that would truly be great.

    Thanks in advance

    -Mitsuwa


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Not sure what the Logic Error is? [help]

    My experience has been that it is not satisfactory to help some one through a third party and in fact is pretty frustrating when we ask for more information and then you have to relay it to her, with all the delays and potential for miscommunication. If your friend is having problems, please ask her to post her questions here herself so we can communicate directly through here. Let's not have to deal with intermediaries.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure what the Logic Error is? [help]

    Quote Originally Posted by curmudgeon View Post
    My experience has been that it is not satisfactory to help some one through a third party and in fact is pretty frustrating when we ask for more information and then you have to relay it to her, with all the delays and potential for miscommunication. If your friend is having problems, please ask her to post her questions here herself so we can communicate directly through here. Let's not have to deal with intermediaries.
    I will give her the account. We tried making one for her earlier but honest to god, we kept getting an error " registration denied, this forum runs an active policy of not allowing spammers. Please contact us via the "contact us" page link if you believe this is an error."

    I don't know why her username wouldn't work when we entered one during registration, but when i entered my regular "forum username" it worked, maybe because i used a different email? I honestly don't know what went wrong.

    But you're right and i will give her the account info so she can be the primary account holder and there will be no "third party" interactions. Just her.

    I am a Psychology Major and a music minor, so I am nowhere suited to help her with all the terms/hints you guys may or may not provide. Thank you for pointing it out!

Similar Threads

  1. Can you help me find my logic error
    By michael305rodri in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 5th, 2012, 12:51 AM
  2. Logic Error in noughts and crosses game
    By eyesackery in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 15th, 2012, 07:40 AM
  3. Lotto Problem logic error
    By ippo in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 10th, 2012, 10:18 AM
  4. Serial Programming Logic Error???
    By bczm8703 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 27th, 2011, 03:28 AM
  5. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 11:40 AM