Question

a derived class from Organism. You must complete the constructors, and the method definitions that were...

a derived class from Organism. You must complete the constructors, and the method definitions that were abstract methods in Organism.

/** Organism.java

* Definition for the Organism base class.

* Each organism has a reference back to the World object so it can move

* itself about in the world.

*/

public abstract class Organism {

   protected int x, y;       // Position in the world

   protected boolean moved;   // boolean to indicate if moved this turn

   protected int breedTicks;   // Number of moves since last breeding

   /** Reference to the world object so we can update its

      * grid when we move around in the world

      */

   protected World world = null;

   /** Abstract method to breed this organism. */

   public abstract void breed();

   /** Abstract method to move this organism. */

   public abstract void move();

   /** Abstract method to determine if this organism is starving this turn. */

   public abstract boolean starve();

   /** Abstract method to return a displayable symbol for this organism */

   public abstract String getPrintableChar();

  

   /** Empty Constructor */

   public Organism()   {

   }

   /** Argument constructor

* @param world - World in which this organism lives, moves

* @param x - Initial x coordinate location for this organism

* @param y - Initial y coordinate location for this organism

*/

   public Organism(World world, int x, int y) {

this.world = world;

       this.x=x;

         this.y=y;

          world.setAt(x, y, this);

   }

   /** Accessor for the Moved variable

      * @return boolean - TRUE if this organism has moved this turn.

      */

   public boolean getMoved() {

return moved;

   }

   public void setMoved(boolean moved)   {

this.moved = moved;

   }

}

/** Doodlebug.java

* This class defines a Doodlebug's attributes and behavior.

*/

public class Doodlebug extends Organism {

/** Constant defining the number of moves required before a Doodlebug

   * can breed. */

   public static final int DOODLEBREED = 4;  

/** Constant defining the number of moves a Doodlebug can make without

   * eating before it starves. */

   public static final int DOODLESTARVE = 8;  

/** Number of moves since last eating */

   private int starveTicks;      

   /** Empty Constructor

   */

    public Doodlebug() {

    // ITP 220 - complete this code

   }

/** Argument constructor

   * @param world - World grid on which this Doodlebug lives

   * @param x - Initial x coordinate position of this Doodlebug

   * @param y - Initial y coordinate position of this Doodlebug

   */

   public Doodlebug(World world, int x, int y) {

   // ITP 220 - complete this code

   }

   /** Method defining what happens when a Doodlebug moves.

   * This method must randomly search all adjacent locations for a cell

   * occupied by an Ant (it wants to move where it can eat).

   * If a cell with an Ant is not found, this Doodlebug moves as an Ant,

   * picking a single random location and not moving if this chosen

   * location is not valid or is occupied.

   */

   public void move() {

   // ITP 220 - complete this code

   }

   /** Method defining what happens when a Doodlebug breeds.

   * - This method must check that the Doodlebug is able to breed and

   * may reset it if the Doodlebug breeds successfully.

   * - The Doodlebug must search all locations around it randomly.

   */

   public void breed() {

   // ITP 220 - complete this code

   }

   /** Method to determine whether the Doodlebug is currently starving.

   * @return boolean - TRUE if the Doodlebug is starving

   */

   public boolean starve()   {

   // ITP 220 - complete this code

   }

   /** Returns a displayable character for this Doodlebug (represented by "X")

   */

