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

This is for my AP java class. Need a complete code solution that I can submit Fo

ID: 3768387 • Letter: T

Question

This is for my AP java class. Need a complete code solution that I can submit

For this exercise, you'll be developing a program that simulates an ecosystem. The user will enter the number of each type of organism (you check that this entry is reasonable), and the program randomly distributes these organisms on 12 by 12 grid (more than one organism can occupy a square). At each turn of the game (as triggered by the use clicking a button), the organisms, which have the properties identified below, check to see whether they will take certain actions, which are also specified below.

Each organism has:

Grass - each unit of grass can produce a new unit of grass every two turns (that is every other turn) - it reproduces in its own square or in an adjacent square (which direction it spreads, including its own square, is random) - it never ages - it only dies through a fire or being eaten - you can have a maximum of ten units of vegetable material (that is, grass and trees combined) on any given square - in the event of a fire, any unit of grass has a 75% chance of dying.

Trees - each tree can produce a new tree every five turns - it reproduces in its own square or in an adjacent square - it never ages - it only dies through a fire - you can have a maximum of ten units of vegetable material (that is, grass and trees combined) on any given square - if a tree wants to spread in a particular direction, it can eliminate the grass that is in that square if necessary to avoid violating the ten vegetable unit limitation - in the event of a fire, any unit of grass has a 60% chance of dying.

Deer - deer can reproduce once every four turns and they must be in perfect health (note, to simplify coding, we've made deer able to asexually reproduce) - deer are not affected by fire - each deer has health (up to a maximum of eight points - the deer starts with eight) based on eating; each turn that the deer eats (which it does whenever it can), its health goes up by one (until the maximum); if the deer has nothing to eat, its health goes down to eat - during any given move, first the deer should eat, then the deer may move (the deer moves to whatever square adjoining it has the most food, unless the square it is on has the most food); if there is no food in its own square or an adjacent square, the deer will move randomly - deer die once their health reaches zero.

When the program starts up, the position and how long since reproduction must be random (otherwise everything reproduces in lockstep).

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class PastureGUI extends JFrame implements ActionListener {

    private final ImageIcon II_UNKNOWN    = new ImageIcon("unknown.gif");
    private final ImageIcon II_EMPTY      = new ImageIcon("empty.gif");
    private final int       SCALE         = 30;
    private Engine          engine;

    private JLabel[][]      grid;
    private Map<Point, java.util.List<ImageIcon>> icons =
        new HashMap<Point, java.util.List<ImageIcon>>();

    private JLabel          clockLabel    = new JLabel("Time: 0");
    private JLabel          entitiesLabel = new JLabel("Entities: 0");
    private JButton         startButton   = new JButton("Start");
    private JButton         stopButton    = new JButton("Stop");
    private JButton         exitButton    = new JButton("Exit");

    private int height;
    private int width;
    int size = 0;

    /**
     * Creates a new instance of this class with the specified
     * settings for the pasture to display. */

    public PastureGUI(int width, int height, Engine engine) {
        this.height = height;
        this.width = width;

        this.engine = engine;
      
        setSize(width * SCALE, height * SCALE);
     
        startButton.addActionListener(this);
        stopButton.addActionListener(this);
        exitButton.addActionListener(this);
      
        JPanel buttons = new JPanel();
        buttons.setLayout(new GridLayout(1,5));
        buttons.add(clockLabel);
        buttons.add(entitiesLabel);
        buttons.add(startButton);
        buttons.add(stopButton);
        buttons.add(exitButton);

        JPanel field = new JPanel();
        field.setBackground(new Color(27,204,89));
        field.setLayout(new GridLayout(height, width));
        grid = new JLabel[width][height];

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                grid[x][y] = new JLabel(II_EMPTY);
                grid[x][y].setVisible(true);
                field.add(grid[x][y]);

            }
        }

        Container display = getContentPane();
        display.setBackground(new Color(27,204,89));
        display.setLayout(new BorderLayout());
        display.add(field,BorderLayout.CENTER);
        display.add(buttons,BorderLayout.SOUTH);

        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        exitButton.setEnabled(true);
      
        update();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
  

    /**
     * This method is called when an action event has occured and
     * carries out the correct actions depending on the event. In this
     * class, this means that someone has pressed any of the buttons
     * start, stop, or exit.
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == startButton) {
            startButton.setEnabled(false);
            stopButton.setEnabled(true);
            exitButton.setEnabled(true);
            engine.start();
        }
        else if (e.getSource() == stopButton) {
            startButton.setEnabled(true);
            stopButton.setEnabled(false);
            exitButton.setEnabled(true);
            engine.stop();
        }
        else if (e.getSource() == exitButton) {
            System.exit(0);
        }
    }

    /**
     * The method addEntity is called to notify the GUI that an entity
     * has been added to a position. The icon of the added entity is
     * displayed at the position.
     */

    public void addEntity(Entity e, Point p) {
        ImageIcon icon = e.getImage();

        java.util.List<ImageIcon> l = icons.get(p);
        if (l==null) {
            l = new ArrayList<ImageIcon>();
            icons.put(p, l);
        }
        l.add(icon);

        int x = (int)p.getX();
        int y = (int)p.getY();
        grid[x][y].setIcon(icon);
      
        size++;
    }

    public void moveEntity(Entity e, Point old, Point ny) {
        removeEntity(e, old);
        addEntity(e, ny);
    }

    /**
     * The method removeEntity is called to notify the GUI that an
     * entity has been removed from a position. One icon among the
     * icons of the remaining entities is displayed at the position.
     */


    public void removeEntity(Entity e, Point p) {
      
        ImageIcon icon0 = e.getImage();

        java.util.List<ImageIcon> l = icons.get(p);
        l.remove(icon0);

        ImageIcon icon;
        if (l.isEmpty()) icon = II_EMPTY ;
            else icon = l.get(0);
      
        int x = (int)p.getX();
        int y = (int)p.getY();
        grid[x][y].setIcon(icon);
      
        size--;
    }

    public void update() {

        clockLabel.setText("Time: " + engine.getTime());
        entitiesLabel.setText("Entities: " + size);
    }
}

