SlideShare a Scribd company logo
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

More Related Content

PDF
most extra cellular environments contain a high concentration of sod.pdf
PDF
Let X be the prive of a meal at a local diner( X = $8,$10,and $12) a.pdf
PDF
is acute or chronic exposure to heptachlor more of a danger to human.pdf
PDF
In “Intellectual Property and the Information Age,” Richard T. De Ge.pdf
PDF
How does horizontal transfer affect the evolution of anti-biotic res.pdf
PDF
Flag this QuestionQuestion 12 1 ptsPolytene chromosomes arise fr.pdf
PDF
Genetic drift tends to genetic variation wITHIN small populations, an.pdf
PDF
For our discussion question, we focus on recent trends in security t.pdf
most extra cellular environments contain a high concentration of sod.pdf
Let X be the prive of a meal at a local diner( X = $8,$10,and $12) a.pdf
is acute or chronic exposure to heptachlor more of a danger to human.pdf
In “Intellectual Property and the Information Age,” Richard T. De Ge.pdf
How does horizontal transfer affect the evolution of anti-biotic res.pdf
Flag this QuestionQuestion 12 1 ptsPolytene chromosomes arise fr.pdf
Genetic drift tends to genetic variation wITHIN small populations, an.pdf
For our discussion question, we focus on recent trends in security t.pdf

More from alokkesh (20)

PDF
Fill in the blanksMicronutrients this refers to a sub-category o.pdf
PDF
ecourses.pvamu.edu Help English United States en us) Question4 Ma.pdf
PDF
Explain why several versions of nMOS transistor models and pMOS trans.pdf
PDF
Explain How Culture influences the way mangers perform their four ma.pdf
PDF
Describeillustrate the construction used by the ancient Indians to s.pdf
PDF
Describe the factors that contribute to a membrane potential. What i.pdf
PDF
DiARTHROIDAL joints can be classified based on their characteristics.pdf
PDF
Define and explain chemical equilibrium Include the defenition o.pdf
PDF
Cite the distinction between graphite and carbon. Carbon and graphit.pdf
PDF
C Language ProblemPlease Explain If int n1 = 5, and int d1 = 2, wh.pdf
PDF
According to Cisco design theory for a fully switched network, hosts.pdf
PDF
A saturated sample has a mass 0.69 kilograms when wet and 0.5 kg whe.pdf
PDF
Changes in the neutral or tan color of Christensen Urea agar broth to.pdf
PDF
A a randomly-chosen voter is a registered Democrat B they are regis.pdf
PDF
1.    Could the bailout by the Federal Reserve in 1998 and subsequen.pdf
PDF
14. (TCO 1-6) Please select the statement which CORRECTLY links a fo.pdf
PDF
Write a command to change all B and B in file foo.html to STRO.pdf
PDF
Which of the following statements about Stanley Miller’s experiment .pdf
PDF
1. Describe in a simplied way, the components and variables of the p.pdf
PDF
What is the role of agar in a nutrient medium Itprovides nutrients.pdf
Fill in the blanksMicronutrients this refers to a sub-category o.pdf
ecourses.pvamu.edu Help English United States en us) Question4 Ma.pdf
Explain why several versions of nMOS transistor models and pMOS trans.pdf
Explain How Culture influences the way mangers perform their four ma.pdf
Describeillustrate the construction used by the ancient Indians to s.pdf
Describe the factors that contribute to a membrane potential. What i.pdf
DiARTHROIDAL joints can be classified based on their characteristics.pdf
Define and explain chemical equilibrium Include the defenition o.pdf
Cite the distinction between graphite and carbon. Carbon and graphit.pdf
C Language ProblemPlease Explain If int n1 = 5, and int d1 = 2, wh.pdf
According to Cisco design theory for a fully switched network, hosts.pdf
A saturated sample has a mass 0.69 kilograms when wet and 0.5 kg whe.pdf
Changes in the neutral or tan color of Christensen Urea agar broth to.pdf
A a randomly-chosen voter is a registered Democrat B they are regis.pdf
1.    Could the bailout by the Federal Reserve in 1998 and subsequen.pdf
14. (TCO 1-6) Please select the statement which CORRECTLY links a fo.pdf
Write a command to change all B and B in file foo.html to STRO.pdf
Which of the following statements about Stanley Miller’s experiment .pdf
1. Describe in a simplied way, the components and variables of the p.pdf
What is the role of agar in a nutrient medium Itprovides nutrients.pdf

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Structure & Organelles in detailed.
PDF
Introduction-to-Social-Work-by-Leonora-Serafeca-De-Guzman-Group-2.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
STATICS OF THE RIGID BODIES Hibbelers.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Basic Mud Logging Guide for educational purpose
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Structure & Organelles in detailed.
Introduction-to-Social-Work-by-Leonora-Serafeca-De-Guzman-Group-2.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
Week 4 Term 3 Study Techniques revisited.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Microbial diseases, their pathogenesis and prophylaxis
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
human mycosis Human fungal infections are called human mycosis..pptx
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES

LinearPerson.javaPlease help me the JAVA programPlease provide t.pdf

  • 1. 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) {
  • 2. 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 } } }
  • 3. 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();
  • 4. 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