Java OOP - Learning instance of element of Array, for simulating human life ... Mating and new Borning

82 Views Asked by At

as you can see, probably I exaggerated this topic. Here is the what i need;

I have a 5-6 classes. and i simulating , when this classes meeting on same location, if they same class with opposite gender, they will be mate. and a baby will born with same class.

But i confused here. I am matching classes with ArrayList and for loop (i and j nested loop).

that's what i need ;

if(male Xclass   matches   female Xclass){   //actually here i dont know classes
  Xclass x = new Xclass();                   // i have just elements i and j
  list.add(x);
} // something like that , but how can i know which class they have and how 
  // can i create a new class while i don't know this.

**Sorry for bad english, Thank you :)

EDIT: I meant same class with different gender; there are classes A,B,C,D and gender is just variable (private int Gender; //1 for male,0for female and it is random value , 1 or 0) i have a also loctaion x,y class. male A and male A cant mate male A and female A can mate male B and female C cant mate i mean this. sorry for bad experession

1

There are 1 best solutions below

10
On

So do you mean like this (it's disturbing)? It's weird what you mean by classes of the same type and then different. From your example you only have one class. If I understand correctly you want to model something like this.

class Person {

  public enum Gender {

     Male,
     Female;
  }

  public enum SocialClass {
    Royal,
    Peasant,
    Barbarian;
  }

  public final Gender gender;
  public final SocialClass socialClass;

  public Person(Gender gender, SocialClass socialClass) {
     this.gender = gender;
     this.socialClass = socialClass;
  }

  public Person meet(Person person) {
    if (person != this && person.socialClass == this.socialClass && person.gender != this.gender) {
       return new Person(Math.random() > .5 ? Gender.Male : Gender.Female, this.socialClass);
    } else {
       return null;
    }
  }
}

Then you could do something like:

List<Person> people = ...// however you got your list
Person person = randomRemoveFrom(people);
Person otherPerson = randomRemoveFrom(people);

Person child = person.meet(otherPerson);
if( child != null) {
   System.out.println("New Person instance?");
} else {
   System.out.println("No new person");
}