# Java: Infection Simulator

Awhile back I wrote a vampire infection sim in Python, as well as Groovy and Ruby.  Below is a simplified version written in Java.

## Concept

The idea behind this simulator is to take a population of people and a virus infection.  To simplify the calculation we will generate stats on the strength of an individual human (member of the population) and the strength of the virus (viruencly.)

The sim will generate 1 human and 1 virus strain.  It will then calculate who is stronger.  If the virus is stronger than this individual, then the infected pool is incremented by 1.  However, if the virus looses against the individual human, than the resistant human population increases by 1.  In either case, the original human population also decrements by 1.

This simulator should continue until the start population is exhausted.

## Calculation

As I’m keeping this calculation very simple, I want to use the same logic to generate this strength indicator for both a human and the virus that infects this human.

The calculation will simply be a random number from 1 to 100, and we will add a modifier to this calculation.

So let’s create a class called StrengthCalculator with the following code:

```public class StrengthCalculator {
int x;
public int setStrength(int y){  //Resistance Modifier
x = (int)(Math.random()) + y;
return x;
}

public int getStrength(){ //Random Resistance + Modifier
int range = Math.abs(1 - 100) + 1;
return (int) (Math.random() * range) + x;
}
}```

This class will be instantiated each time we want to check the strength of a human or virus strain.  We also allow a modifier to be passed in as a parameter.  This would allow us to simulate a stronger or weaker virus (or human.)

## Main Class

I created a class called InfectionSim with the following code:

```public class InfectionSim {
int hStats;
int vStats;
int infected;
int resistant;

public static void main(String[] args){
InfectionSim iS = new InfectionSim();
iS.run(1,10, 2000000); //arg: (human strength modifier, virus strength modifier, population)
}
public void run(int hs, int vs, int population){
infected = 1;
resistant = 0;
while (population>0){
StrengthCalculator h = new StrengthCalculator();
StrengthCalculator v = new StrengthCalculator();
h.setStrength(hs);
v.setStrength(vs);
hStats = h.getStrength();
vStats = v.getStrength();
if(hStats > vStats){
population --;
resistant ++;
}else{
population --;
infected ++;
}
System.out.println("Pop: "+population);
System.out.println("Infected: "+infected);
System.out.println("Resistant: "+resistant);
System.out.println("-------------------");
}
}
}```

At the top of the class I’m statically typing the variables hStats (human stats), vStatus (virus stats), infected (the population infected) and resistant (the population that is resistant to infection.)

The main class simply instantiates this class and calls the run method.

The run method takes 3 params (human strength modifier, virus strength modifier and population size.)  Therefore iS.run(1,20,2000000) runs the run method, generating a strength for the human without much of a modifier (bonus) and the virus is generated with a +10 to it’s strength.  Finally the population is set to start at 2,000,000.

A loop is set so that while the population is greater than 0, it will generate a new human and a new virus strain.  The results of which are compared.  Conditional logical will increment or decrement the specific pools (population, infected, resistant.)

## Conclusion

This is a simple little Java app that gives an introduction to using classes, instantiation, static typing, flow control and incrementing/decrementing.