SlideShare a Scribd company logo
1 of 2
Download to read offline
LinearPerson.java
please help me the JAVA program 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
public class LinearPerson
{
private int curr_pos;
private char curr_dir;
LinearPerson()
{
curr_pos=0;
curr_dir='r';
}
LinearPerson(int p)
{
curr_pos=p;
curr_dir='r';
}
public void turn()
{
if(curr_dir=='r')curr_dir='l';
else curr_dir='r';
}
public void move()
{
if(curr_dir=='r')curr_pos=curr_pos+1;
else curr_pos=curr_pos-1;
}
public int getPosition()
{
return this.curr_pos;
}
}
public class LinearPersonPair
{
public static void main(String args[])
{
LinearPerson a=new LinearPerson();
LinearPerson b=new LinearPerson(3);
a.move();
b.move();
a.turn();
b.turn();
a.move();
b.move()
System.out.println(a.getPosition()+" "+b.getPosition());
}
}

More Related Content

More from fashioncollection2

What is the role of abiotic factors in the formation of biomesS.pdf
What is the role of abiotic factors in the formation of biomesS.pdfWhat is the role of abiotic factors in the formation of biomesS.pdf
What is the role of abiotic factors in the formation of biomesS.pdffashioncollection2
 
What information needs to be encoded in a loadstorebranchALU inst.pdf
What information needs to be encoded in a loadstorebranchALU inst.pdfWhat information needs to be encoded in a loadstorebranchALU inst.pdf
What information needs to be encoded in a loadstorebranchALU inst.pdffashioncollection2
 
What are the major terrestrial adaptations of plants1- List the F.pdf
What are the major terrestrial adaptations of plants1- List the F.pdfWhat are the major terrestrial adaptations of plants1- List the F.pdf
What are the major terrestrial adaptations of plants1- List the F.pdffashioncollection2
 
The total lung capacity of a patient is 5.5 liters. Find the patient.pdf
The total lung capacity of a patient is 5.5 liters. Find the patient.pdfThe total lung capacity of a patient is 5.5 liters. Find the patient.pdf
The total lung capacity of a patient is 5.5 liters. Find the patient.pdffashioncollection2
 
The insatiable demand for everything wireless, video, and Web-enable.pdf
The insatiable demand for everything wireless, video, and Web-enable.pdfThe insatiable demand for everything wireless, video, and Web-enable.pdf
The insatiable demand for everything wireless, video, and Web-enable.pdffashioncollection2
 
Technology is an ever-growing system that provides advantages and in.pdf
Technology is an ever-growing system that provides advantages and in.pdfTechnology is an ever-growing system that provides advantages and in.pdf
Technology is an ever-growing system that provides advantages and in.pdffashioncollection2
 
Suppose that the proportions of blood phenotypes in a particular pop.pdf
Suppose that the proportions of blood phenotypes in a particular pop.pdfSuppose that the proportions of blood phenotypes in a particular pop.pdf
Suppose that the proportions of blood phenotypes in a particular pop.pdffashioncollection2
 
Select all of the following that are true regarding evolution. Altho.pdf
Select all of the following that are true regarding evolution.  Altho.pdfSelect all of the following that are true regarding evolution.  Altho.pdf
Select all of the following that are true regarding evolution. Altho.pdffashioncollection2
 
Role of involvement in consumer decision-making. Identify the level .pdf
Role of involvement in consumer decision-making. Identify the level .pdfRole of involvement in consumer decision-making. Identify the level .pdf
Role of involvement in consumer decision-making. Identify the level .pdffashioncollection2
 
Questionsa What are some of the barriers to understanding an issu.pdf
Questionsa What are some of the barriers to understanding an issu.pdfQuestionsa What are some of the barriers to understanding an issu.pdf
Questionsa What are some of the barriers to understanding an issu.pdffashioncollection2
 
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdfRainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdffashioncollection2
 
Quality software project managementHow are tasks, activities, and .pdf
Quality software project managementHow are tasks, activities, and .pdfQuality software project managementHow are tasks, activities, and .pdf
Quality software project managementHow are tasks, activities, and .pdffashioncollection2
 
