Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

LinearPerson A LinearPerson lives on a number line. At any time, he or she has a

ID: 3803846 • Letter: L

Question

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: the first using the no-argument constructor, the second should be created at a given location. The program moves the objects in various directions and prints their final locations.

Place the following code in main() to test your program.  

LinearPerson objFirst = new LinearPerson();

LinearPerson objSecond = new LinearPerson(5);

objFirst.turn();

objFirst.move();

objFirst.move();

objFirst.turn();

objFirst.move();

System.out.println("LinearPerson1 at " + objFirst.toString());

objSecond.turn();

objSecond.move();

objSecond.turn();

objSecond.move();

objSecond.turn();

objSecond.move();

System.out.println("LinearPerson2 at " + objSecond.toString());

Explanation / Answer

import java.util.*;

import java.lang.*;

import java.io.*;
class LinearPerson{
int position;
String direction;
public LinearPerson(){ //constructor
position=0;
direction="to the right";
}
public LinearPerson(int _position){ //constrctr with int value
position=_position;
direction="to the right";
}
public void turn() {// changes the direction of the LinearPerson (right to left, or left to right)
if (direction.equals("to the right")){
direction="to the left";
}
else
direction ="to the right";
}
public void move() // moves the LinearPerson one position in his or her current direction
{
if (direction.equals("to the right")){
position=position+1;
}
else
position=position-1;
}
public int getPosition() // returns the current position of the LinearPerson
{
return this.position;
  
}
public String toString()// returns a string which indicates current position and current direction
{
return this.getPosition()+" position and "+this.direction + " direction";
}
public static void main(String args[]){
/*LinearPerson lp = new LinearPerson();
System.out.println("LinearPerson1 at " + lp.toString());*/
LinearPerson objFirst = new LinearPerson();
LinearPerson objSecond = new LinearPerson(5);

objFirst.turn();
objFirst.move();
objFirst.move();
objFirst.turn();
objFirst.move();
System.out.println("LinearPerson1 at " + objFirst.toString());

objSecond.turn();
objSecond.move();
objSecond.turn();
objSecond.move();
objSecond.turn();
objSecond.move();
System.out.println("LinearPerson2 at " + objSecond.toString());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote