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

Java Program The lab this lesson introduces students to writing programs that co

ID: 3864546 • Letter: J

Question

Java Program

The lab this lesson introduces students to writing programs that consist of multiple classes. By the end of this lab, students should be able to

write a class to define a type that contains instance data and methods

create multiple objects of that type

use objects of the same type independently

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, 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

class LinearPerson{

   int direction;
   int position;

   LinearPerson(){
       direction = 1;
       position = 0;
   }

   LinearPerson(int _position){
       position = _position;
       direction = 1;
   }

   public void turn() {
   // changes the direction of the LinearPerson (right to left, or left to right)
       direction *= -1;
   }
   public void move(){
       // moves the LinearPerson one position in his or her current direction
       position += direction;
   }
   public int getPosition() {
       // returns the current position of the LinearPerson
       return position;
   }

  

}


public class LinearPersonPair{

  

   public static void main(String[] argv) {

      LinearPerson sophie = new LinearPerson();
      // sophie is at position 0, moving right
      System.out.println("soohie's current position is " + sophie.getPosition());
       sophie.turn();
       // sophie is at position 0, moving left
       sophie.move();
       // sophie is at position -1, moving left
       System.out.println("soohie's current position is " + sophie.getPosition());
       sophie.move();
       // sophie is at position -2, moving left
       System.out.println("soohie's current position is " + sophie.getPosition());
       sophie.turn();
       // sophie is at position -2, moving right
       System.out.println("soohie's current position is " + sophie.getPosition());
       sophie.move();
       // sophie is at position -1, moving right
       System.out.println("soohie's current position is " + sophie.getPosition());


       LinearPerson mandy = new LinearPerson(5);
     
      System.out.println("mandy's current position is " + mandy.getPosition());
       mandy.turn();
      
       mandy.move();
      
       System.out.println("mandy's current position is " + mandy.getPosition());
       mandy.move();
      
       System.out.println("mandy's current position is " + mandy.getPosition());
       mandy.turn();
         
       mandy.move();
      
       System.out.println("mandy's current position is " + mandy.getPosition());
   }

}

Output :-

soohie's current position is 0
soohie's current position is -1
soohie's current position is -2
soohie's current position is -2
soohie's current position is -1
mandy's current position is 5
mandy's current position is 4
mandy's current position is 3
mandy's current position is 4

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