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

Need help with this problem. Can someone help me please ? Thank you Simulators a

ID: 3572068 • Letter: N

Question

Need help with this problem. Can someone help me please ? Thank you

Simulators are programs that attempt to duplicate real life in software. For this question, you will use well-formed techniques and design patterns to construct a portion of a simulator. As in question 1, you will need to provide a readme.txt file making explicit the well-formed techniques and the design patterns you used to answer this question. One of the most fundamental parts of a simulator is the World and those items that populate the World. Often a simple way to represent a world is using a 2D array. This array is imagined to be the ground. Items populate this array. Some items are immoveable, others are moveable, and others move autonomously. We want to maximize good programming techniques to implement this simulator. You can use class, interface, and abstract when creating your objects. I am assuming you will use everything you have learned when designing and choosing your programming constructs. This includes inheritance, controls, and design patterns. You will need to do the following: World: It contains a 2D array that can store one Moveable, Immoveable, and Autonomous object per cell of the array. This World class has a public method called "void step()" that iterates once changing the state of the world by updating the position of all the Autonomous and Moveable objects. It also calls the method display() (see below). The World also as a "void add(item)" that is used to create the world by adding items to the array. The World constructor defines the size of the array. A third method "void display()" prints the array to the screen by calling the "char getToken()" method of the item. This permits the user to see state change. Display the world in an way that is easy to view. Immovable: An object with a string name describing what it is. It also has a character variable that stores a symbol that represents the item. The method "char getToken()" returns the character symbol. If you want a greater challenge, you can replace the symbol by a graphic picture. Moveable: is implemented exactly as Immovable, however it can be moved by one cell position if bumped into by an Autonomous object. It is displaced in the same direction as it was bumped. Autonomous: is implemented exactly like Moveable (bumped by a Moveable object or another Autonomous object causes it to shift one cell in the direction it was bumped). This object has a "void step()" method that randomly picks a direction and updates the symbol's array location to the new location by one cell. Construct a main method that builds a world and then runs a simulation for 100 iterations.

Explanation / Answer

World.java

import java.util.*;

public class World {
    private Item[][] store;
    private Autonomous[] automatas;
    private int height;
    private int width;
    private Random randomGenerator;

    /**
     * Constructor for the world
     * @param height of world
     * @param width of world
     * @param agentsQty # of diffrent agents
     */
    public World(int height, int width, int agentsQty) {
        if (height*width > agentsQty*3){
            throw new RuntimeException ( "Area indicant for given qty of agents" );
        }
        this.height = height;
        this.width = width;
        this.store = new Item[height][width];
        this.automatas = new Autonomous[agentsQty];
        for(int i =0 ; i < agentsQty; i++){
            Autonomous a = new Autonomous(this);
            this.add(new Immoveable());
            this.add(new Moveable(this));
            this.add(a);
            this.automatas[i] = a;
        }
    }

    public static void main(String[] args){
        World w = new World(5, 5, 2);
        System.out.println("Inital Display:");
        w.display();
        for (int i=1; i<101; i++){
            System.out.println(i+" Display:");
            w.step();
            w.display();
        }
    }

    /**
     * aggregator for all of our automatas
     */
    public void step(){
        for( Autonomous a: automatas){
            a.step();
        }
    }

    /**
     * randomly picks a location on the world to add a new item
     * if that spot is ocupied keep on picking a new location until
     * an open space is found
     * @param item to be added
     */
    private void add(Item item){
        randomGenerator = new Random();
        int x = randomGenerator.nextInt(this.width);
        int y = randomGenerator.nextInt(this.height);
        Item loc = this.store[y][x];
        while( loc != null){
            x = randomGenerator.nextInt(this.width);
            y = randomGenerator.nextInt(this.height);
            loc = this.store[y][x];
        }
        item.setX(x);
        item.setY(y);
        this.store[y][x] = item;
    }

