Question: c.In this part you are going to begin to write code to animate the StickFigure. i.Write a public instance method move() that takes two integer

c.In this part you are going to begin to write code to animate the StickFigure.

  • i.Write a public instance method move() that takes two integer arguments representing the amount by which to change the values of the instance variables xPos and yPos. The method should return no value. It should make use of the provided method delay() to pause so that the effects of running the method repeatedly are visible, e.g.

    this.delay(20);

    pauses execution for 20 ms.

    Test your code by moving an instance of StickFigure and checking that it remains aligned.

  • ii.Explain with reference to formal and actual arguments and the objects in the Shapes window what happens when you execute the following code in the OUWorkspace:

    StickFigure sf = new StickFigure(); sf.move(0,-1);

  • iii. Write a public instance method moveBy() whose first argument is an int representing the distance to move and whose second argument is a char representing the direction in which to move. It should return no value.
  • Begin by declaring local int variables xInc and yInc and use the table above to set these for the appropriate increments depending on the supplied argument for direction.The method should then move the StickFigure the required distance and direction 1 step at a time, using a loop.

You will now use the methods you have written in part (c) to complete an animation.

  • i.Write a public instance method doSquareDance() that takes no arguments and returns no value. It should walk the StickFigure along the four sides of a square of sides 150 in an anticlockwise direction. This means beginning with right ('R'), then up ('U'), left ('L') and finally down ('D') so that the StickFigure returns to its starting position.

    Test your code to make sure it works as expected.

  • ii.A StickFigure is able to spin round so that it displays the colour of its reverse side. (See the example video.)

    Uncomment the provided method spinBody() which narrows the body in a loop till it has width 0, centring it on each iteration, before expanding it again to its original size.

    Add code to this method to:

    change the colour of the body to yellow if it is red, or red if it is yellow once it reaches its minimum width

    complete the second while loop condition to restore the stick figures body to its width at the start of the method

    You may assume in writing this method that the body of a StickFigure will always have a width that is an even number.

  • iii.Add code to doSquareDance() to call spinBody() when the StickFigure reaches the top right and bottom left corners and test your code again.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!