import java.util.*;
import java.awt.Point;


/**
* A pasture contains sheep, wolves, fences, plants, and possibly
* other entities. These entities move around in the pasture and try
* to find food, other entities of the same kind and run away from
* possible enimies.
*/
public class Pasture {

    private int         width = 20;
    private int         height = 20;

    private int         dummys = 20;
    private int         wolves;
    private int         sheep;
    private int         plants;
    private int         fences;

    private Set<Entity> world =
        new HashSet<Entity>();
    private Map<Point, List<Entity>> grid =
        new HashMap<Point, List<Entity>>();
    private Map<Entity, Point> point
        = new HashMap<Entity, Point>();

    private PastureGUI gui;

    /**
     * Creates a new instance of this class and places the entities in
     * it on random positions.
     */
    public Pasture() {

        Engine engine = new Engine(this);
        gui = new PastureGUI(width, height, engine);

        /* The pasture is surrounded by a fence. Replace Dummy for
         * Fence when you have created that class */
        for (int i = 0; i < width; i++) {
            addEntity(new Dummy(this, false), new Point(i,0));
            addEntity(new Dummy(this, false), new Point(i, height - 1));
        }
        for (int i = 1; i < height-1; i++) {
            addEntity(new Dummy(this, false), new Point(0,i));
            addEntity(new Dummy(this, false), new Point(width - 1,i));
        }

        /*
         * Now insert the right number of different entities in the
         * pasture.
         */
        for (int i = 0; i < dummys; i++) {
            Entity dummy = new Dummy(this, true);
            addEntity(dummy, getFreePosition(dummy));
        }

        gui.update();
    }

    public void refresh() {
        gui.update();
    }

    /**
     * Returns a random free position in the pasture if there exists
     * one.
     *
     * If the first random position turns out to be occupied, the rest
     * of the board is searched to find a free position.
     */
    private Point getFreePosition(Entity toPlace)
        throws MissingResourceException {
        Point position = new Point((int)(Math.random() * width),
                                   (int)(Math.random() * height));
        int startX = (int)position.getX();
        int startY = (int)position.getY();

        int p = startX+startY*width;
        int m = height * width;
        int q = 97; //any large prime will do

        for (int i = 0; i<m; i++) {
            int j = (p+i*q) % m;
            int x = j % width;
            int y = j / width;

            position = new Point(x,y);
            boolean free = true;

            Collection <Entity> c = getEntitiesAt(position);
            if (c != null) {
                for (Entity thisThing : c) {
                    if(!toPlace.isCompatible(thisThing)) {
                        free = false; break;
                    }
                }
            }
            if (free) return position;
        }
        throw new MissingResourceException(
                  "There is no free space"+" left in the pasture",
                  "Pasture", "");
    }
  
          
    public Point getPosition (Entity e) {
        return point.get(e);
    }