Part I Write the complete class definition (or server) for Unsorte.pdf
Part I Write the complete class definition (or server) for Unsorte.pdfPart I Write the complete class definition (or server) for Unsorte.pdf
Part I Write the complete class definition (or server) for Unsorte.pdffashioncollection2
 
OverviewThe Internet and World Wide Web (WWW) were originally two .pdf
OverviewThe Internet and World Wide Web (WWW) were originally two .pdfOverviewThe Internet and World Wide Web (WWW) were originally two .pdf
OverviewThe Internet and World Wide Web (WWW) were originally two .pdffashioncollection2
 
Nutrition Design a one day menu for a diabetic patient with pressur.pdf
Nutrition Design a one day menu for a diabetic patient with pressur.pdfNutrition Design a one day menu for a diabetic patient with pressur.pdf
Nutrition Design a one day menu for a diabetic patient with pressur.pdffashioncollection2
 
Java code to find the closest pair using divide and conquer algorith.pdf
Java code to find the closest pair using divide and conquer algorith.pdfJava code to find the closest pair using divide and conquer algorith.pdf
Java code to find the closest pair using divide and conquer algorith.pdffashioncollection2
 
In _________________, substances move from an area of high concentra.pdf
In _________________, substances move from an area of high concentra.pdfIn _________________, substances move from an area of high concentra.pdf
In _________________, substances move from an area of high concentra.pdffashioncollection2
 
Im confused on question 6,7 and 8 On what point(s) would Charles Dar.pdf
Im confused on question 6,7 and 8 On what point(s) would Charles Dar.pdfIm confused on question 6,7 and 8 On what point(s) would Charles Dar.pdf
Im confused on question 6,7 and 8 On what point(s) would Charles Dar.pdffashioncollection2
 
Histone deacylase modulate gene transcription. Which sentence is inco.pdf
Histone deacylase modulate gene transcription. Which sentence is inco.pdfHistone deacylase modulate gene transcription. Which sentence is inco.pdf
Histone deacylase modulate gene transcription. Which sentence is inco.pdffashioncollection2
 
Hi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdfHi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdffashioncollection2
 

More from fashioncollection2 (20)

What is the role of abiotic factors in the formation of biomesS.pdf
What is the role of abiotic factors in the formation of biomesS.pdfWhat is the role of abiotic factors in the formation of biomesS.pdf
What is the role of abiotic factors in the formation of biomesS.pdf
 
What information needs to be encoded in a loadstorebranchALU inst.pdf
What information needs to be encoded in a loadstorebranchALU inst.pdfWhat information needs to be encoded in a loadstorebranchALU inst.pdf
What information needs to be encoded in a loadstorebranchALU inst.pdf
 
What are the major terrestrial adaptations of plants1- List the F.pdf
What are the major terrestrial adaptations of plants1- List the F.pdfWhat are the major terrestrial adaptations of plants1- List the F.pdf
What are the major terrestrial adaptations of plants1- List the F.pdf
 
The total lung capacity of a patient is 5.5 liters. Find the patient.pdf
The total lung capacity of a patient is 5.5 liters. Find the patient.pdfThe total lung capacity of a patient is 5.5 liters. Find the patient.pdf
The total lung capacity of a patient is 5.5 liters. Find the patient.pdf
 
The insatiable demand for everything wireless, video, and Web-enable.pdf
The insatiable demand for everything wireless, video, and Web-enable.pdfThe insatiable demand for everything wireless, video, and Web-enable.pdf
The insatiable demand for everything wireless, video, and Web-enable.pdf
 
Technology is an ever-growing system that provides advantages and in.pdf
Technology is an ever-growing system that provides advantages and in.pdfTechnology is an ever-growing system that provides advantages and in.pdf
Technology is an ever-growing system that provides advantages and in.pdf
 
Suppose that the proportions of blood phenotypes in a particular pop.pdf
Suppose that the proportions of blood phenotypes in a particular pop.pdfSuppose that the proportions of blood phenotypes in a particular pop.pdf
Suppose that the proportions of blood phenotypes in a particular pop.pdf
 
