# Disease Propagation Simulation

• (2 Pages)
• 1
• 2

## 21 Replies - 2719 Views - Last Post: 04 December 2013 - 12:49 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=335628&amp;s=95ff951f5d49167bd50e170ebe9d633f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 trilla

Reputation: 0
• Posts: 8
• Joined: 03-December 13

# Disease Propagation Simulation

Posted 03 December 2013 - 10:18 PM

SO I have this diseaseProp package that includes StdDraw, Universe, ZombieTurtle, and diseaseProp.

This code features a simulation of zombies and non-zombies in a population. Both types move around randomly in the universe and if a non-zombie gets too close to a zombie, the non-zombie becomes a zombie.

The current code only has two states: zombie and not-zombie and that is determined by the zombie variable in the ZombieTurtle class. The new code needs to have three states: Susceptible, Infected, and Recovering, so you can use the same idea as what's currently there, but the boolean variable won't work. You will also need a method that moves the zombieturturtle between those states. The for loop in diseaseProp.java can be used to control time, and the zombieturtle is in each state for some number of days, which is the same as iterations of the loop.

StdDraw is StdDraw

Code for Universe:
```/* We needa virtual physical space in which to place our Turtle objects. We'll call this space Universe, which means that we have a Universe
* object and in our diseaseProp.java code, we'll create an instance of Universe.
*/
package diseaseProp;

/**
*
* @author user
*/
public class Universe {
//N will be the number of objects in our universe
private int N;
private ZombieTurtle[] zt;
private int ztLocations[][];
private int width;
private int height;

/*we want to have objects interact, so radius is how many squares around the Turtle can the Turtle "see". We are assuming that space
* is discrete here, that each Turtle has an (x,y) position on the grid.
*/

/*the constructor, takes an int and uses this to create N instances of Turtle and place them in the Universe.*/
public Universe(int numTurtles, int numZombies, int w, int h)
{
StdDraw.setCanvasSize(w, h);
StdDraw.setXscale(0, w);
StdDraw.setYscale(0, h);

/*set the Universe variables using arguments to the Universe constructor*/
ztLocations = new int[w][h];
N = numTurtles;
zt = new ZombieTurtle[N];

width = w;
height = h;

/*Create the ZombieTurtles in the Universe and put them at random (x,y) locations.
* Notice that the ZombieTurtle constructor also has it's own constructor.
*/

for(int i = 0; i < N; i++)
{
if (i < numZombies)
zt[i] = new ZombieTurtle(true);
else
zt[i] = new ZombieTurtle(false);
//generate random integer location for each zt
int x = (int)(Math.random()*width);
int y = (int)(Math.random()*height);
ztLocations[x][y] = i; //this only lets one at a location, we'll assume that means a zombie is hiding
/*setLocation is a method in ZombieTurtle. We call it here to set the zt location*/
zt[i].setLocation(x, y);
}
}
public void moveZombies()
{
/*This method is part of the Universe and determines how each zt will move given it's current location.
* We take it's current location, select a random integer between min and max and add that value to the x and y position
* of the zt. The ztLocations matrix tracks each zt in the Universe.
*/
int min = -8;
int max = 8;
int newX;
int newY;
for(int x = 0; x < N; x++)
{
int oldX = zt[x].getXLocation();
int oldY = zt[x].getYLocation();
//need a new x to be +1, +0, or -1 of current x
newX = min + (int)(Math.random() * ((max - min)+1));
if ((oldX+newX < 0) || (oldX+newX >= width))
newX = 0;
newY = min + (int)(Math.random() * ((max - min)+1));
if ((oldY+newY < 0) || (oldY+newY >= width))
newY = 0;

ztLocations[oldX][oldY] = 0;
ztLocations[oldX+newX][oldY+newY] = x;
zt[x].setLocation(oldX+newX, oldY+newY);
}
}
public void zombieAttack()
{
/*check each zombie turtle and if it's infected, check it's surrounding neighborhood within the radius to find if other non-infected
* zombie turtles are there. If so, make the non-infected into an infected.
*/
for(int x = 0; x < N; x++)
{
if(zt[x].isZombie())
{
int locX = zt[x].getXLocation();
int locY = zt[x].getYLocation();
int xLow;
int xHigh;
int yLow;
int yHigh;

if (locX - radius < 0)
xLow = 0;
else

if (locX + radius >= width)
xHigh = width-1;
else

if (locY - radius < 0)
yLow = 0;
else

if (locY + radius >= height)
yHigh = height-1;
else

for(int x2 = xLow; x2 < xHigh; x2++)
{
for(int y2 = yLow; y2 < yHigh; y2++)
{
if(ztLocations[x2][y2] > 0) //there's a zombie here
{
zt[ztLocations[x2][y2]].makeZombie();
//System.out.println("zombie attack");
}

}
}
}
}
}
}

```

