Question

Java. i.Write a public instance method alignBody() that takes no arguments and returns no value. It...

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");
}
}


}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

Here alignBody() takes no argument and returns no value also.It is just setting the value of xPos and yPos by taking the values from 'body' of current instance.

2.

Here alignLeg() takes no argument and returns no value also.It is setting the value of leg position according to body position in (x,y) co-ordinate by applying a simple formula.

Add a comment
Know the answer?
Add Answer to:
Java. i.Write a public instance method alignBody() that takes no arguments and returns no value. It...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • If anyone could give me some guidance for these questions. I know i'm chancing my arm but its worth a shot.... Thanks in advance i.Write a private instance method cycleState() which takes no ar...

    If anyone could give me some guidance for these questions. I know i'm chancing my arm but its worth a shot.... Thanks in advance i.Write a private instance method cycleState() which takes no argument and returns no value. This method cycles the value of state, from 1 to 2, from 2 to 3, from 3 to 4 and from 4 back to 1 again. (Note that the value of 0 for state is not part of this cycle). ii.Write a...

  • If anyone could give me some guidance for these questions. I know i'm chancing my arm...

    If anyone could give me some guidance for these questions. I know i'm chancing my arm but its worth a shot.... Thanks in advance i.Write a private instance method cycleState() which takes no argument and returns no value. This method cycles the value of state, from 1 to 2, from 2 to 3, from 3 to 4 and from 4 back to 1 again. (Note that the value of 0 for state is not part of this cycle). ii.Write a...

  • Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Nod...

    Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...

  • We have written the following Node class: class Node { // instance variables private int value;...

    We have written the following Node class: class Node { // instance variables private int value; private Node next; // constructor public Node(int value) { this.value = value; } // get the 'next' variable public Node getNext() { return this.next; } // get the 'value' variable public int getValue() { return this.value; } // set the 'next' variable public void setNext(Node next) { this.next = next; } } TASK: Create a class called List that has the following properties: It...

  • Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major...

    Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [ ] Name: courseCreditArray Access modifier: private Data type: int [ ] Static variables Name: maximumNumberOfCourses Access modifier: private Data type: int Initial Value: 40 Constructor Name: CollegeDegree Access modifier: public Parameters: none (default constructor) Task: sets major to the empty string...

  • Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add...

    Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add an instance method deleteFront such that … Method deleteFront has no input. Method deleteFront returns … an empty Optional instance if the list is empty an Optional instance whose value is the integer that was deleted from the front of the list, otherwise Hints https://docs.oracle.com/javase/10/docs/api/java/util/Optional.html import java.util.Optional; public class IntegerLinkedList { private IntegerNode head ; private int numberOfItems ; public int getNumberOfItems() { return...

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • public class Car {    /* four private instance variables*/        private String make;   ...

    public class Car {    /* four private instance variables*/        private String make;        private String model;        private int mileage ;        private int year;        //        /* four argument constructor for the instance variables.*/        public Car(String make) {            super();        }        public Car(String make, String model, int year, int mileage) {        super();        this.make = make;        this.model...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • Need help writing Java code for this question. CircleFrame class is provided below. what happen after...

    Need help writing Java code for this question. CircleFrame class is provided below. what happen after u add the code and follow the requirement? It would be nice to be able to directly tell the model to go into wait mode in the same way that the model's notify method is called. (i.e. notify is called directly on the model, whereas wait is called indirectly through the suspended attribute.) Try altering the the actionPerformed method of the SliderListener innner class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT