I'm very new to Java and I am making this program which randomly puts robots in a predefined arena.
I was testing my addRobot method in the main when this happened
Here's the code of my 2 classes:
import java.util.Scanner;
import java.util.Random;
public class RobotArena {
private int xmax,ymax;
Robot ro;
RobotArena roaa;
public static Robot rrr[];
RobotArena (int xs, int ys, Random r){
xmax = xs;
ymax = ys;
ro = new Robot(r.nextInt(xmax), r.nextInt(ymax), this);
}
public void addRobot () {
Scanner add = new Scanner(System.in);
System.out.print("How many robots would you like to add?");
int numberofRobots = add.nextInt();
rrr = new Robot[numberofRobots];
add.close();
for(int c=0 ;c < numberofRobots ; c++)
{
Random addro2 = new Random();
int Xcoordinate = addro2.nextInt(xmax);
int Ycoordinate = addro2.nextInt(ymax);
rrr[c]= new Robot(Xcoordinate,Ycoordinate, null);
System.out.println( rrr[c]);
}
}
import java.util.Random;
public class Robot {
private int x, y, dx, dy, ID;
private static int RobotID;
private RobotArena roA;
Robot(){
this(0,0, null);
}
Robot(int sx, int sy, RobotArena ra){
x = sx;
y = sy;
dx = 1;
dy = 1;
roA = ra;
ID = RobotID;
RobotID++;
}
public String toString() {
return "Robot ID = " + ID +", robot is at " + x + ", " + y;
}
public boolean isHere(int ix, int iy) {
if (ix == x && iy == y) {
return true;
}
else
{
return false;
}
}
public static void main(String[] args) {
RobotArena rrr1;
Random addro = new Random();
rrr1 = new RobotArena(20, 5, addro);
rrr1.addRobot();
}
}
Even though I never call the toString function, which generates a string for every robot's ID and position in the Arena,