Code for ZombieTurtle:
```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package diseaseProp;

import java.awt.Color;

/**
*
* @author user
*/
public class ZombieTurtle {
private int x;
private int y;
private boolean zombie;

public ZombieTurtle(boolean z)
{
zombie = z;
}
public void setLocation(int xIn, int yIn)
{
//erase old zombie location
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledCircle((double)x, (double)y, 5);

if (zombie == true)
StdDraw.setPenColor(StdDraw.RED);
else
StdDraw.setPenColor(Color.BLUE);

x = xIn;
y = yIn;
StdDraw.filledCircle((double)x, (double)y, 3);
StdDraw.show(1);
}
public boolean isZombie()
{
return zombie;
}
public void makeZombie()
{
zombie = true;
}
public int getXLocation()
{
return x;
}

public int getYLocation()
{
return y;
}
}

```

Code for diseaseProp:
```package diseaseProp;

/**
*
* @author user
*/
public class diseaseProp {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// set some number to be zombies
int z = 20;
//this is number of total ZombieTurtles, only z of which are initially zombies
int zTurtles = 300;
int N = 150;
/*need a Universe object that we can put turtles into. turtles will be circles and change colors when they
* come in contact with other turtles that are inflicted with zombie-ism
*
*/
Universe un = new Universe(zTurtles,z,600, 600);
/*our simulation will run for N iterations */
for(int i = 0; i < N; i++)
{
/*choose new random location for all ZombieTurtles in the Universe */
un.moveZombies();
/*Convert new zombies from ZombieTurtle population */
un.zombieAttack();
```

Is This A Good Question/Topic? 0

## Replies To: Disease Propagation Simulation

### #2 astonecipher

• Senior Systems Engineer

Reputation: 2555
• Posts: 10,249
• Joined: 03-December 12

## Re: Disease Propagation Simulation

Posted 03 December 2013 - 10:27 PM

Which variable is not working?

### #3 trilla

Reputation: 0
• Posts: 8
• Joined: 03-December 13

## Re: Disease Propagation Simulation

Posted 03 December 2013 - 10:30 PM

astonecipher, on 03 December 2013 - 10:27 PM, said:

Which variable is not working?

Nothing isn't working right now. I cant even fathom a starting point for creating the other states of disease like I mentioned in the second paragraph

### #4 trilla

Reputation: 0
• Posts: 8
• Joined: 03-December 13

## Re: Disease Propagation Simulation

Posted 03 December 2013 - 10:57 PM

trilla, on 03 December 2013 - 10:18 PM, said:

SO I have this diseaseProp package that includes StdDraw, Universe, ZombieTurtle, and diseaseProp.

This code features a simulation of zombies and non-zombies in a population. Both types move around randomly in the universe and if a non-zombie gets too close to a zombie, the non-zombie becomes a zombie.

The current code only has two states: zombie and not-zombie and that is determined by the zombie variable in the ZombieTurtle class. The new code needs to have three states: Susceptible, Infected, and Recovering, so you can use the same idea as what's currently there, but the boolean variable won't work. You will also need a method that moves the zombieturturtle between those states. The for loop in diseaseProp.java can be used to control time, and the zombieturtle is in each state for some number of days, which is the same as iterations of the loop.