    /**
     * Add a new entity to the pasture.
     */
    public void addEntity(Entity entity, Point pos) {

        world.add(entity);

        List<Entity> l = grid.get(pos);
        if (l == null) {
            l = new ArrayList<Entity>();
            grid.put(pos, l);
        }
        l.add(entity);

        point.put(entity,pos);

        gui.addEntity(entity, pos);
    }
  
    public void moveEntity(Entity e, Point newPos) {
      
        Point oldPos = point.get(e);
        List<Entity> l = grid.get(oldPos);
        if (!l.remove(e))
            throw new IllegalStateException("Inconsistent stat in Pasture");
        /* We expect the entity to be at its old position, before we
           move, right? */
              
        l = grid.get(newPos);
        if (l == null) {
            l = new ArrayList<Entity>();
            grid.put(newPos, l);
        }
        l.add(e);

        point.put(e, newPos);

        gui.moveEntity(e, oldPos, newPos);
    }

    /**
     * Remove the specified entity from this pasture.
     */
    public void removeEntity(Entity entity) {

        Point p = point.get(entity);
        world.remove(entity);
        grid.get(p).remove(entity);
        point.remove(entity);
        gui.removeEntity(entity, p);

    }

    /**
     * Various methods for getting information about the pasture
     */

    public List<Entity> getEntities() {
        return new ArrayList<Entity>(world);
    }
      
    public Collection<Entity> getEntitiesAt(Point lookAt) {

        Collection<Entity> l = grid.get(lookAt);
      
        if (l==null) {
            return null;
        }
        else {
            return new ArrayList<Entity>(l);
        }
    }


    public Collection<Point> getFreeNeighbours(Entity entity) {
        Set<Point> free = new HashSet<Point>();
        Point p;

        int entityX = (int)getEntityPosition(entity).getX();
        int entityY = (int)getEntityPosition(entity).getY();

        for (int x = -1; x <= 1; x++) {
            for (int y = -1; y <= 1; y++) {
            p = new Point(entityX + x,
                          entityY + y);
            if (freeSpace(p, entity))
                free.add(p);
            }
        }
        return free;
    }

    private boolean freeSpace(Point p, Entity e) {
                            
        List <Entity> l = grid.get(p);
        if ( l == null ) return true;
        for (Entity old : l )
            if (! old.isCompatible(e)) return false;
        return true;
    }

    public Point getEntityPosition(Entity entity) {
        return point.get(entity);
    }


    /** The method for the JVM to run. */
    public static void main(String[] args) {

        new Pasture();
    }


}

import java.util.*;
import javax.swing.Timer;
import java.awt.event.*;

/**
* The simulation is run by an internal timer that sends out a 'tick'
* with a given interval. One tick from the timer means that each
* entity in the pasture should obtain a tick. When an entity obtains
* a tick, this entity is allowed to carry out their tasks according
* to what kind they are. This could mean moving the entity, making
* the entity starve from hunger, or producing a new offspring.
*/

public class Engine implements ActionListener {
  
    private final int SPEED_REFERENCE = 1000; /* 1000 */
    private static int speed   = 10;
    private Timer      timer   = new Timer(SPEED_REFERENCE/speed,this);
    private int        time    = 0;

    private Pasture pasture;


    Engine (Pasture pasture) {
        this.pasture = pasture;
        this.speed = speed;
    }

    public void actionPerformed(ActionEvent event) {
   
        List<Entity> queue = pasture.getEntities();
        for (Entity e : queue) {
            e.tick();
        }
        pasture.refresh();
        time++;
    }

    public void setSpeed(int speed) {
        timer.setDelay(SPEED_REFERENCE/speed);
    }

    public void start() {
        setSpeed(speed);
        timer.start();
    }

    public void stop() {
        timer.stop();
    }

    public int getTime () {
        return time;
    }

}

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