Ok I've been working on this homework for some hours now. Sorry If the english isn't good I just translated from google.
"For our homework synchronized threads must develop a simulation of wildlife. There will be a single kind of animal . We will have a very particular kind of gorillas. We will have an area ( 20 x 20 Array ) . Initially we will create 5 male and 5 female gorillas . Then we must put in random positions. All gorillas are going to move in the Array, can not have more than three gorillas in a space. For the coexistence of these gorillas must follow these rules:
If two gorillas are found in a single cell and they are the same sex one of the two gorilla dies (this should be done randomly ) If two gorillas have a different sex, a new one is born (sex must also be selected at random ) . And they should continue their separate ways . We must identify the share and synchronize all the threads."
And I did this: Main:
public static void main(String[] args) {
InformacionGorila info1 = new InformacionGorila(true, 1);
InformacionGorila info2 = new InformacionGorila(true, 2);
Gorilas gorila1 = new Gorilas(info1);
Gorilas gorila2 = new Gorilas(info2);
gorila1.start();
gorila2.start();
}
InformacionGorila class:
public class InformacionGorila {
private boolean genero;
private String sexo;
private int id;
public InformacionGorila(boolean genero, int id) {
this.genero = genero;
this.id = id;
DecisionDeGenero(genero);
}
private void DecisionDeGenero(boolean genero) {
if (genero == true) {
setSexo("Macho");
} else if (genero == false) {
setSexo("Hembra");
}
}
public int getId() {
return id;
}
public boolean getGenero() {
return genero;
}
public void setGenero(boolean genero) {
this.genero = genero;
}
public String getSexo() {
return sexo;
}
private void setSexo(String sexo) {
this.sexo = sexo;
} }
Gorilas class:
public class Gorilas extends Thread {
private final InformacionGorila info;
private Territorio terr;
public Gorilas(InformacionGorila info) {
this.info = info;
}
public InformacionGorila getInfo() {
return info;
}
@Override
public void run() {
try {
terr.Convivencia(info);
System.out.println("Gorila: " + info.getId() + " Genero: " + info.getSexo());
} catch (Exception e) {
System.out.println(e);
}
} }
And Territorio class:
public class Territorio {
public boolean ocupado = false;
public int cont = 0;
Gorilas[][] gorilas = new Gorilas[20][20];
public synchronized void Convivencia(InformacionGorila info) {
while (ocupado) {
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(Territorio.class.getName()).log(Level.SEVERE, null, ex);
}
}
cont++;
if (cont == 2) {
ocupado = true;
}
jaula(info);
}
int j = 0;
int cont2 = 0;
private synchronized void jaula(InformacionGorila info) {
String g = null;
String h = null;
while (cont2 != 2) {
if (j == 0) {
g = info.getSexo();
j++;
} else if (j == 1) {
h = info.getSexo();
}
cont2++;
}
decision(g, h);
}
public void decision(String g, String h) {
if (g.equals(h)) {
InformacionGorila info = new InformacionGorila(true, 11);
Gorilas gorila = new Gorilas(info);
gorila.start();
}
} }
Of course is not finished yet but when I try to test it I get nullpointer exception when the program comes to call terr.Convivencia(info);
in Gorilas class method run. Can someone tell me why? What is wrong? or what am I doing wrong?
These are the exception:
Exception in thread "Thread-0" Exception in thread "Thread-1" >java.lang.NullPointerException at hilossync.Gorilas.run(Gorilas.java:28) java.lang.NullPointerException at hilossync.Gorilas.run(Gorilas.java:28)