In JAVA Add a graphics (or ascii like printing an array with characters) to lab
ID: 3772456 • Letter: I
Question
In JAVA
Add a graphics (or ascii like printing an array with characters) to lab 6 so that it shows the initial World, and the World after each arrest (or move).
Lab 6 show below
Simulation
For this lab, you will 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)!!!
1.1 Point2D
Create a new class, Point2D, which has the following:
• two integer data fields, x and y declared according to rules of encapsulation.
• 2 constructors; one of them takes no parameters and sets the fields 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 fields.
• 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 different than the passed Point2D object.
• main method to perform unit test for this class and its methods.
1.2 PatrolCar
Create a new class, PatrolCar, which has the following:
• data fields:
a unique integer, id, for each PatrolCar object (hint: static modifier)
a Point2D object variable, location, representing the position of the PatrolCar in the world,
an integer data field, arrested which represents the number of suspects this PatrolCar object have arrested.
• a constructor which doesn’t take any arguments, set’s the id to a unique number, initializes the arrested field to 0 and assigns the location to (0,0).
• a method moveWest() which returns false if the PatrolCar 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 PatrolCar 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 PatrolCar 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 PatrolCar is already on the south end of the world
(y==World.SIZE) and increases y by 1 and returns true otherwise.
• an accessor method getId(), that returns the id of the PatrolCar
• an accessor method getLocation(), that returns the location of the PatrolCar
• an accessor method getNumSuspectsArrested(), that returns the arrested value of the PatrolCar
• a method addArrested(), which increments the arrested value by 1.
• overriden toString() method to display PatrolCar object’s id, location (call Point2D toString method to display this attribute) and number of suspects arrested.
• 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.
1.3 World
• data fields:
an array of Point2D objects, each representing a suspects location. a PatrolCar object.
a public, static constant integer, SIZE, which represents the world size in each direction, set to 9.
• a constructor which initializes the suspect locations and PatrolCar objects randomly.
• a method update(), which checks if the patrolCar is in the same location as a suspect. If it is it changes the suspects position to (-1,-1) and increases the PatroCar’s arrested value by 1.
• a method move() which moves the PatrolCar in a random direction (east, west, north, south) and for a random number of cells ( upto World.SIZE) and calls update() method at each step.
• application’s main method, which calls the move() method and prints the position and number of arrests made by the PatrolCar object, until all suspects are arrested.
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();
}
}
Comment
Explanation / Answer
public class PatrolCar
{
private static int ID;
private Point2D location;
public int arrested;
public PatrolCar()
{
this.ID = ((int)(System.nanoTime() * 1000000000) / 1000000);
this.location = new Point2D();
this.arrested = 0;
}
public boolean moveWest()
{
if(this.location.getx() == 0)
{
return false;
} else {
this.location.setx(this.location.getx() - 1);
return true;
}
}
public boolean moveEast()
{
if(this.location.getx() == World.SIZE)
{
return false;
} else {
this.location.setx(this.location.getx() + 1);
return true;
}
}
public boolean moveNorth()
{
if(this.location.gety() == 0)
{
return false;
} else {
this.location.sety(this.location.gety() - 1);
return true;
}
}
public boolean moveSouth()
{
if(this.location.gety() == World.SIZE)
{
return false;
} else {
this.location.sety(this.location.gety() + 1);
return true;
}
}
// getters
public int getID()
{
return this.ID;
}
public Point2D getLocation()
{
return this.location;
}
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 car)
{
if(this.getID() == car.getID()) return 0;
if(this.getID() > car.getID()) return 1;
else return -1;
}
public static void main(String[] args)
{
PatrolCar car1 = new PatrolCar();
PatrolCar car2 = new PatrolCar();
car2.moveEast();
car2.moveSouth();
System.out.println(car1.toString());
System.out.println(car2.toString());
System.out.println("Comparing: " + car1.compareTo(car2));
car1.addArrested();
System.out.println(car1.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.