import becker.robots.*; /** A class of robot that can plant a field of things. * * @author Duncan Buell * * This program adapts the class Harvester of the text author. * **/ public class Planter extends RobotSE { /** Construct a new Planter robot. * @param aCity The city where the Planter will be created. * @param aStreet The Planter's initial street. * @param anAvenue The Planter's initial avenue. * @param aDirection The Planter's initial direction, one of {Direction.NORTH,SOUTH,EAST,WEST}. * @param numThings The number of things to be able to plant. **/ public Planter(City aCity,int aStreet,int anAvenue,Direction aDirection,int numThings) { super(aCity, aStreet, anAvenue, aDirection, numThings); } /** Go one row "down". If we started facing EAST, then we will move one row * SOUTH. **/ private void moveToNextRow() { this.turnRight(); this.move(); this.turnLeft(); } /** Plant a field of things. * The planting is assumed to start one space over in the current direction, * planting the appropriate number of "columns" and then moving to the right * to plant the next "row". * Thus, if starting pointing EAST, then we go across by columns and * then down by rows; this is the standard orientation, and if we start in * that orientation then we are planting in columns further to the EAST * and in rows further to the SOUTH. **/ public void plantAField(int numRows,int numCols) { boolean goodArguments; goodArguments = true; if(numRows < 0) { goodArguments = false; System.out.println("ERROR: numRows " + numRows + " is negative"); } if(numCols < 0) { goodArguments = false; System.out.println("ERROR: numCols " + numCols + " is negative"); } if(goodArguments == true) { System.out.println("Input parameters ok, continue"); this.move(); for(int i = 1; i <= numRows; ++i) { this.plantOneRow(numCols); this.returnToPosition(numCols); this.moveToNextRow(); } // for(int i = 1; i <= numRows; ++i) // // If we started out facing EAST, then we will have finished in the first // column of the planted field and one row SOUTH of the last row actually // planted. To return to the original starting position, we need to move // one column to the WEST, and then numRows to the NORTH. // this.returnToStartingPosition(numRows); } // if(goodArguments == true) else { System.out.println("ERROR: one or more parameter was invalid"); } } // public void plantAField(int numRows,int numCols) /** Plant one row of the field, returning to the same position we were * when the method was called. **/ private void plantOneRow(int numCols) { for(int i = 1; i <= numCols; ++i) { this.putThing(); this.move(); } } // private void plantOneRow(int numCols) /** Turn around, return to position, and then turn around again. **/ private void returnToPosition(int numCols) { this.turnAround(); for(int i = 1; i <= numCols; ++i) { this.move(); } this.turnAround(); } /** Return to the starting position from what is the last position after * planting. **/ private void returnToStartingPosition(int numRows) { this.turnAround(); this.move(); this.turnRight(); for(int i = 1; i <= numRows; ++i) { this.move(); } this.turnRight(); } } // public class Planter extends RobotSE