   public String getPrintableChar()   {

       return "X";

   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Doodlebug extends Organism
{
        public static final int DOODLEBREED = 8;                // Ticks to breed
        public static final int DOODLESTARVE = 3;               // Ticks to starve

        private int starveTicks;                // Number of moves since last feeding

        public Doodlebug()
        {
                super();
                starveTicks = 0;
        }

        public Doodlebug(World world, int x, int y)
        {
                super(world,x,y);
                starveTicks = 0;
        }

        public void breed()     // Must define this since virtual
        {
                breedTicks++;
                if (breedTicks == DOODLEBREED)
                {
                        breedTicks = 0;
                        // Try to make a new ant either above, left, right, or down
                        if ((y>0) && (world.getAt(x,y-1)==null))
                        {
                                Doodlebug newDoodle = new Doodlebug(world, x, y-1);
                        }
                        else if ((y<World.WORLDSIZE-1) && (world.getAt(x,y+1)==null))
                        {
                                Doodlebug newDoodle = new Doodlebug(world, x, y+1);
                        }
                        else if ((x>0) && (world.getAt(x-1,y)==null))
                        {
                                Doodlebug newDoodle = new Doodlebug(world, x-1, y);
                        }
                        else if ((x<World.WORLDSIZE-1) && (world.getAt(x+1,y)==null))
                        {
                                Doodlebug newDoodle = new Doodlebug(world, x+1, y);
                        }
                }
        }

        public void move()      // Must define this since virtual
        {
                // Look in each direction and if a bug is found move there
                // and eat it.
                if ((y>0) && (world.getAt(x,y-1)!=null) &&
                                 (world.getAt(x,y-1) instanceof Ant))
                {
                        world.setAt(x,y-1,this);    // Move to spot
                        world.setAt(x,y,null);
                        starveTicks =0 ;                        // Reset hunger
                        y--;
                        return;
                }
                else if ((y<World.WORLDSIZE-1) && (world.getAt(x,y+1)!=null) &&
                                 (world.getAt(x,y+1) instanceof Ant))
                {
                        world.setAt(x,y+1,this);        // Move to spot
                        world.setAt(x,y,null);
                        starveTicks =0;                         // Reset hunger
                        y++;
                        return;
                }
                else if ((x>0) && (world.getAt(x-1,y)!=null) &&
                                 (world.getAt(x-1,y) instanceof Ant))
                {
                        world.setAt(x-1,y,this);    // Move to spot
                        world.setAt(x,y,null);
                        starveTicks =0 ;                        // Reset hunger
                        x--;
                        return;
                }
                else if ((x<World.WORLDSIZE-1) && (world.getAt(x+1,y)!=null) &&
                                 (world.getAt(x+1,y) instanceof Ant))
                {
                        world.setAt(x+1,y,this);    // Move to spot
                        world.setAt(x,y,null);
                        starveTicks =0 ;                        // Reset hunger
                        x++;
                        return;
                }


                // If we got here, then we didn't find food.  Move
                // to a random spot if we can find one.
                int dir = (int) (Math.random() * 4);
                // Try to move up, if not at an edge and empty spot
                if (dir==0)
                {
                        if ((y>0) && (world.getAt(x,y-1)==null))
                        {
                         world.setAt(x,y-1,world.getAt(x,y));  // Move to new spot
                         world.setAt(x,y,null);
                         y--;
                        }
                }
                // Try to move down
                else if (dir==1)
                {
                        if ((y<World.WORLDSIZE-1) && (world.getAt(x,y+1)==null))
                        {
                         world.setAt(x,y+1,world.getAt(x,y));  // Move to new spot
                         world.setAt(x,y,null);  // Set current spot to empty
                         y++;
                        }
                }
                // Try to move left
                else if (dir==2)
                {
                        if ((x>0) && (world.getAt(x-1,y)==null))
                        {
                         world.setAt(x-1,y,world.getAt(x,y));  // Move to new spot
                         world.setAt(x,y,null);  // Set current spot to empty
                         x--;
                        }
                }
                // Try to move right
                else
                {
                        if ((x<World.WORLDSIZE-1) && (world.getAt(x+1,y)==null))
                        {
                         world.setAt(x+1,y,world.getAt(x,y));  // Move to new spot
                         world.setAt(x,y,null);  // Set current spot to empty
                         x++;
                        }
                }
                starveTicks++;
        }

        public boolean starve()
        {
                return (starveTicks > DOODLESTARVE);
        }

        public String getPrintableChar()
        {
                return "X";
        }
}
Add a comment
Know the answer?
Add Answer to:
a derived class from Organism. You must complete the constructors, and the method definitions that were...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • Can you tell me if this is right please import java.lang.reflect.AnnotatedArrayType; /** * The class <b>Point</b>...

    Can you tell me if this is right please import java.lang.reflect.AnnotatedArrayType; /** * The class <b>Point</b> is a simple helper class that stares a 2 dimentional element on a grid * * @author Guy-Vincent Jourdan, University of Ottawa */ public class Point { public int dot, row, col;       // ADD YOUR INSTANCE VARIABLES HERE /**    * Constructor    *    * @param x    * the x coordinate    * @param y    * the y coordinate...

  • This week we want to build an inheritance hierarchy using targets. You now need to take...

    This week we want to build an inheritance hierarchy using targets. You now need to take the Target class we have built and create a subclass that inherits from it. You can do whatever you want here, but it should be obvious how you changed the target. Some examples might include adding an additional ellipse to the target to increase the number of levels or changing up the colors. Once you have come up with one way to change the...

  • Requirements:  Your Java class names must follow the names specified above. Note that they are...

    Requirements:  Your Java class names must follow the names specified above. Note that they are case sensitive. See our template below.  BankAccount: an abstract class.  SavingAccount: a concrete class that extends BankAccount. o The constructor takes client's firstname, lastname and social security number. o The constructor should generate a random 6-digit number as the user account number. o The initial balance is 0 and the annual interest rate is fixed at 1.0% (0.01).o The withdraw method signature...

  • // ====== FILE: Point.java ========= // package hw3; /** * A class to that models a...

    // ====== FILE: Point.java ========= // package hw3; /** * A class to that models a 2D point. */ public class Point { private double x; private double y; /** * Construct the point (<code>x</code>, <code>y</code>). * @param x the <code>Point</code>'s x coordinate * @param y the <code>Point</code>'s y coordinate */ public Point(double x, double y) { this.x = x; this.y = y; } /** * Move the point to (<code>newX</code>, <code>newY</code>). * @param newX the new x coordinate for...

  • On the Turtle class I need to make a method so turtle 0 and turtle 2...

    On the Turtle class I need to make a method so turtle 0 and turtle 2 to "kiss" or touch their heads. Everything that can be use is in the SimpleTurtle link. https://www2.cs.uic.edu/~i101/doc/SimpleTurtle.html public void turtleTricks() { int worldWidth = 800; int worldHeight = 600; Random random = new Random(); World earth = new World(worldWidth, worldHeight );//Make a big world for turtles.    Turtle[] turtles = new Turtle[5]; int xLocation, yLocation, angle; for ( int i = 0; i <...

  • All question base on Java Se 8 Consider the Top class i public abstract class Topí...

    All question base on Java Se 8 Consider the Top class i public abstract class Topí 2 private 3 protected String name; 4 public intx - 12; int age; e public Top(String name)[ this.namename age0; System.out.println(x); 10 12 public abstract void foo(String f); 13 Create a Middle class that extends the Top class. The class should have a single constructor that takes two input parameters: a String (for name) and int (for age). Your constructor should not have any redundant...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • class x { x () {} private void one () {} } public class Y extends...

    class x { x () {} private void one () {} } public class Y extends x { Y() {} private void two () { one(); } public static void main (String[] args) { new Y().two(); } } What changes will make this code compile? a. Adding the public modifier to the declaration of class x b. Adding the protected modifier to the x() constructor c. Changing the private modifier on the declaration of the one() method to protected d....

  • Consider the following class Point which represents a point in Cartesian coordinate system. The class has...

    Consider the following class Point which represents a point in Cartesian coordinate system. The class has two instance variables x and y which represent its position along the x-axis and y-axis. The constructor of the class initializes the x and y of the point with the specific value init; the method move() moves the point as specified by its parameter amount. Using the implementation of the class Point information and write its specification. The specification of the class includes the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT