-2

I have been given the task of writing a segment of code using the becker.robots package and I must create a method called pickUpThings for a class which has a parameter for the number of Things to be picked up ie

public void pickUpThings(int thingsToBePickedUp)

I have figured out that i must create a segment of code which lets me use a while loop (i think)

while(this.countThingsInBackpack() < thingsToBePickedUp)
{
    //...
}

Assumptions:

  1. There will be either 0 or 1 things on each intersection.
  2. Enough 'things' will be laid out in front of the robot.

Also, the robot travels on the same intersection i.e. heading east for the entire coded procedure.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
user3223921
  • 1
  • 1
  • 5
  • 1
    Ahm. And what is the question? – petajamaja Jan 22 '14 at 15:34
  • I've never used becker.robots, but at a guess... Before you loop add something like Robot robot = new Robot(). Inside the loop add 'robot.pickup();' – Scott Allen Jan 22 '14 at 15:37
  • Ok there's more to it than than I guessed. Create your robot before the loop. Inside the loop, check that the front is clear. If so, move. if (robot.frontIsClear()){robot.move}. Outside of that if, add a new if. if (robot.canPickThing){robot.pickThing} – Scott Allen Jan 22 '14 at 15:42
  • Also, your example is not a do while. A do while goes like this "do{ // stuff inside loop }while (robot.countThingsInBackpack() < thingsToBePickedUp) – Scott Allen Jan 22 '14 at 15:43
  • Thanks Scott!! I will try this as it seems to make sense! It just wasnt clear to me while i was reading over the specification as to whether i needed to use an if statment or while statement! Thanks – user3223921 Jan 22 '14 at 15:46
  • The problem with that scott is that if there is nothing on the intersection to pick up then the program crashes. and i have to find a way that lets the robot only pick up 10 things. for example, if the parameter for the pickUpThings were to equal 10, then the robot would only be able to pickup 10 things then the code is finished. – user3223921 Jan 22 '14 at 15:55

1 Answers1

0

From the sounds of it your starting point is correct the logic should be along the lines of;

while (stuff in backpack is less than the stuff that needs to be picked up)
    if there is somthing on the intersection I am on then
        put it in backpack
    endif
    move forward
end while

As for how to put that in code would you be able to figure that out or would you like my to edit with something that would compile?

Melbz
  • 512
  • 4
  • 14
  • i shall implement this to my robot class so that i can attempt to compile it in my main class!! i will get back to you in 10 minutes of so! Thanks bro! – user3223921 Jan 22 '14 at 15:43
  • Apologies again! But i am still rather new to this java and i dont have much of a clue as to where the () and {} brackets must go between all this. here is the code copied as i have typed into JCreator. public void pickUpThings(int thingsToBePickedUp) { while(this.countThingsInBackpack() < thingsToBePickedUp) if (this.canPickThing();) this.pickThing(); else this.move(); } – user3223921 Jan 22 '14 at 15:51
  • Would it be possible for you to show something that may actually compile as i am struggling with the brackets @Funkotron_King – user3223921 Jan 22 '14 at 16:00
  • Thanks very much. Got it working perfectly and does exactly what i needed it to do! Very much appreciated! :) – user3223921 Jan 22 '14 at 16:06
  • Apologies for not adding something but I'm glad (and I'm sure it feels better) that you got it sorted on your own back :) – Melbz Jan 23 '14 at 09:21
  • Most certainly! Thanks very much! Still stuck on my third question though! Its my other question on my profile if you wouldn't mind taking a wee look. PLease and thanks! :) – user3223921 Jan 23 '14 at 13:47