LinearPersonjava please help me the JAVA program A LinearPer

LinearPerson.java

please help me the JAVA program

A LinearPerson lives on a number line. At any time, he or she has a position, such as 0 or 3 or -5. He or she can move one position at a time. He or she has a direction of movement. So if a LinearPerson is moving right and is at position -3, the new position will be -2. If a LinearPerson is moving left and is at position -3, the new position will be -4. The following describes the LinearPerson class: You should decide the instance variables needed for LinearPerson. Overloaded constructors: a no-argument constructor that sets the current position at 0 and the current direction as \"to the right.\" a constructor that takes one int as a parameter; the parameter represents the initial position of the LinearPerson methods: public void turn()//changes the direction of the LinearPerson (right to left, or left to right) public void move()//moves the LinearPerson one position in his or her current direction public int getPosition()//returns the current position of the LinearPerson As an example LinearPerson sophie = new LinearPerson();//sophie is at position 0, moving right sophie.turn();//sophie is at position 0, moving left sophie.move();//sophie is at position -1, moving left sophie.move();//sophie is at position -2, moving left sophie.turn();//sophie is at position -2, moving right sophie.move();//sophie is at position -1, moving right Create a class LinearPersonPairthat creates two LinearPerson objects, one using the no-argument constructor, the other object should be created at a given location. The program moves the objects in various directions and prints their final locations.

Solution

public class LinearPerson

{

private int curr_pos;

private char curr_dir;

LinearPerson()

{

curr_pos=0;

curr_dir=\'r\';

}

LinearPerson(int p)

{

curr_pos=p;

curr_dir=\'r\';

}

public void turn()

{

if(curr_dir==\'r\')curr_dir=\'l\';

else curr_dir=\'r\';

}

public void move()

{

if(curr_dir==\'r\')curr_pos=curr_pos+1;

else curr_pos=curr_pos-1;

}

public int getPosition()

{

return this.curr_pos;

}

}

public class LinearPersonPair

{

public static void main(String args[])

{

LinearPerson a=new LinearPerson();

LinearPerson b=new LinearPerson(3);

a.move();

b.move();

a.turn();

b.turn();

a.move();

b.move()

System.out.println(a.getPosition()+\"\ \"+b.getPosition());

}

}

LinearPerson.java please help me the JAVA program A LinearPerson lives on a number line. At any time, he or she has a position, such as 0 or 3 or -5. He or she
LinearPerson.java please help me the JAVA program A LinearPerson lives on a number line. At any time, he or she has a position, such as 0 or 3 or -5. He or she

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site