/*
 * NamedBunny class: we adapted the Bunny class to have allow starting
 * position and number of carrots to be set through the constructor,
 * and also keep track of a name for each Bunny.
 * Lecture 18, Tue Mar 14 2006
 */
public class NamedBunny
{
    private int x;
    private int y;
    private int numCarrots;
    private String name;
    
    /** 
     * NamedBunny constructor
     * 
     * Note that we have used the same names for our formal parameters
     * to this method as we have for the class fields. Thus we have to
     * use the "this" in front of the class fields to distinguish them
     * from the parameters.

     * @param x initial x location
     * @param y initial y location
     * @param numCarrots initial number of carrots
     * @param name name of the bunny
     */
    public NamedBunny(int x, int y, int numCarrots, String name) 
    {
        this.x = x;
        this.y = y;
        this.numCarrots = numCarrots;
        this.name = name;
    }

    /* 
     * The code below would also be legal, and since the parameters
     * are named differently we don't need to use "this." before our
     * class fields. We could, but we don't have to.

    public NamedBunny(int myx, int myy, int mynumCarrots, String myname) 
    {
        x = myx;
        y = myy;
        numCarrots = mynumCarrots;
        name = myname;
    }

    * The code below is NOT correct. Although it will compile, the
    * local variables vanish when the method returns, and the class
    * fields are not set.

    public NamedBunny(int myx, int myy, int mynumCarrots, String myname) 
    {
        int x = myx;
        int y = myy;
        int numCarrots = mynumCarrots;
        String name = myname;
    }

    */

    public void hop(int direction) {
        if (numCarrots > 0)    {
            System.out.println("hop");
            if (direction == 12)
                y++;
            else if (direction == 3) 
		x++;
	    else if (direction == 6) 
		y--;
	    else 
		x--;
            numCarrots--;
        } else {
            System.out.println("This bunny can't hop");
        }
    }
    
    public void displayInfo()
    {
        System.out.println("This bunny is at position "+x+","+y);
        System.out.println("This bunny has "+numCarrots+" carrots remaining");
    }
 
    public static void main( String[] args )
    {
        System.out.println("Testing Peter");
        NamedBunny peter = new NamedBunny(3,6,1,"Peter");
        peter.displayInfo();
        peter.hop(12);
        peter.hop(12);
        peter.hop(9);
        peter.displayInfo();
        System.out.println("Testing Emily");
        NamedBunny emily = new NamedBunny(3,4,5,"Emily");
        emily.displayInfo();
        emily.hop(9);
        emily.hop(9);
        emily.hop(9);
        emily.hop(12);
        emily.hop(9);
        emily.hop(12);
        emily.displayInfo();

    }

}

