Java Program cant figure out. A LinearPerson lives on a number line. At any time
ID: 3809181 • Letter: J
Question
Java Program cant figure out. 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 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.
Explanation / Answer
LinearPerson.java
public class LinearPerson {
private int position;
// 1 means right
// -1 means left
private int direction;
LinearPerson()
{
this(0, 1);
}
LinearPerson(int position)
{
this(position, 1);
}
LinearPerson(int position, int direction)
{
this.position = position;
this.direction = direction;
}
public void turn()
{
direction = direction*(-1);
}
public void move()
{
if (direction == 1)
{
position++;
}
else
{
position--;
}
}
int getPosition()
{
return position;
}
}
LinearPersonPair.java
public class LinearPersonPair {
//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.
public static void main(String[] args)
{
LinearPerson person1 = new LinearPerson(); // Person1 is at position 0, moving right
LinearPerson person2 = new LinearPerson(10); // Person2 is at position 10, moving right
System.out.println("Person1's position: " + person1.getPosition());
System.out.println("Person2's position: " + person2.getPosition());
System.out.println("Both turn: ");
person1.turn();
person2.turn();
System.out.println("Both move: ");
person1.move();
person2.move();
System.out.println("Person1 position: " + person1.getPosition());
System.out.println("Person2 position: " + person2.getPosition());
System.out.println("Person 2 move:");
person2.move();
System.out.println("Person1 position: " + person1.getPosition());
System.out.println("Person2 position: " + person2.getPosition());
System.out.println("Person1 turn amd moved: ");
person1.turn();
person1.move();
System.out.println("Person1 position: " + person1.getPosition());
System.out.println("Person2 position: " + person2.getPosition());
}
}
Sample run
Person1's position: 0
Person2's position: 10
Both turn:
Both move:
Person1 position: -1
Person2 position: 9
Person 2 move:
Person1 position: -1
Person2 position: 8
Person1 turn amd moved:
Person1 position: 0
Person2 position: 8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.