Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So we are creating an ecosystem. Organism is the superclass and deer and grass a

ID: 3624771 • Letter: S

Question

So we are creating an ecosystem. Organism is the superclass and deer and grass are the subclasses. Attached are the individual classes. Right now I can't get my deer class to compile. Not sure how to attach the code so I will paste it all here.

Deer class:

import java.util.List;
import java.util.Iterator;
import java.util.Random;

/**
* A simple model of a deer.
* Deers age, move, eat grasss, and die.
*
* @author Maintainer
* @version 2011.03.30
*/
public class Deer extends Organism
{
    // Characteristics shared by all deers (static fields).
   
    // The age at which a deer can start to breed.
    private static final int BREEDING_AGE = 4;
    // The age to which a deer can live.
    private static final int giveBirth=8;
    // The likelihood of a deer breeding.
    private static final double BREEDING_PROBABILITY = 1; //;
    // The maximum number of births.
    private static final int MAX_LITTER_SIZE = (1);
    // The food value of a single grass. In effect, this is the
    // number of steps a deer can go before it has to eat again.
    private static final int GRASS_FOOD_VALUE = (1);
    // A shared random number generator to control breeding.
    //private static final Random rand = Randomizer.getRandom();
    //The health of the deer
   
   
    // Individual characteristics (instance fields).
    // The deer's age.
    private int age;
    // The deer's health, which is increased by eating grass.
    private int health;

    /**
     * Create a deer. A deer can be created as a new born (age zero
     * ) or with a random age and health.
     *
     *
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Deer(Field field, Location location)
    {
        super(field, location);
       
            age = 0;
            health = 0;
    }
   
    /**
     * This is what the deer does most of the time: it hunts for
     * grasss. In the process, it might breed, die of hunger,
     * (or die of old age.???????)
     * @param field The field currently occupied.
     * @param newDeers A list to add newly born deers to.
     */
    public void act(List<Organism> newDeers)
    {
        incrementAge();
        decrementhealth();
        if(isAlive()) {
            giveBirth(newDeers);           
            // Move towards a source of food if found.
            Location location = getLocation();
            Location newLocation = findFood(location);
            if(newLocation == null) {
                // No food found - try to move to a free location.
                newLocation = getField().freeAdjacentLocation(location);
            }
            // See if it was possible to move.
            if(newLocation != null) {
                setLocation(newLocation);
            }
            else {
                // Overcrowding.
                setDead();
            }
        }
    }

    /**
     * Increase the age. (This could result in the deer's death??).
     */
    private void incrementAge()
    {
        age++;
       /** if(age > MAX_AGE) {
            setDead();
        }*/
    }
   
    /**
     * Make this deer lose health. This could result in the deer's death.
     */
    private void decrementhealth()
    {
        health--;
        if(health <= 0) {
            setDead();
        }
    }
   
    /**
     * Tell the deer to look for grass adjacent to its current location.
     * Only the first live grass is eaten.
     * @param location Where in the field it is located.
     * @return Where food was found, or null if it wasn't.
     */
    private Location findFood(Location location)
    {
        Field field = getField();
        List<Location> adjacent = field.adjacentLocations(getLocation());
        Iterator<Location> it = adjacent.iterator();
        while(it.hasNext()) {
            Location where = it.next();
            Object organism = field.getObjectAt(where);
            if(organism instanceof Grass) {
                Grass grass = (Grass) organism;
                if(grass.isAlive()) {
                    grass.setDead();
                   
                    if(health < 9) {
                       health =+ 2;}
                }
                    // Remove the dead grass from the field.
                    return where;
                }
            }
        }
        //return null;//
    }
   
    /**
     * Check whether or not this deer is to give birth at this step.
     * New births will be made into free adjacent locations.
     * @param newDeers A list to add newly born deers to.
     */
    private void giveBirth(List<Organism> newDeers)
    {
        // New deers are born into adjacent locations.
        // Get a list of adjacent free locations.
        Field field = getField();
        List<Location> free = field.getFreeAdjacentLocations(getLocation());
        int births = breed();
        for(int b = 0; b < births && free.size() > 0; b++) {
            Location loc = free.remove(0);
            Deer young = new Deer(false, field, loc);
            newDeers.add(young);
        }
    }
       
    /**
     * Generate a number representing the number of births,
     * if it can breed.
     * @return The number of births (may be zero).
     */
    private int breed()
    {
        int births = 0;
        if(canBreed() {
            births = rand.nextInt(MAX_LITTER_SIZE) = 1;
        }
        return births;
    }

    /**
     * A deer can breed if it has reached the breeding age.
     */
    private boolean canBreed()
    {
        (if (age % BREEDING_AGE = 0) {
       (if (health = 8) {
       return 1;
       } else {
       return 0;
       }
        } else {
         return 0;
        }
    }

Organism superclass

import java.util.List;

/**
* A class representing shared characteristics of the Ecosystems organisms.
*
* @author
* @version 2011.03.30
*/
public abstract class Organisms
{
    // Whether the organism is alive or not.
    private boolean alive;
    // The organisms' field.
    private Field field;
    // The organisms' position in the field.
    private Location location;
   
    /**
     * Create a new organism at location in field.
     *
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Organisms(Field field, Location location)
    {
        alive = true;
        this.field = field;
        setLocation(location);
    }
   
    /**
     * Make this organism act - that is: make it do
     * whatever it wants/needs to do.
     * @param newOrganisms A list to add new organisms to.
     */
    abstract public void act(List<Organisms> newOrganisms);

    /**
     * Check whether the organism is alive or not.
     * @return true if the organism is still alive.
     */
    public boolean isAlive()
    {
        return alive;
    }

    /**
     * Indicate that the organism is no longer alive.
     * It is removed from the field.
     */
    public void setDead()
    {
        alive = false;
        if(location != null) {
            field.clear(location);
            location = null;
            field = null;
        }
    }

    /**
     * Return the organisms location.
     * @return The organisms location.
     */
    public Location getLocation()
    {
        return location;
    }
   
    /**
     * Return the organisms field.
     * @return The organisms field.
     */
    public Field getField()
    {
        return field;
    }
   
    /**
     * Place the organism at the new location in the given field.
     * @param newLocation The organisms new location.
     */
    public void setLocation(Location newLocation)
    {
        if(location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }
   
}

Grass class

import java.util.List;
import java.util.Random;

/**
* A simple model of a grass.
* Grass does not age, it can move, reproduce, and die.
*
* @author maintainer
* @version 2011.03.31
*/
public class Grass extends Organism
{
    // Characteristics shared by all grass (static fields).

    // The age at which a grass can start to breed.
    private static final int BREEDING_AGE = 2;
    // The age to which a grass can live.
    private static final int MAX_AGE = //;
    // The likelihood of a grass breeding.
    private static final double BREEDING_PROBABILITY = //;
    // The maximum number of births.
    private static final int MAX_LITTER_SIZE = (1)//;
    // A shared random number generator to control breeding.
    private static final Random rand = Randomizer.getRandom();
   
    // Individual characteristics (instance fields).
   
    // The grass's age.
    private int age;

    /**
     * Create a new grass. A grass may be created with age
     * zero (a new born) or with a random age.
     *
     * @param randomAge If true, the grass will have a random age.
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Grass(boolean randomAge, Field field, Location location)
    {
       super(field, location);
       
            age = 0;
            health = 0
    }
   
    /**
     * This is what the grass does most of the time - it spreads.
     * Sometimes it will reproduce.
     * @param newGrass A list to add newly born grass to.
     */
    public void act(List<Organism> newGrass)
    {
        incrementAge();
        if(isAlive()) {
            giveBirth(newGrass);           
            // Try to move into a free location.
            Location newLocation = getField().freeAdjacentLocation(getLocation());
            if(newLocation != null) {
                setLocation(newLocation);
            }
            else {
                // Overcrowding.
                setDead();
            }
        }
    }

    /**
     * Increase the age.
     * This doesn't seem to matter since it doesn't age.
     */
    private void incrementAge()
    {
        age++;
        if(age > MAX_AGE) {
            setDead();
        }
    }
   
    /**
     * Check whether or not this grass is to reproduce at this step.
     * New reproductions will be made into free adjacent locations.
     * @param newGrass A list to add newly born grass to.
     */
    private void giveBirth(List<Organism> newGrass)
    {
        // New grass are born into adjacent locations.
        // Get a list of adjacent free locations.
        Field field = getField();
        List<Location> free = field.getFreeAdjacentLocations(getLocation());
        int births = breed();
        for(int b = 0; b < births && free.size() > 0; b++) {
            Location loc = free.remove(0);
            Grass young = new Grass(false, field, loc);
            newGrass.add(young);
        }
    }
       
    /**
     * Generate a number representing the number of births,
     * if it can breed.
     * @return The number of births (may be zero).
     */
    private int breed()
    {
        int births = 0;
        if(canBreed(){
            births = rand.nextInt(MAX_LITTER_SIZE) = 1;
        }
        return births;
    }

   
}

Explanation / Answer

I fixed all errors that do not require class Field or Location to compile. Note that I renamed Organisms to Organism to be consistent with other uses.

From the number of syntax errors it is clear you wrote the entire program before compiling once. This is bad practice, because 1) as you've seen you have a massive number of compile-time errors, and 2) when you do go back to fix them, you have to figure out what you were trying to do.

Instead, you should write your code in this manner:

a) Define each class; create stub methods (methods that contain no code, but simply return a value to be compilable). Compile after defining each class (i.e. after Organism, Grass, Deer, etc.)

b) Implement each method, compiling after or even several times throughout writing code for one implementation.

import java.util.List;
import java.util.Iterator;
import java.util.Random;

/**
* A simple model of a deer.
* Deers age, move, eat grasss, and die.
*
* @author Maintainer
* @version 2011.03.30
*/
public class Deer extends Organism
{
    // Characteristics shared by all deers (static fields).

    // The age at which a deer can start to breed.
    private static final int BREEDING_AGE = 4;
    // The age to which a deer can live.
    private static final int giveBirth=8;
    // The likelihood of a deer breeding.
    private static final double BREEDING_PROBABILITY = 1; //;
    // The maximum number of births.
    private static final int MAX_LITTER_SIZE = (1);
    // The food value of a single grass. In effect, this is the
    // number of steps a deer can go before it has to eat again.
    private static final int GRASS_FOOD_VALUE = (1);
    // A shared random number generator to control breeding.
    //private static final Random rand = Randomizer.getRandom();
    //The health of the deer


    // Individual characteristics (instance fields).
    // The deer's age.
    private int age;
    // The deer's health, which is increased by eating grass.
    private int health;

    /**
     * Create a deer. A deer can be created as a new born (age zero
     * ) or with a random age and health.
     *
     *
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Deer(Field field, Location location)
    {
        super(field, location);

            age = 0;
            health = 0;
    }

    /**
     * This is what the deer does most of the time: it hunts for
     * grasss. In the process, it might breed, die of hunger,
     * (or die of old age.???????)
     * @param field The field currently occupied.
     * @param newDeers A list to add newly born deers to.
     */
    public void act(List<Organism> newDeers)
    {
        incrementAge();
        decrementhealth();
        if(isAlive()) {
            giveBirth(newDeers);
            // Move towards a source of food if found.
            Location location = getLocation();
            Location newLocation = findFood(location);
            if(newLocation == null) {
                // No food found - try to move to a free location.
                newLocation = getField().freeAdjacentLocation(location);
            }
            // See if it was possible to move.
            if(newLocation != null) {
                setLocation(newLocation);
            }
            else {
                // Overcrowding.
                setDead();
            }
        }
    }

    /**
     * Increase the age. (This could result in the deer's death??).
     */
    private void incrementAge()
    {
        age++;
       /** if(age > MAX_AGE) {
            setDead();
        }*/
    }

    /**
     * Make this deer lose health. This could result in the deer's death.
     */
    private void decrementhealth()
    {
        health--;
        if(health <= 0) {
            setDead();
        }
    }

    /**
     * Tell the deer to look for grass adjacent to its current location.
     * Only the first live grass is eaten.
     * @param location Where in the field it is located.
     * @return Where food was found, or null if it wasn't.
     */
    private Location findFood(Location location)
    {
        Field field = getField();
        List<Location> adjacent = field.adjacentLocations(getLocation());
        Iterator<Location> it = adjacent.iterator();
        while(it.hasNext()) {
            Location where = it.next();
            Object organism = field.getObjectAt(where);
            if(organism instanceof Grass) {
                Grass grass = (Grass) organism;
                if(grass.isAlive()) {
                    grass.setDead();

                    if(health < 9) {
                       health =+ 2;}
                }
                    // Remove the dead grass from the field.
                    return where;
            }
        }
        return null;
    }

    /**
     * Check whether or not this deer is to give birth at this step.
     * New births will be made into free adjacent locations.
     * @param newDeers A list to add newly born deers to.
     */
    private void giveBirth(List<Organism> newDeers)
    {
        // New deers are born into adjacent locations.
        // Get a list of adjacent free locations.
        Field field = getField();
        List<Location> free = field.getFreeAdjacentLocations(getLocation());
        int births = breed();
        for(int b = 0; b < births && free.size() > 0; b++) {
            Location loc = free.remove(0);
            Deer young = new Deer(field, loc);
            newDeers.add(young);
        }
    }

    /**
     * Generate a number representing the number of births,
     * if it can breed.
     * @return The number of births (may be zero).
     */
    private int breed()
    {
        Random rand = new Random();

        int births = 0;
        if(canBreed()) {
            births = rand.nextInt(MAX_LITTER_SIZE);
        }
        return births;
    }

    /**
     * A deer can breed if it has reached the breeding age.
     */
    private boolean canBreed()
    {
       if (age % BREEDING_AGE == 0) {
           if (health == 8) {
                   return true;
           } else {
                   return false;
       }
        }
        else {
             return false;
        }
    }
}
/**
* A class representing shared characteristics of the Ecosystems organisms.
*
* @author
* @version 2011.03.30
*/
public abstract class Organism
{
    // Whether the organism is alive or not.
    private boolean alive;
    // The organisms' field.
    private Field field;
    // The organisms' position in the field.
    private Location location;

    /**
     * Create a new organism at location in field.
     *
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Organism(Field field, Location location)
    {
        alive = true;
        this.field = field;
        setLocation(location);
    }

    /**
     * Make this organism act - that is: make it do
     * whatever it wants/needs to do.
     * @param newOrganisms A list to add new organisms to.
     */
    abstract public void act(List<Organism> newOrganisms);

    /**
     * Check whether the organism is alive or not.
     * @return true if the organism is still alive.
     */
    public boolean isAlive()
    {
        return alive;
    }

    /**
     * Indicate that the organism is no longer alive.
     * It is removed from the field.
     */
    public void setDead()
    {
        alive = false;
        if(location != null) {
            field.clear(location);
            location = null;
            field = null;
        }
    }

    /**
     * Return the organisms location.
     * @return The organisms location.
     */
    public Location getLocation()
    {
        return location;
    }

    /**
     * Return the organisms field.
     * @return The organisms field.
     */
    public Field getField()
    {
        return field;
    }

    /**
     * Place the organism at the new location in the given field.
     * @param newLocation The organisms new location.
     */
    public void setLocation(Location newLocation)
    {
        if(location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }

}





/**
* A simple model of a grass.
* Grass does not age, it can move, reproduce, and die.
*
* @author maintainer
* @version 2011.03.31
*/
public class Grass extends Organism
{
    // Characteristics shared by all grass (static fields).

    // The age at which a grass can start to breed.
    private static final int BREEDING_AGE = 2;
    // The age to which a grass can live.
    private static final int MAX_AGE = 20;
    // The likelihood of a grass breeding.
    private static final double BREEDING_PROBABILITY = 0.5;
    // The maximum number of births.
    private static final int MAX_LITTER_SIZE = 1;
    // A shared random number generator to control breeding.
    private static final Random rand = new Random();

    // Individual characteristics (instance fields).

    // The grass's age.
    private int age;
    private int health;

    /**
     * Create a new grass. A grass may be created with age
     * zero (a new born) or with a random age.
     *
     * @param randomAge If true, the grass will have a random age.
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Grass(boolean randomAge, Field field, Location location)
    {
       super(field, location);

            age = 0;
            health = 0;
    }

    /**
     * This is what the grass does most of the time - it spreads.
     * Sometimes it will reproduce.
     * @param newGrass A list to add newly born grass to.
     */
    public void act(List<Organism> newGrass)
    {
        incrementAge();
        if(isAlive()) {
            giveBirth(newGrass);
            // Try to move into a free location.
            Location newLocation = getField().freeAdjacentLocation(getLocation());
            if(newLocation != null) {
                setLocation(newLocation);
            }
            else {
                // Overcrowding.
                setDead();
            }
        }
    }

    /**
     * Increase the age.
     * This doesn't seem to matter since it doesn't age.
     */
    private void incrementAge()
    {
        age++;
        if(age > MAX_AGE) {
            setDead();
        }
    }

    /**
     * Check whether or not this grass is to reproduce at this step.
     * New reproductions will be made into free adjacent locations.
     * @param newGrass A list to add newly born grass to.
     */
    private void giveBirth(List<Organism> newGrass)
    {
        // New grass are born into adjacent locations.
        // Get a list of adjacent free locations.
        Field field = getField();
        List<Location> free = field.getFreeAdjacentLocations(getLocation());
        int births = breed();
        for(int b = 0; b < births && free.size() > 0; b++) {
            Location loc = free.remove(0);
            Grass young = new Grass(false, field, loc);
            newGrass.add(young);
        }
    }

    /**
     * Generate a number representing the number of births,
     * if it can breed.
     * @return The number of births (may be zero).
     */
    private int breed()
    {

        int births = 0;
        if(canBreed()){
            births = rand.nextInt(MAX_LITTER_SIZE);
        }
        return births;
    }

    public boolean canBreed()
    {
        // stub
        return false;
    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote