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

In Java You need to develop a program that simulates a patrol car moving around

ID: 3767503 • Letter: I

Question

In Java

You need to develop a program that simulates a patrol car moving around in a 2-dimensional world, looking for suspects. The patrol car’s current location is represented by an (x, y) point in space. Imagine that the patrol car can choose a direction command one at a time. The program will have a broad control loop, and for each iteration of the loop, the patrol car must decide what to do (i.e., which command to execute). The goal is for the patrol car to locate all the suspects hidden in its world. The patrol car’s world is 10 × 10 units in size, and the x and y coordinates are in the range [0, 9] (i.e., from 0 to 9, inclusive).

Location (0,0) is assumed to be on the top-left corner, to conform computer graphics standards.

Add comments to your code, describing each data field, method and class (in the beginning of each file)!!!

Use these three class (Point2D, PatrolCar, World ) to show the simulation in graphics(like a game).

———————————————————————————————

public class Point2D {

   private int x, y;

public Point2D() {

   this.x = 0;

   this.y = 0;

}

  

public Point2D(int x, int y) {

   this.x = x;

this.y = y;

}

  

public int getX(){

   return this.x;

}

  

public int getY(){

   return this.y;

}

  

public void setX(int x){

   this.x = x;

}

  

public void setY(int y){

   this.y = y;

}

  

   public String toString(){

       return "("+this.x+","+this.y+")";

   }

  

   public int compareTo(Point2D point2D){

       if(point2D.getX() == this.getX()

           && point2D.getY()== this.getY())

           return 0;

       else

           return 1;  

   }

   public static void main(String[] args){  

       Point2D pd1 = new Point2D();

       System.out.println(pd1.toString());

       System.out.println(pd1.compareTo(new Point2D(0,0)));

      

       Point2D pd2 = new Point2D(7,5);

       System.out.println(pd2.toString());

       System.out.println(pd2.compareTo(new Point2D(7,5)));

       System.out.println("---------------");

      

       System.out.println( "x = "+ pd2. getX());  

       System.out.println( "y = "+ pd2. getY());

       System.out.println(pd2.toString());

       System.out.println(pd2.compareTo(new Point2D(4,5)));

       pd2.setX(6);

       pd2.setY(9);      

       System.out.println(pd2.toString());

       System.out.println(pd2.compareTo(new Point2D(6,9)));

       pd2.setX(5);

       pd2.setY(5);                      

       System.out.println(pd2.toString());

       System.out.println(pd2.compareTo(new Point2D(4,5)));      

   }  

}

———————————————————————————————

public class PatrolCar {

   //a unique integer, id,

   // for each PatrolCar object (hint: static modifier)

   private static int num = 0;

  

   private int id;

   //a Point2D object variable, location, representing

   //the position of the PatrolCar in the world,

   private Point2D location;

   //an integer data field, arrested which represents

   //the number of suspects this PatrolCar object have arrested.

   private int arrested;

  

public PatrolCar() {

   this.id = num;

   this.arrested =0;

   this.location = new Point2D(0,0);

      

   num++;

}

  

public boolean moveWest(){

   int currentX = this.location.getX();

   if(currentX==0){

       return false;        

   }

   else{

       location.setX(currentX - 1 );

       return true;  

   }     

}

  

public boolean moveEast(){

   int currentX = this.location.getX();

   if(currentX==World.SIZE){

       return false;

   }

   else{

       location.setX(currentX + 1);

       return true;

   }

}

      

public boolean moveNorth(){

   int currentY = this.location.getY();

   if(currentY==0){

       return false;

   }

   else{

       location.setY(currentY - 1);

       return true;

   }

}

public boolean moveSouth(){

   int currentY = this.location.getY();

   if(currentY==World.SIZE){

       return false;

   }

   else{

       location.setY(currentY + 1);

       return true;

   }

}

public int getID(){

   return this.id;

}

public String getLocation(){

   return this.location.toString();

}

   public int getNumSuspectsArrested(){

       return this.arrested;

   }

  

   public void addArrested(){

       this.arrested = this.getNumSuspectsArrested()+1;

   }

  

   public String toString(){

       String s= " ID: " + this.getID() + " Location: "

               + this.getLocation().toString() + " Arrested: "

               + this.getNumSuspectsArrested();

       return s;

   }

  

public int compareTo(PatrolCar patrolCar){

   if(this.getID()== patrolCar.getID()){

       return 0;

   }

   if(this.getID()> patrolCar.getID()){

       return 1;

   }

   else{

       return -1;

   }

}

  

public static void main(String[] args){

   PatrolCar patrolCar1 = new PatrolCar();

   PatrolCar patrolCar2 = new PatrolCar();

   PatrolCar patrolCar3 = new PatrolCar();

      

   patrolCar1.moveEast();

   patrolCar2.moveSouth();

   patrolCar3.moveEast();

   patrolCar3.moveSouth();

      

      

   System.out.println(patrolCar1.toString());

System.out.println(patrolCar2.toString());

System.out.println(patrolCar3.toString());

System.out.println("Comparing: " + patrolCar1.compareTo(patrolCar2));

patrolCar1.addArrested();

System.out.println(patrolCar1.toString());

}

}

———————————————————————————————

import java.util.*;

public class World {

  

   private Point2D [] suspectLocations ; //an array of Point2D objects, each representing a suspects location.

   private PatrolCar patrolCar; //a PatrolCar object.

   public static final int SIZE = 10;//a public, static constant integer, SIZE, which represents the world size in each direction, set to 10.

public World() {

   patrolCar = new PatrolCar();   //initialize PatrolCar object (0,0)

      

   Random random = new Random(); //create a random object

   this.suspectLocations = new Point2D[random.nextInt(SIZE)];

   for(int i= 0; i<this.suspectLocations.length;i++){

       suspectLocations[i] = new Point2D(random.nextInt(SIZE),

                                          random.nextInt(SIZE));

   }

}

  

public void update(){

   for(int i=0;i<suspectLocations.length;i++){

       if(this.patrolCar.getLocation().compareTo(this.suspectLocations[i])==0)

       {

           this.suspectLocations[i].setX(-1);

           this.suspectLocations[i].setY(-1);

           this.patrolCar.addArrested();

       }

   }

}

  

public void move(){

       catchS();

   System.out.println("ID : " + this.patrolCar.getID()+" Location : "

                       +this.patrolCar.getLocation()+ "," + " Arrested "

                       + this.patrolCar.getNumSuspectsArrested() );      

}

public void catchS(){

   while(this.patrolCar.getNumSuspectsArrested()<this.suspectLocations.length)

   {

       Random random = new Random();

       int number = random.nextInt(5);

      

       switch(number)

       {

           case 0:

               catchS();

               break;

           case 1:

               this.patrolCar.moveEast();

               this.update();

               break;

           case 2:

               this.patrolCar.moveWest();

               this.update();

               break;

                 case 3:

               this.patrolCar.moveNorth();

               this.update();

               break;

           case 4:

               this.patrolCar.moveSouth();

               this.update();

               break;

           default:

               System.out.print("^*^");

              

       }

   }

}

      

public static void main(String[] args){

   World w = new World();

   w.move();

}

}

Explanation / Answer

Requirement -> develop a program that simulates within a 2-dimensional world. The world is 10 x10 units
              in size, and the x and y coordinates are in the range [0, 9]
              Location (0,0) is assumed to be on the top-left corner, to conform computer graphics standards.

Point2D

Create a new class, Point2D, which has the following:

_ two integer data _elds, x and y declared according to rules of encapsulation.

_ 2 constructors; one of them takes no parameters and sets the _elds x and y both to 0, and the other takes

two integer parameters and sets x and y values respectively.

_ accessor and mutator methods to change the values of its data _elds.

_ overriden toString() method to display any Point2D object in mathematical format, e.g. (2,7)

_ a compareTo(Point2D) method :

to return 0, if two Point2D objects have the same x and y values,

to return -1, if the caller object's x and y value is di_erent than the passed Point2D object.

_ main method to perform unit test for this class and its methods.

Person

Create a new class, Person, which has the following:

_ data _eld:

a Point2D object variable, location, representing the position of the Person in the world,

_ a constructor which doesn't take any arguments and sets both the x and y to 0.

_ a constructor which takes two int arguments and assignes x and y to these values.

_ a method moveWest() which returns false if the Person is already on the west end of the world (x==0)

and decreases x by 1 and returns true otherwise.

_ a method moveEast() which returns false if the Person is already on the east end of the world (x==World.SIZE)

and increases x by 1 and returns true otherwise.

_ a method moveNorth() which returns false if the Person is already on the north end of the world (y==0)

and decreases y by 1 and returns true otherwise.

_ a method moveSouth() which returns false if the Person is already on the south end of the world (y==World.SIZE)

and increases y by 1 and returns true otherwise.

_ add a move method which returns a boolean and takes an int parameter in the range of [0, 3], representing

directions EAST, WEST, NORTH and SOUTH (will be de_ned in the World class). This method will call

