Question: java . iii. The class already has methods to set the xPos and yPos of the StickFigure. However, all they do at present is set
- java . 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.
-
- 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);
- 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.
public class StickFigure { /*Instance variables*/ private int xPos;//The horizontal position of a StickFigure private int yPos;//The vertical position of a StickFigure private Circle head; private Triangle body; private Rectangle leg;
/** * Constructor for objects of class StickFigure that * provides a default stick figure near the bottom left corner of the graphical display. * */ public StickFigure() { super(); this.head = new Circle(30,OUColour.PINK); this.body = new Triangle(50,50,OUColour.RED); this.leg = new Rectangle(6,50,OUColour.PINK); this.setXPos(25); //sets starting position towards bottom left of Shapes window this.setYPos(220); //part (b)(iii) } /** * Alignig the body with no argument and no returning value */ public void alignBody(){ this.body.setXPos(this.xPos); this.body.setYPos(this.yPos); } /** * Alignig the leg with no argument and no returning value */ public void alignLeg(){ this.leg.setXPos(this.body.getXPos()+(this.body.getWidth()-this.leg.getWidth())/2); this.leg.setYPos(this.body.getYPos()+this.leg.getHeight()); } /** * Sets the xPos of the receiver to the value of the argument. */ public void setXPos(int newPos) { this.xPos = newPos; //part (b)(iii) }
/** * Returns the xPos of the receiver. */ public int getXPos() { return this.xPos; }
/** * Sets the yPos of the receiver to the value of the argument. */ public void setYPos(int newPos) { this.yPos = newPos; //part (b)(iii) }
/** * Returns the yPos of the receiver. */ public int getYPos() { return this.yPos; } /**You will need to uncomment these methods when directed to*******/ /** * Returns a reference to the head of the receiver. */ public Circle getHead() { return this.head; } /** * Returns a reference to the body of the receiver. */ public Triangle getBody() { return this.body; } /** * Returns a reference to the leg of the receiver. */ public Rectangle getLeg() { return this.leg; }
/** * Aligns the head of the receiver relative to the xPos and yPos of the body. */ public void alignHead() { this.head.setXPos(this.body.getXPos() + (this.body.getWidth() - this.head.getDiameter())/2); this.head.setYPos(this.body.getYPos() - this.head.getDiameter()); } /** * Aligns all the body parts of the receiver to form the * StickFigure-type figure. */ public void alignAll() { this.alignBody(); this.alignHead(); this.alignLeg(); } // /** // * Shrink body so it appears to spin and change colour from red to yellow. // * Then it expands again before turning from yellow to red once more. // */ // public void spinBody() // { // int bodyWidth = this.body.getWidth(); // while (this.body.getWidth() > 0) // { // this.body.setWidth(this.body.getWidth() - 2); // this.body.setXPos(this.body.getXPos() + 1); // this.delay(20); // } //complete part (c)(ii) to flip the colour // while (// part (c)(ii) complete the while loop condition here to expand the body ) // { // this.body.setWidth(this.body.getWidth() + 2); // this.body.setXPos(this.body.getXPos() - 1); // this.delay(20); // } // } /*********************end methods that need uncommenting****************/ /** * method to delay action so effects are shown clearly in Shapes window */ public void delay(int ms) { try { Thread.sleep(ms); } catch(Exception e) { System.out.println("Problem in delay method"); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
