0

having a bit of difficulty with my code and very new to this so any help would be much appreciated.

Lets say I had a circle object which I could view in a shapes window, and I need it to move different ways, one step at a time, depending on the argument.

For example, lets say the circle had a Start position which was 0 (based on x pos) & the furtherst it could go is 15 - Furthest.

If the circle is at Furthest (position 15), it needs to move left back to Start (position 0) one step at a time.

Otherwise, it should move to Furthest (if its position is > 0, most likely to the right)

I am working with a few classes and within another class I can use methods right() and left() which are set to move the circle to the right or left by 1. I also have another method I can use called getPosition() to show where the circle currently is.

My if statement is as follows:

 if(circle.getPosition() == Furthest)
 // then a while/ for loop here - to move the circle LEFT back so Start (which is 0) one step at a 
 time
 OTHERWISE
 Circle should move to Furthest if the position is currently not furthest

Hopefully I have gave enough info, it's just the loop I'm having trouble with. I can provide any more code if needed but even just an example as to how to move an object using similar methods would be fab.

Thanks so much

Nicole
  • 53
  • 6
  • Have a look at some examples of a while loop. What you want to do is simply perform a `left()` until `getPosition() == 0`. In other words `while (circle.getPosition() != 0) { circle.left(); }` – lucidbrot Mar 09 '20 at 17:14
  • I have tried this, just under the if statement but the circle just jumps to 0 straight away :/ no steps in between – Nicole Mar 09 '20 at 17:21
  • By "move left back to Start (position 0) one step at a time.", do you mean every time you call the function, it should move one step towards Start? – MT756 Mar 09 '20 at 17:24
  • Yes, so it should move to 14, 13, 12 etc down to 0. If it is not at 15, it should move to 15 towards the right one step at a time. For example, if it was at 5, it should move to 6, 7, then 15 one at a time. – Nicole Mar 09 '20 at 17:27
  • So my method kind of goes: public static void move(Circle aCircle) { if(aCircle.getPosition() == Furthest) { while/forNOT SURE}{while/forNOT SURE} } – Nicole Mar 09 '20 at 17:31
  • 1
    @lucidbrot sorry forgot to tag – Nicole Mar 09 '20 at 17:37
  • @MT756 sorry didn't know I had to tag – Nicole Mar 09 '20 at 17:37
  • Add a breakpoint and step through your code with a debugger, and you will likely find that it does indeed do everything step by step. Just very fast. If you simply want to delay without other code running in that time, check out [this answer](https://stackoverflow.com/questions/24104313/how-do-i-make-a-delay-in-java) – lucidbrot Mar 09 '20 at 17:41
  • 1
    @ShanNicole Maybe I wasn't clear enough. Let's suppose you execute this if-else statement when the circle is at index 0. Where should the circle be after finishing the execution of the if-else statement? At index Furthest? or at index 1? That's the part i'm confused about "one step at a time" and "no steps in between". – MT756 Mar 09 '20 at 17:50
  • @MT756 the way I understood it is that the part in the else section is already covered and moves the circle in one step to `Furthest`. If the circle is already at `furthest`, it is moved back step by step using a while loop. – lucidbrot Mar 09 '20 at 18:16
  • @ShanNicole did you figure it out? If yes, it is totally acceptable to write an answer for your own question, so that future visitors with similar problems will be helped – lucidbrot Mar 10 '20 at 10:18
  • 1
    @lucidbrot hi, no I still haven't cracked it unfortunately. I think because the just under the if statement, I should have a for or a while loop and there are 2 different scenarios for the circle to move. I have also added a delay method and still just jumps from start to finish with no steps inbetween – Nicole Mar 10 '20 at 16:46
  • 1
    @lucidbrot I have got the first part working woohoo! Just as you said with a delay, turns out I didn't have != 0 in the loop. Now just trying to figure out how to move the circle to Furthest if it's position is not 15 – Nicole Mar 10 '20 at 16:50
  • @ShanNicole Should it jump to `Furthest` in one step? If yes, and assuming you can not do something like `circle.setPosition(15)`, you could use a for loop in the `else` part, to do `right` as often as you need – lucidbrot Mar 10 '20 at 20:50
  • @lucidbrot no, that should also be one step at a time. So same while loop only it should move right towards pos 15. Thanks so much! – Nicole Mar 10 '20 at 21:53
  • @ShanNicole well, then it's pretty much the same as the while loop I commented above. Instead of `left()`, you want it to go to the right, and of course the condition must be changed so that it will stop when it reaches `Furthest` instead of `0` – lucidbrot Mar 10 '20 at 22:08
  • @lucidbrot yep! I have another quick question if you have any advice. If I had two circles and I wanted to switch their positions and If I have used of other helper methods which could set and get their positions... my suggested code is: aCircle1.setPos(aCircle2.getPos()); aCircle2.setPos(aCircle1.getPos()); This code successfully switches the first circle but then the second remains the same as then the first circle will then be in the same position as the second already. I think I need to use a loop? Is there a really simple form? – Nicole Mar 14 '20 at 16:32
  • @ShanNicole store the circle1 position in a variable first. Then assign the circle1 as you did and then set the circle1 position to that variable – lucidbrot Mar 15 '20 at 14:49
  • @lucidbrot thanks! – Nicole Mar 15 '20 at 19:56

0 Answers0