Select all of the following that are true regarding evolution. Altho.pdf
Select all of the following that are true regarding evolution.  Altho.pdfSelect all of the following that are true regarding evolution.  Altho.pdf
Select all of the following that are true regarding evolution. Altho.pdf
 
Role of involvement in consumer decision-making. Identify the level .pdf
Role of involvement in consumer decision-making. Identify the level .pdfRole of involvement in consumer decision-making. Identify the level .pdf
Role of involvement in consumer decision-making. Identify the level .pdf
 
Questionsa What are some of the barriers to understanding an issu.pdf
Questionsa What are some of the barriers to understanding an issu.pdfQuestionsa What are some of the barriers to understanding an issu.pdf
Questionsa What are some of the barriers to understanding an issu.pdf
 
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdfRainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
 
Quality software project managementHow are tasks, activities, and .pdf
Quality software project managementHow are tasks, activities, and .pdfQuality software project managementHow are tasks, activities, and .pdf
Quality software project managementHow are tasks, activities, and .pdf
 
Part I Write the complete class definition (or server) for Unsorte.pdf
Part I Write the complete class definition (or server) for Unsorte.pdfPart I Write the complete class definition (or server) for Unsorte.pdf
Part I Write the complete class definition (or server) for Unsorte.pdf
 
OverviewThe Internet and World Wide Web (WWW) were originally two .pdf
OverviewThe Internet and World Wide Web (WWW) were originally two .pdfOverviewThe Internet and World Wide Web (WWW) were originally two .pdf
OverviewThe Internet and World Wide Web (WWW) were originally two .pdf
 
Nutrition Design a one day menu for a diabetic patient with pressur.pdf
Nutrition Design a one day menu for a diabetic patient with pressur.pdfNutrition Design a one day menu for a diabetic patient with pressur.pdf
Nutrition Design a one day menu for a diabetic patient with pressur.pdf
 
Java code to find the closest pair using divide and conquer algorith.pdf
Java code to find the closest pair using divide and conquer algorith.pdfJava code to find the closest pair using divide and conquer algorith.pdf
Java code to find the closest pair using divide and conquer algorith.pdf
 
In _________________, substances move from an area of high concentra.pdf
In _________________, substances move from an area of high concentra.pdfIn _________________, substances move from an area of high concentra.pdf
In _________________, substances move from an area of high concentra.pdf
 
Im confused on question 6,7 and 8 On what point(s) would Charles Dar.pdf
Im confused on question 6,7 and 8 On what point(s) would Charles Dar.pdfIm confused on question 6,7 and 8 On what point(s) would Charles Dar.pdf
Im confused on question 6,7 and 8 On what point(s) would Charles Dar.pdf
 
Histone deacylase modulate gene transcription. Which sentence is inco.pdf
Histone deacylase modulate gene transcription. Which sentence is inco.pdfHistone deacylase modulate gene transcription. Which sentence is inco.pdf
Histone deacylase modulate gene transcription. Which sentence is inco.pdf
 
Hi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdfHi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdf
 

Recently uploaded

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 

LinearPerson.javaplease help me the JAVA program A LinearPerson li.pdf

  • 1. LinearPerson.java please help me the JAVA program 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 public class LinearPerson { private int curr_pos; private char curr_dir; LinearPerson() { curr_pos=0; curr_dir='r'; } LinearPerson(int p) { curr_pos=p; curr_dir='r';
  • 2. } public void turn() { if(curr_dir=='r')curr_dir='l'; else curr_dir='r'; } public void move() { if(curr_dir=='r')curr_pos=curr_pos+1; else curr_pos=curr_pos-1; } public int getPosition() { return this.curr_pos; } } public class LinearPersonPair { public static void main(String args[]) { LinearPerson a=new LinearPerson(); LinearPerson b=new LinearPerson(3); a.move(); b.move(); a.turn(); b.turn(); a.move(); b.move() System.out.println(a.getPosition()+" "+b.getPosition()); } }