the approprite move method according to the input (e.g. moveWest()), return the result of the operation

or print out a message if the input is not a valid direction.

_ an accessor method getLocation(), that returns the location of the Person.

_ a modi_er method setLocation(), that takes 2 int parameters and modi_es x and y. respectively.

_ overriden toString() method to return Person object's location (call Point2D toString method to return this

attribute)

2

_ a compareTo(Person) method. Return 0 if the location of the Person object and the one that is passed as

a paramenter have the same location, -1 otherwise. This method will call compareTo method of Point2D.

_ main method to perform unit test for this class and its methods.

Suspect

Create a new class, Suspect, as follows:

_ Inherit from Person class so that a Suspect becomes a specialized Person object.

_ data _elds:

a unique int, id, for each Suspect object.

a boolean, arrested, initially false but will be set to true if the Suspect objects gets arrested by a

PatrolCar object.

an int, money.

_ a constructor which doesn't take any arguments, set's the id to a unique number, initializes the arrested

_eld to false, money to 0 and assigns the location to a random x, y.

_ an accessor method getId(), that returns the id of the Suspect.

_ an accessor method isArrested(), that returns the arrested value of the Suspect.

_ a method caught(), which sets the arrested value to true and moves the suspect to location (-1,-1).

_ a method rob() which increments money by 10.

_ overriden toString() method to return Suspet object's id, location (call Point2D toString method to return

this attribute), amount of money and if the Suspect is arrested or not.

_ a compareTo(Suspect) method. Return 0 if two objects have the same id, 1 if the id of the object is bigger

than the id of the object passed as a parameter and -1 otherwise.

_ main method to perform unit test for this class and its methods.

PatrolCar

Modify class PatrolCar as follows:

_ Inherit from Person class so that a PatrolCar becomes a specialized Person object.

_ data _elds:

unique int, id, for each PatrolCar object.

an integer array, arrested which stores the id of suspects this PatrolCar object have arrested.

a constant int shared by all PatrolCar objects, called range, which represents the number of cells that

a PatrolCar object can travel for each move call made in World class. Set it to 2.

_ a constructor which sets the arrested array size to 3 and initializes each value in the array to -1.

_ accessor method getId() that return the id of the PatrolCar.

_ accessor method getNumSuspectsArrested(), that returns the number of suspects arrested.

_ method addArrested(), such that it takes and int parameter, id of the suspect, and adds it to the array

arrested.

_ overriden toString method to display arrested suspect's id as well as PatrolCar object's id, location (call

Point2D toString method to display this attribute) and number of suspects arrested.

3

_ a compareTo(PatrolCar) method. Return 0 if two objects have the same id, 1 if the id of the object is

bigger than the id of the object passed as a parameter and -1 otherwise.

_ main method to perform unit test for this class and its methods.

Civilian

Create a new class, Civilian, as follows:

_ Inherit from Person class so that a Civilian becomes a specialized Person object.

_ data _elds:

an int, money.

_ a constructor which doesn't take any arguments, initializes money to 30 and assigns the location to a

random x, y.

_ an accessor method getMoney(), that returns the money value of the Civilian

_ a method giveMoney(), which decrements the money value by 10 if it is not 0. If the civilian doesn't have

any money left, it moves it to (-1,-1).

_ overriden toString() method to return Civilian object's money, location (call Point2D toString method to

return this attribute).

_ main method to perform unit test for this class and its methods.

World

Modify class World as follows:

_ data fields:

a vector of Suspect objects.

a vector of PatrolCar objects.

a vector of Civilian objects.

a public, static constant integer, SIZE, which represents the world size in each direction, set to 9.

a public static constant integer EAST set to 0.

a public static constant integer WEST set to 1.

a public static constant integer NORTH set to 2.

a public static constant integer SOUTH set to 3.

_ a constructor which creates 3 Suspect objects, 2 PatrolCar objects and 5 Civilian objecs and store these

objects in the vectors.

_ a method update(), which does the following in order:

checks if a Suspect is in the same location as a Civilian. If so, the Suspect robs the Civilian and money

is transferred. If the Civilian doesn't have any money left, it is removed from the vector of Civilians.

checks if a PatrolCar is in the same location as a Suspect. If so, the Suspect is caught and its id value

is added to the PatroCar's arrested array.

_ a method move() which moves _rst the Civilians and then the Suspects in a random direction (EAST,

WEST, NORTH, SOUTH) and calls update() method at each step. At last step it moves PatrolCars also

in a random direction upto their range value and call update method at every cell.

_ a method print() which prints all the information of civilians, suspects and patrol cars.

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