AP comp sci help!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: AP comp sci help!

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    5

    AP comp sci help!

    hi! how do you make an object go backwards through an array? my class is doing an ap question called robot clears the hall. everything is working except for the fact that the object doesn't turn around to see if any thing isnt picked up. I need 9 moves and i keep getting 6.
    Here is my class defn:


    public class Robot
    {
    private int[] hall;
    private int pos; //current position (tile number) of Robot
    private boolean facingRight; //true means this Robot is facing right


    //Constructor
    public Robot (int[] hll, int ps, boolean fcngRgt)
    {
    hall = hll;
    pos = ps;
    facingRight = fcngRgt;
    }
    /**
    * @return String containing a row of the tile numbers and
    * Underneath a representation of the hallway
    * And underneath the hallway, either a < or a > showing the
    * Position of the robot
    * 0 1 2 3
    * _______________
    * | 1 | 1 | 2 | 2 |
    * ---------------
    * >
    */
    public String toString()
    {
    String returnStr = "\n ";
    for (int i = 0; i < hall.length; i++)
    returnStr += (i + " ");
    returnStr += "\n ";
    for (int j = 0; j < (4 * hall.length - 1); j++)
    returnStr += "_";
    returnStr += "\n| ";
    for (int k = 0; k < hall.length; k++)
    returnStr += (hall[k] + " | ");
    returnStr += "\n ";
    for (int l = 0; l < (4 * hall.length - 1); l++)
    returnStr += "-";
    returnStr += "\n ";
    for (int m = 0; m < pos; m++)
    returnStr += " ";
    if (facingRight)
    returnStr += ">";
    else
    returnStr += "<";
    returnStr += "\n";
    return returnStr;
    //thank you Mike Ross!
    }
    /**
    * postcondition: returns true if this Robot has a wall immediately in
    * front of it, so that it cannot move forward;
    * otherwise, returns false
    */
    public boolean forwardMoveBlocked()
    {
    if ((facingRight && pos ==hall.length-1))
    {
    return true;
    }
    else if(!facingRight && pos == hall[0])
    {
    return true;
    }
    else
    {
    return false;
    }

    }


    /**
    * if there are any items on the current tile, then one items is removed
    * if there are more item on current tile, then robot remains facing same direction
    * if no more item exist on current tile
    * if robot can move forward, it advances
    * otherwise it turns around without changing position
    */
    public void move()
    {
    if (hall[pos] > 0)
    hall[pos]--;

    if (hall[pos] > 0)
    {
    facingRight = facingRight;
    }


    if (hall[pos] == 0)
    {
    if (!forwardMoveBlocked())
    {
    pos ++;
    }
    else
    {
    facingRight = !facingRight;
    }

    }

    }
    /**
    * postcondition: returns true if the hallway contains no items;
    * otherwise returns false;
    */
    public boolean hallIsClear()
    {
    boolean noItems = true;
    for (int i =0; i< hall.length; i ++)
    {
    if (hall[i] == 0)
    {
    noItems = true;
    }
    else
    {
    noItems = false;
    }
    }
    return noItems;
    }
    /**
    * postcondition: no more items remain in the hallway.
    * inside your loop please have the robot print his new situation after each move
    */

    public int clearHall()
    {
    int count = 0;
    while(!hallIsClear())
    {
    move();
    count ++;
    System.out.println("Yes! I picked up a toy from this hallway!");
    }
    return count;
    }
    }

    driver:
    public class RobotTester
    {
    public static void main(String[] args)
    {
    int [] hall = {1, 1, 2, 2};
    Robot botOne = new Robot(hall, 0, true);
    System.out.println(botOne.clearHall());
    Robot bot1E = new Robot(hall, 1, true);
    System.out.println("Robot at index 1 of the hallway facing east: Is he blocked? " +
    bot1E.forwardMoveBlocked() + "\n" + bot1E);
    Robot bot2E = new Robot(hall, 2, true);
    System.out.println("Robot at index 2 of the hallway facing east: Is he blocked? " +
    bot2E.forwardMoveBlocked() + "\n" + bot2E);
    Robot bot3E = new Robot(hall, 3, true);
    System.out.println("Robot in at index 3 of the hallway facing east: Is he blocked? " +
    bot3E.forwardMoveBlocked() + "\n" + bot3E);
    Robot bot0W = new Robot(hall, 0, false);
    System.out.println("Robot at the beginning of the hallway facing west: Is he blocked? " +
    bot0W.forwardMoveBlocked() + "\n" + bot0W);
    Robot bot1W = new Robot(hall, 1, false);
    System.out.println("Robot at index 1 of the hallway facing west: Is he blocked? " +
    bot1W.forwardMoveBlocked() + "\n" + bot1W);
    Robot bot2W = new Robot(hall, 2, false);
    System.out.println("Robot at index 2 of the hallway facing west: Is he blocked? " +
    bot2W.forwardMoveBlocked() + "\n" + bot2W);
    Robot bot3W = new Robot(hall, 3, false);
    System.out.println("Robot in at index 3 of the hallway facing west: Is he blocked? " +
    bot3W.forwardMoveBlocked() + "\n" + bot3W);

    }

    }
    output:
    Yes! I picked up a toy from this hallway!
    Yes! I picked up a toy from this hallway!
    Yes! I picked up a toy from this hallway!
    Yes! I picked up a toy from this hallway!
    Yes! I picked up a toy from this hallway!
    Yes! I picked up a toy from this hallway!
    6
    Robot at index 1 of the hallway facing east: Is he blocked? false

    0 1 2 3
    _______________
    | 0 | 0 | 0 | 0 |
    ---------------
    >

    Robot at index 2 of the hallway facing east: Is he blocked? false

    0 1 2 3
    _______________
    | 0 | 0 | 0 | 0 |
    ---------------
    >

    Robot in at index 3 of the hallway facing east: Is he blocked? true

    0 1 2 3
    _______________
    | 0 | 0 | 0 | 0 |
    ---------------
    >

    Robot at the beginning of the hallway facing west: Is he blocked? true

    0 1 2 3
    _______________
    | 0 | 0 | 0 | 0 |
    ---------------
    <

    Robot at index 1 of the hallway facing west: Is he blocked? false

    0 1 2 3
    _______________
    | 0 | 0 | 0 | 0 |
    ---------------
    <

    Robot at index 2 of the hallway facing west: Is he blocked? false

    0 1 2 3
    _______________
    | 0 | 0 | 0 | 0 |
    ---------------
    <

    Robot in at index 3 of the hallway facing west: Is he blocked? false

    0 1 2 3
    _______________
    | 0 | 0 | 0 | 0 |
    ---------------
    <

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: AP comp sci help!

    I'm not 100% clear on what you are doing or why your main method keeps on creating new Robots but doesn't reset the values in the hall array which will be zeroed by the first Robot.
    Can you explain what you mean by needing 9 moves?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Feb 2013
    Posts
    5

    Re: AP comp sci help!

    hi! in the main method, im starting at index one of the array(i made a mistake in the botOne creation). because the robot goes toward the right, there is still one more toy that needs to be picked up. However, im not able to figure out how to make the robot turn around when it reaches the end of the hallway. what i need help with is making it turn around...

  4. #4
    Join Date
    Feb 2013
    Posts
    5

    Re: AP comp sci help!

    also, we need to show how the toString works... so thats why there is so many robots. im just focusing on botOne

  5. #5
    Join Date
    Feb 2013
    Posts
    5

    Re: AP comp sci help!

    Here's to make the code a bit easier to read..... i'll take out the unnessessary stuff
    Code:
    public class Robot 
    {
    private int[] hall;
    private int pos;	 //current position (tile number) of Robot
    private boolean facingRight;	//true means this Robot is facing right
    
    
    //Constructor
    public Robot (int[] hll, int ps, boolean fcngRgt)
    {
    hall = hll;
    pos = ps;
    facingRight = fcngRgt;
    }
    /**
    * @return String containing a row of the tile numbers and 
    * Underneath a representation of the hallway
    * And underneath the hallway, either a < or a > showing the 
    * Position of the robot
    * 0 1 2 3
    * _______________
    * | 1 | 1 | 2 | 2 |
    * ---------------
    * >
    */
    public String toString()
    {
    String returnStr = "\n ";
    for (int i = 0; i < hall.length; i++)
    returnStr += (i + " ");
    returnStr += "\n ";
    for (int j = 0; j < (4 * hall.length - 1); j++)
    returnStr += "_";
    returnStr += "\n| ";
    for (int k = 0; k < hall.length; k++)
    returnStr += (hall[k] + " | ");
    returnStr += "\n ";
    for (int l = 0; l < (4 * hall.length - 1); l++)
    returnStr += "-";
    returnStr += "\n ";
    for (int m = 0; m < pos; m++)
    returnStr += " ";
    if (facingRight)
    returnStr += ">";
    else
    returnStr += "<";
    returnStr += "\n";
    return returnStr;
    //thank you Mike Ross!
    }
    /**
    * postcondition: returns true if this Robot has a wall immediately in 
    * front of it, so that it cannot move forward;
    * otherwise, returns false
    */
    public boolean forwardMoveBlocked()
    {
    if ((facingRight && pos ==hall.length-1))
    {
    return true;
    }
    else if(!facingRight && pos == hall[0])
    {
    return true;
    }
    else
    {
    return false;
    }
    
    }
    
    
    /**
    * if there are any items on the current tile, then one items is removed
    * if there are more item on current tile, then robot remains facing same direction
    * if no more item exist on current tile 
    * if robot can move forward, it advances
    * otherwise it turns around without changing position
    */
    public void move()
    {
    if (hall[pos] > 0)
    hall[pos]--;
    
    if (hall[pos] > 0)
    {
    facingRight = facingRight;
    }
    
    
    if (hall[pos] == 0)
    {
    if (!forwardMoveBlocked())
    {
    pos ++;
    }
    else
    {
    facingRight = !facingRight; 
    }
    
    }
    
    }
    /**
    * postcondition: returns true if the hallway contains no items;
    * otherwise returns false;
    */
    public boolean hallIsClear()
    {
    boolean noItems = true;
    for (int i =0; i< hall.length; i ++)
    {
    if (hall[i] == 0)
    {
    noItems = true;
    }
    else
    {
    noItems = false;
    }
    }
    return noItems;
    }
    /**
    * postcondition: no more items remain in the hallway.
    * inside your loop please have the robot print his new situation after each move
    */
    
    public int clearHall()
    {
    int count = 0;
    while(!hallIsClear())
    {
    move();
    count ++;
    System.out.println("Yes! I picked up a toy from this hallway!");
    }
    return count;
    }
    }
    
    driver:
    public class RobotTester 
    {
    public static void main(String[] args) 
    {
    int [] hall = {1, 1, 2, 2};
    Robot botOne = new Robot(hall, 1, true);
    System.out.println(botOne.clearHall());
    
    
    }
    
    }
    output:
    Yes! I picked up a toy from this hallway!
    Yes! I picked up a toy from this hallway!
    Yes! I picked up a toy from this hallway!
    Yes! I picked up a toy from this hallway!
    Yes! I picked up a toy from this hallway!
    5

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: AP comp sci help!

    Code:
    /**
    * if there are any items on the current tile, then one items is removed
    * if there are more item on current tile, then robot remains facing same direction
    * if no more item exist on current tile 
    * if robot can move forward, it advances
    * otherwise it turns around without changing position
    */
    public void move()
    {
    if (hall[pos] > 0)
    hall[pos]--;
    
    if (hall[pos] > 0)
    {
    facingRight = facingRight;
    }
    
    
    if (hall[pos] == 0)
    {
    if (!forwardMoveBlocked())
    {
    pos ++;
    }
    else
    {
    facingRight = !facingRight; 
    }
    
    }
    Why does this code have no indentation? Consistent, correct indentation makes code much easier to read.
    What is the point of the second if statement? facingRight = facingRight; changes nothing so is pointless and just adds noise to the code.

    To your problem: You are always moving forward? ie pos++, this should be pos-- when you are facing left.

    Code:
    public boolean hallIsClear()
    {
    boolean noItems = true;
    for (int i =0; i< hall.length; i ++)
    {
    if (hall[i] == 0)
    {
    noItems = true;
    }
    else
    {
    noItems = false;
    }
    }
    return noItems;
    }
    Rather than doing this in the negative ie noItems = true it's far easier to understand if you do hasItems = true; and negate the return value or change the method to hallHasItems().
    BTW As soon as you have found an item you can break out of the for loop, there is no point in searching further as the hall can not be clear.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Feb 2013
    Posts
    5

    Re: AP comp sci help!

    thank you! i was able to figure it out... the errror was with the forward move blocked and a little with move and hall is clear. Anyways here is the code:
    Code:
    public class Robot 
    {
    	private int[] hall;
    	private int pos;				//current position (tile number) of Robot
    	private boolean facingRight;	//true means this Robot is facing right
    
    
    
    
    	//Constructor
    	public Robot (int[] hll, int ps, boolean fcngRgt)
    	{
    		hall = hll;
    		pos = ps;
    		facingRight = fcngRgt;
    	}
    	/**
    	* @return String containing a row of the tile numbers and 
    	* Underneath a representation of the hallway
    	* And underneath the hallway, either a < or a > showing the 
    	* Position of the robot
    	*    0   1   2   3
    	*   _______________
    	*  | 1 | 1 | 2 | 2 |
    	*   ---------------
     	*        >
     	*/
    	public String toString()
    	{
    		String returnStr = "\n  ";
    		  for (int i = 0; i < hall.length; i++)
    		   returnStr += (i + "   ");
    		  returnStr += "\n ";
    		  for (int j = 0; j < (4 * hall.length - 1); j++)
    		   returnStr += "_";
    		  returnStr += "\n| ";
    		  for (int k = 0; k < hall.length; k++)
    		   returnStr += (hall[k] + " | ");
    		  returnStr += "\n ";
    		  for (int l = 0; l < (4 * hall.length - 1); l++)
    		   returnStr += "-";
    		  returnStr += "\n  ";
    		  for (int m = 0; m < pos; m++)
    		   returnStr += "    ";
    		  if (facingRight)
    		   returnStr += ">";
    		  else
    		   returnStr += "<";
    		  returnStr += "\n";
    		  return returnStr;
    
    	}
    	/**
    	* postcondition:  returns true if this Robot has a wall immediately in 
    	* front of it, so that it cannot move forward;
    	* otherwise, returns false
    	*/
    	public boolean forwardMoveBlocked()
    	{
    		if ((facingRight && pos ==hall.length-1))
    		{
    			return true;
    		}
    		else if(!facingRight && pos == 0)
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}
    			
    	}
    	
    		
    	/**
    	* if there are any items on the current tile, then one items is removed
    	* if there are more item on current tile, then robot remains facing same direction
    	* if no more item exist on current tile 
    	* 		if robot can move forward, it advances
    	* 		otherwise it turns around without changing position
    	*/
    	public void move()
    	{
    		if (hall[pos] > 0)
    		{
    			hall[pos]--;
    		}
    		if (hall[pos]==0)
    		{
    			if (forwardMoveBlocked()==false && facingRight==true)
    				pos++;
    			else if (forwardMoveBlocked()==false && facingRight==false)
    				pos--;
    			else if (facingRight==true && forwardMoveBlocked()==true)
    				facingRight = false;
    			else if (facingRight==false && forwardMoveBlocked()==true)
    				facingRight = true;
    		}
    		
    	}
    	/**
    	* postcondition:  returns true if the hallway contains no items;
         	* 			otherwise returns false;
    	*/
    	
    		public boolean hallIsClear()
    		{
    			boolean noItems = true;
    			for (int i = 0; i<hall.length; i++)
    			{
    				if (hall[i]>0)
    				noItems = false;
    			}
    		return noItems;
    	}
    	/**
    	* postcondition:  no more items remain in the hallway.
    	* inside your loop please have the robot print his new situation after each move
    	*/
    
    
    public int clearHall()
    	{
    		int count = 0;
    		while(!hallIsClear())
    		{
    			move();
    			count ++;
    			System.out.println(this);
    		}
    		return count;
    	}
    }
    tester:
    [code]
    public class RobotTester
    {
    public static void main(String[] args)
    {
    int [] hall = {1, 1, 2, 2};
    Robot botOne = new Robot(hall, 1, true);
    System.out.println(botOne.clearHall());
    Robot bot1E = new Robot(hall, 1, true);
    System.out.println("Robot at index 1 of the hallway facing east: Is he blocked? " +
    bot1E.forwardMoveBlocked() + "\n" + bot1E);
    Robot bot2E = new Robot(hall, 2, true);
    System.out.println("Robot at index 2 of the hallway facing east: Is he blocked? " +
    bot2E.forwardMoveBlocked() + "\n" + bot2E);
    Robot bot3E = new Robot(hall, 3, true);
    System.out.println("Robot in at index 3 of the hallway facing east: Is he blocked? " +
    bot3E.forwardMoveBlocked() + "\n" + bot3E);
    Robot bot0W = new Robot(hall, 0, false);
    System.out.println("Robot at the beginning of the hallway facing west: Is he blocked? " +
    bot0W.forwardMoveBlocked() + "\n" + bot0W);
    Robot bot1W = new Robot(hall, 1, false);
    System.out.println("Robot at index 1 of the hallway facing west: Is he blocked? " +
    bot1W.forwardMoveBlocked() + "\n" + bot1W);
    Robot bot2W = new Robot(hall, 2, false);
    System.out.println("Robot at index 2 of the hallway facing west: Is he blocked? " +
    bot2W.forwardMoveBlocked() + "\n" + bot2W);
    Robot bot3W = new Robot(hall, 3, false);
    System.out.println("Robot in at index 3 of the hallway facing west: Is he blocked? " +
    bot3W.forwardMoveBlocked() + "\n" + bot3W);

    }

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: AP comp sci help!

    Code:
    		public boolean hallIsClear()
    		{
    			boolean noItems = true;
    			for (int i = 0; i<hall.length; i++)
    			{
    				if (hall[i]>0)
    				noItems = false;
    			}
    		return noItems;
    	}
    That's better but you still aren't breaking after you have found an item. It should be:
    Code:
    		public boolean hallIsClear()
    		{
    			boolean noItems = true;
    			for (int i = 0; i<hall.length; i++)
    			{
    				if (hall[i]>0)
    {
    				        noItems = false;
                                                         break;
                                                 }
    			}
    		return noItems;
    	}
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: AP comp sci help!

    Code:
    		public boolean hallIsClear()
    		{
    			boolean noItems = true;
    			for (int i = 0; i<hall.length; i++)
    			{
    				if (hall[i]>0)
    				noItems = false;
    			}
    		return noItems;
    	}
    That's better but you still aren't breaking after you have found an item. It should be:
    Code:
    		public boolean hallIsClear()
    		{
    			boolean noItems = true;
    			for (int i = 0; i<hall.length; i++)
    			{
    				if (hall[i]>0)
    				{
    					noItems = false;
    					break;
    				}
    			}
    		return noItems;
    		}
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center