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. (1 mark)
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 It's good practice to test as we go along, so at this point you can test what you have done so far. Open the OUWorkspace and execute: StickFigure sf = new StickFigure(); If all is well, sf should appear in the Variables pane and inspecting it should show that its instance variables are set correctly. This will create a StickFigure but nothing will appear yet in the Shapes Window, because the latter only displays a shape if it is referenced by a workspace variable. Now execute: Circlec = sf.getHead(); Triangle t = sf.getBody(); Rectangle r= sf.getLeg(); The component parts of the stick figure should now be visible, although they will all be squashed on top of one another in the top left-hand corner, as shown in Figure 3.
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.
(3 marks)
- 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);
(2 marks)
- iii. Write a public instance method moveBy() whose first argument is an intrepresenting the distance to move and whose second argument is a charrepresenting the direction in which to move. It should return no value.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started