There are four classes that interact with my project. The aim is to generate an object using enum, using the random values from enum.
EthicalEngine
class. I'll call the method from another class called ScenarioGenerator
.Person
class inherited from the Character
abstract class. I am not sure whether my method of initialization is correct or not. My idea is to initialize the instance of the Person
in the ScenarioGenerator
class, and use a method getRandomPerson()
to call the constructor.
But I stuck, keep getting NullPointerException
when I called the method.
Here is part of my Character
class:
abstract class Character {
private int age;
private Gender gender;
private BodyType bodyType;
protected Profession profession;
protected AgeCategory ageCategory;
protected boolean isPregnant;
public enum Gender {
MALE, FEMALE, UNKNOWN;
}
public enum BodyType {
AVERAGE, ATHLETIC, OVERWEIGHT, UNSPECIFIED;
}
public enum Profession {
DOCTOR, CEO, CRIMINAL, HOMELESS, UNEMPLOYED, MUSICIAN, BOXER ,UNKNOWN, NONE;
}
public Character(int age, Profession profession, Gender gender, BodyType bodyType, boolean isPregnant) {
this.age = age;
this.profession = profession;
this.gender = gender;
this.bodyType = bodyType;
this.isPregnant = isPregnant;
}
part of my Person
class:
Person(int age, Profession profession ,Gender gender, BodyType bodyType, boolean isPregnant) {
super(age, profession, gender, bodyType, isPregnant);
}
public Profession getProfession () { //use getter to generate a random value
//only adults have profession
if (getAge()<=16 || getAge()>68) {
return Profession.NONE;
} else {
return Profession.values()[new Random().nextInt(Profession.values().length)];
}
//other getters and setters
}
Part of my ScenarioGenerator
class:
public class ScenarioGenerator {
private Person person;
private Animal animal;
private Scenario scenario;
private Random random = new Random();
private int passengerCountMinimum;
private int passengerCountMaximum;
private int pedestrianCountMininum;
private int pedestrianCountMaximum;
public ScenarioGenerator() {
random.setSeed(random.nextInt());
}
public ScenarioGenerator(long seed) {
this.random = new Random(seed);
}
public Person getRandomPerson() {
//need age, gender, bodyType, profession, pregnancy
Person people = new Person(person.getAge(), person.getProfession(), person.getGender(),person.getBodyType(), person.isPregnant());
return people;
}
Part of my EthicalEngine
class:
public class EthicalEngine {
public static void main(String[] args) throws Exception {
EthicalEngine ethicalEngine = new EthicalEngine();
ScenarioGenerator scenarioGenerator = new ScenarioGenerator();
scenarioGenerator.getRandomPerson();
}
you can generate random number and select all values
import java.util.Random;
public class ScenarioGenerator {
public Person getRandomPerson() {
Random rand = new Random();
// max age
int age = rand.nextInt(100);
// no of profession - 1
int profession = rand.nextInt(8);
// no of gender - 1
int gender = rand.nextInt(2);
// no of bodyType - 1
int bodyType = rand.nextInt(3);
int pragnency = rand.nextInt(2);
//need age, gender, bodyType, profession, pregnancy
Person people = new Person(age, Profession.values()[profession], Gender.values()[gender],BodyType.values()[bodyType], pragnency == 1 ? true : false);
return people;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.