    /**
     * moves the object in the array from current cords to the old cords
     * if new cords are ocupied move the item in the direction specified, this is a rec behavior
     * @param x current pos
     * @param y current pos
     * @param newX cordinate to be placed
     * @param newY cordinate to be placed
     * @param direction in whcich the move occures
     * @return fn has to be successful: true if successful: false if otherwise
     */
    public Boolean move(int x, int y, int newX, int newY, char direction){
        Item dest = store[newY][newX];
        // if somethign is trying to move into a new space swap it with null
        if (dest == null){
            Item temp = store[y][x];
            store[y][x] = null;
            store[newY][newX] = temp;
            return true;
        }
        else{
            //If the item in the space is not movable try to move it
            if( dest instanceof Moveable){
                Moveable toMove = (Moveable) dest;
                toMove.move(direction);
                store[newY][newX] = store[y][x];
                store[y][x] = null;
                return true;
            }
            return false;
        }
    }

    /**
     * makes sure we dont try to ove anyhting out side the world
     * @param x pos
     * @param y pos
     * @return valid position on the world
     */
    public Boolean isValidPosition(int x, int y){
        Boolean bottom,left,right,top;
        top = (height > y );
        bottom = (y > -1);
        left = (x > -1);
        right = (width > x);
        return bottom && left && right && top;
    }

    /**
     * prints all items in their place and a '_' if the position is empty
     */
    public void display(){
        String toPrint = "";
        for ( Item[] row : this.store){
            for (Item value: row){
                try {
                    toPrint+= value.getToken();
                }
                catch (NullPointerException e){
                    toPrint+= "_";
                }

            }
            System.out.println(toPrint);
            toPrint = "";
        }
    }
}


Autonomous.java

import java.util.Random;
public class Autonomous extends Moveable {
    private Random randomGenerator;
    private static char[] directions = {'N','W','E','S'};

    /**
     * default constructor
     * @param world
     */
    public Autonomous(World world){
        super("A", world);
    }

    /**
     * when called calls this instances move on a random direction
     */
    public void step(){
        randomGenerator = new Random();
        int x = randomGenerator.nextInt(4);
        this.move(directions[x]);
    }

}
/**/

Moveable.java

public class Moveable extends Item {

    /**
     * allows for customly named movable items
     * @param name
     * @param world
     */
    public Moveable(String name, World world){
        super(name, world);
    }

    /**
     * creates default named movable items
     * @param world
     */
    public Moveable(World world){
        super("M", world);
    }

    /**
     * given a direciton to move in, computes new x and y cords and calls the world method move
     * to move it, if successful update x and y
     * @param direction
     */
    public void move(char direction){
        int newX = x;
        int newY = y;
        switch (direction){
            case 'S':
                newY += 1;
                break;
            case 'W':
                newX -= 1;
                break;
            case 'E':
                newX += 1;
                break;
            case 'N':
                newY -= 1;
                break;
        }
        if(world.isValidPosition(newX,newY)){
            Boolean success = world.move(x,y,newX,newY,direction);
            if( success ){
                this.setY(newY);
                this.setX(newX);
            }
        }
    }
}


Item.java

public abstract class Item {
    private String name;
    protected World world;
    protected int x,y;

    /**
     * Constructor of an item
     * the x and y are lazzly eval'd
     * @param name of token
     */
    public Item(String name){
        this.name = name;
    }

    /**
     * Constructor of an item with a refrence to the world they occupy
     * @param name of token
     * @param world reffrence
     */
    public Item(String name, World world){
        this.name = name;
        this.world = world;
    }

    /**
     * @return token
     */
    public String getToken(){
        return name;
    }

    /**
     * sets x of the item
     * @param x
     */
    public void setX(int x) {
        this.x = x;
    }

    /**
     * sets y of the item
     * @param y
     */
    public void setY(int y) {
        this.y = y;
    }
}


Immoveable.java

public class Immoveable extends Item{
    /**
     * constructs an object that is not movable
     */
    public Immoveable(){
        super("I");
    }
}