LinearPerson A LinearPerson lives on a number line At any ti

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.

As an example


Create a class LinearPersonPair that 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{

   int direction;
   int position;

   LinearPerson(){
       direction = 1;
       position = 0;
   }

   LinearPerson(int _position){
       position = _position;
       direction = 1;
   }

   public void turn() {
   // changes the direction of the LinearPerson (right to left, or left to right)
       direction *= -1;
   }
   public void move(){
       // moves the LinearPerson one position in his or her current direction
       position += direction;
   }
   public int getPosition() {
       // returns the current position of the LinearPerson
       return position;
   }

  

}


public class LinearPersonPair{

  

   public static void main(String[] argv) {

      LinearPerson sophie = new LinearPerson();
      // sophie is at position 0, moving right
      System.out.println(\"soohie\'s current position is \" + sophie.getPosition());
       sophie.turn();
       // sophie is at position 0, moving left
       sophie.move();
       // sophie is at position -1, moving left
       System.out.println(\"soohie\'s current position is \" + sophie.getPosition());
       sophie.move();
       // sophie is at position -2, moving left
       System.out.println(\"soohie\'s current position is \" + sophie.getPosition());
       sophie.turn();
       // sophie is at position -2, moving right
       System.out.println(\"soohie\'s current position is \" + sophie.getPosition());
       sophie.move();
       // sophie is at position -1, moving right
       System.out.println(\"soohie\'s current position is \" + sophie.getPosition());


       LinearPerson mandy = new LinearPerson(5);
     
      System.out.println(\"mandy\'s current position is \" + mandy.getPosition());
       mandy.turn();
      
       mandy.move();
      
       System.out.println(\"mandy\'s current position is \" + mandy.getPosition());
       mandy.move();
      
       System.out.println(\"mandy\'s current position is \" + mandy.getPosition());
       mandy.turn();
         
       mandy.move();
      
       System.out.println(\"mandy\'s current position is \" + mandy.getPosition());
   }

}

Output :-

soohie\'s current position is 0
soohie\'s current position is -1
soohie\'s current position is -2
soohie\'s current position is -2
soohie\'s current position is -1
mandy\'s current position is 5
mandy\'s current position is 4
mandy\'s current position is 3
mandy\'s current position is 4

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 o
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 o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site