LinearPersonjava Please help me the JAVA program Please prov

LinearPerson.java

Please help me the JAVA program

Please provide the output.

LinearPerson 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

class LinearPerson {

   private String direction; // current direction
   private int position; // current position
   public static final String RIGHT = \"right\";
   public static final String LEFT = \"left\";

   // no arg constructor
   public LinearPerson() {
       this.position = 0;
       this.direction = LinearPerson.RIGHT;
   }

   //arg constructor
   LinearPerson(int position) {
       this.position = position; // setting the initial position
       if (position >= 0) {
           this.direction = LinearPerson.RIGHT; // setting the direction to right if position is >=0
       } else {
           this.direction = LinearPerson.LEFT; // setting the direction to left if position is < 0
       }

   }

   //get the curent direction
   public String getDirection() {
       return direction;
   }

   // get the current position
   public int getPosition() {
       return position;
   }

   // method to turn the direction  
   public void turn(){
       if(this.direction==LinearPerson.RIGHT){
           this.direction=LinearPerson.LEFT; // set the direction to left if current direction is right
       }else{
           this.direction=LinearPerson.RIGHT; // set the direction to left if current direction is right
       }
   }
  
  
   //method to move the position in current direction
   public void move(){
       if(this.direction==LinearPerson.RIGHT){
           this.position=this.position+1; // move the position to right
       }else{
           this.position=this.position-1; // move the position to left
       }
   }
  
      

}

public class LinearPersonPair {

   /**
   * @param args
   */
   public static void main(String[] args) {
       LinearPerson sophie=new LinearPerson(); // make one object with no arg constructor
       System.out.println(\"Sophie is at postion \"+sophie.getPosition()+\", moving \"+sophie.getDirection());
       sophie.turn();
       System.out.println(\"Sophie is at postion \"+sophie.getPosition()+\", moving \"+sophie.getDirection());
       sophie.move();
       System.out.println(\"Sophie is at postion \"+sophie.getPosition()+\", moving \"+sophie.getDirection());
       sophie.move();
       System.out.println(\"Sophie is at postion \"+sophie.getPosition()+\", moving \"+sophie.getDirection());
       sophie.turn();
       System.out.println(\"Sophie is at postion \"+sophie.getPosition()+\", moving \"+sophie.getDirection());
       sophie.move();
       System.out.println(\"Sophie is at postion \"+sophie.getPosition()+\", moving \"+sophie.getDirection());
      
       System.out.println(\"----------------Making Another object-------------\");
      
       LinearPerson jack = new LinearPerson(-2); // make another object with arg constructor
       System.out.println(\"Jack is at postion \"+jack.getPosition()+\", moving \"+jack.getDirection());
       jack.move();
       System.out.println(\"Jack is at postion \"+jack.getPosition()+\", moving \"+jack.getDirection());
       jack.turn();
       System.out.println(\"Jack is at postion \"+jack.getPosition()+\", moving \"+jack.getDirection());
       jack.move();
       System.out.println(\"Jack is at postion \"+jack.getPosition()+\", moving \"+jack.getDirection());
      
      
   }

}

-------------------------------------------------------------------------------------------------------------------------------------------------

output :

Sophie is at postion 0, moving right
Sophie is at postion 0, moving left
Sophie is at postion -1, moving left
Sophie is at postion -2, moving left
Sophie is at postion -2, moving right
Sophie is at postion -1, moving right
----------------Making Another object-------------
Jack is at postion -2, moving left
Jack is at postion -3, moving left
Jack is at postion -3, moving right
Jack is at postion -2, moving right

LinearPerson.java Please help me the JAVA program Please provide the output. LinearPerson A LinearPerson lives on a number line. At any time, he or she has a po
LinearPerson.java Please help me the JAVA program Please provide the output. LinearPerson A LinearPerson lives on a number line. At any time, he or she has a po
LinearPerson.java Please help me the JAVA program Please provide the output. LinearPerson A LinearPerson lives on a number line. At any time, he or she has a po

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site