I just need to build off of what I already have. I need some serious handholding to get this (I'm sorry), I'm fairly lost.

### #5 astonecipher

• Senior Systems Engineer

Reputation: 2555
• Posts: 10,249
• Joined: 03-December 12

## Re: Disease Propagation Simulation

Posted 03 December 2013 - 11:02 PM

You need a parent class that defines everything that is the same. Then derived classes to change what is different.

### #6 astonecipher

• Senior Systems Engineer

Reputation: 2555
• Posts: 10,249
• Joined: 03-December 12

## Re: Disease Propagation Simulation

Posted 03 December 2013 - 11:03 PM

You need a parent class that defines everything that is the same. Then derived classes to change what is different.

You need a parent class that defines everything that is the same. Then derived classes to change what is different.

### #7 trilla

Reputation: 0
• Posts: 8
• Joined: 03-December 13

## Re: Disease Propagation Simulation

Posted 03 December 2013 - 11:19 PM

astonecipher, on 03 December 2013 - 11:03 PM, said:

You need a parent class that defines everything that is the same. Then derived classes to change what is different.

You need a parent class that defines everything that is the same. Then derived classes to change what is different.

Can you provide an example? I'm sorry I don't mean to come off as totally inept, but I do need some handholding. I'm still very much a beginner

### #8 astonecipher

• Senior Systems Engineer

Reputation: 2555
• Posts: 10,249
• Joined: 03-December 12

## Re: Disease Propagation Simulation

Posted 03 December 2013 - 11:29 PM

Still in school?

### #9 trilla

Reputation: 0
• Posts: 8
• Joined: 03-December 13

## Re: Disease Propagation Simulation

Posted 03 December 2013 - 11:39 PM

astonecipher, on 03 December 2013 - 11:29 PM, said:

Still in school?

Very much so... I'm kind of in a tight spot as well. I hate having to just ask for an answer within the context of this random code, but I really don't have much of a choice.

### #10 astonecipher

• Senior Systems Engineer

Reputation: 2555
• Posts: 10,249
• Joined: 03-December 12

## Re: Disease Propagation Simulation

Posted 03 December 2013 - 11:42 PM

Look in your book at abstract classes.

Reputation: 0
• Posts: 5
• Joined: 03-December 13

## Re: Disease Propagation Simulation

Posted 03 December 2013 - 11:44 PM

Hey! What a terrific coincidence! I have just about the EXACT same project, just with a few alterations.
I figured out how to include the infection rate if you have to do that as well. But I am in the same dilemma about giving another instance of being "recovered." I understand in theory how to make it work but I can't quite understand how to code it properly. This is my first semester in programming so I'm in the same boat of being a noob.

### #12 trilla

Reputation: 0
• Posts: 8
• Joined: 03-December 13

## Re: Disease Propagation Simulation

Posted 03 December 2013 - 11:50 PM

astonecipher, on 03 December 2013 - 11:42 PM, said:

Look in your book at abstract classes.

I don't want to come off as ungrateful but I've used all of my available resources with no resolve. Can you be more specific with your responses. I'm sure this isn't that much coding and it would actually make my year if you could give me some feedback within the context of the code I posted.

Reputation: 0
• Posts: 5
• Joined: 03-December 13

## Re: Disease Propagation Simulation

Posted 04 December 2013 - 12:00 AM

So, astonecipher, how do you suggest implementing a parent class? Would it be simpler to keep Zombieturtle and just add two other classes of Recovered and Susceptible? Within those classes you could have a for-loop that counts days to determine how long a turtle is Infected or Recovered, right? I thought that was the way to attack this.

### #14 astonecipher

• Senior Systems Engineer

Reputation: 2555
• Posts: 10,249
• Joined: 03-December 12

## Re: Disease Propagation Simulation

Posted 04 December 2013 - 12:07 AM

You two are doing a similar project. How would you approach it and what would the new class be responsible for?

Reputation: 0
• Posts: 5
• Joined: 03-December 13

## Re: Disease Propagation Simulation

Posted 04 December 2013 - 12:25 AM

You would have a class Recovery_Days (arbitrary name) and it would keep track of how long a zombie turtle has been infected.
If it takes 7 days for an Infected to become Recovered, then you would have something like:

```

// specify number of days until recovered
private int N = 6;

public boolean Recovered(){

for(int i = 0; i < N; i++){
if (i == N){
is_Recovered == true;
zombie == false;
}
}

```

So, in theory, you would say that when a turtle becomes a zombie, start running the countdown clock. When that clock hits the correct number of days until recovery, the zombie stops being a zombie and starts being a recovered.

Does that make sense? And yeah I know that what I have up there is incomplete code but it gets the idea across.

Then you would use that same idea for Susceptible.
N would change based on how long they are to be in Recovery mode for... and then the conclusion in the if statement would be:

recovered == false
susceptible == true

and either susceptible means the original zturtle or there will have to be more editing to make the normal turtles defined as "Susceptible."