Introduction

For this lab, you will extend the classes that you build in lab II.2. Do not change the classes that you worked on lab II.2, instead please make a seperate copy of each class and work on these copies for this lab. Add comments to your code, describing each data field, method and class (in the beginning of each file)!!!

Person

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

  • data field:
    • 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 defined 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 modifier method setLocation(), that takes 2 int parameters and modifies x and y. respectively.
  • overriden toString() method to return Person object’s location (call Point2D toString method to return this attribute)
  • 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 fields:
    • 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 field 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 fields:
    • keep unique int, id, for each PatrolCar object.
    • replace int arrested with an integer array, arrested which stores the id of suspects this PatrolCar object have arrested.
    • add 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.
  • remove moveWest, moveEast, moveNorth, moveSouth methods as they will be inherited from Person base class.
  • remove getLocation method.
  • keep accessor method getId() as it is, returning the id of the PatrolCar.
  • modify accessor method getNumSuspectsArrested(), that returns the number of suspects arrested.
  • modify method addArrested(), such that it takes and int parameter, id of the suspect, and adds it to the array arrested.
  • modify toString method to display arrested suspect’s id as well as the information presented in the previous lab.
  • keep compareTo method as it is.
  • 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 fields: 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 first 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.
  • application’s main method, which calls the move() and print() methods until all suspects are arrested.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.