Question
Java. 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
Java.
- 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.
public class StickFigure { /*Instance variables*/ private int xPos;//The horizontal position of a StickFigure private int yPos;//The vertical position of a StickFigure //add your declarations here for part (a)(i) 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 whit no argument and no returning value */ public void alignBody() { return this.alignBody; } /** * 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
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