Question: a. i.So far, StickFigure has only two instance variables, xPos and yPos, which hold the x- and y-coordinates in the Shapes Window of the displayed

a.

  • i.So far, StickFigure has only two instance variables, xPos and yPos, which hold the x- and y-coordinates in the Shapes Window of the displayed StickFigure. Add declarations for three further instance variables head, body and leg that will reference instances of Circle, Triangle and Rectangle respectively. Then you can uncomment the provided getter methods for head, body and leg as well as alignHead(), as they should now compile correctly.

    ii.Complete the constructor for StickFigure so that it:

  • initialises head to an instance of Circle with diameter 30 and colour OUColour.PINK

    initialises body to an instance of Triangle with width and height 50 and colour OUColour.RED

    initialises leg to a Rectangle of width 6, height 50 and colour OUCOLOUR.PINK

b.Your task in this part is to write methods which align the various body parts of the StickFigure.

Look at Figure 4 below. Note that the value of the x position in the Shapes Window increases from left to right, but the value of the y position increases from top to bottom. So the x and y positions of a Circle, Triangle or Rectangle tucked into the top left hand corner would all be 0.

Recall that, as stated earlier, the head and leg are to be aligned with the body of the figure.

Consider the provided method alignHead(), which aligns the head relative to the xPos and yPos of the body of the stick figure so it is centred immediately above the body. Make sure you understand how it works.

Writing alignBody() is rather simpler as the body is simply placed at the coordinates of the stick figure itself, but alignLeg() will take a little more thought.

  • i.Write a public instance method alignBody() that takes no arguments and returns no value. It should set the xPos and yPos of the body to the xPos and yPos of the StickFigure.
  • ii.Write a public instance method alignLeg() that takes no argument and returns no value. It should set the xPos and yPos of the leg so it is centred immediately below the body of the StickFigure.
  • iii. The class already has methods to set the xPos and yPos of the StickFigure. However, all they do at present is set the values of the respective instance variables. They do not move the StickFigure to the new location.

    Uncomment the provided alignAll() method and add a line of code to the constructor to call this method. Add similar lines to the provided setter methods setXPos() and setYPos().

  • iv.Execute code in the OUWorkspace as in part a(ii) and paste a screenshot of the StickFigure displayed in the Shapes Window into your Solution Document.

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!