SlideShare a Scribd company logo
1 of 4
Download to read offline
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

More from alokkesh

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

More from alokkesh (20)

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

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 

Recently uploaded (20)

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 

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