Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

QUESTION: This exercise is inspired by the movie Contagion which ran in theaters

ID: 3543090 • Letter: Q

Question

QUESTION:


This exercise is inspired by the movie Contagion which ran in theaters in 2011. Imagine

a small town of 100,000 people in which each person has exactly 5 friends (chosen randomly;

assume for simplicityâs sake that friendships are not mutual, i.e., it is possible that A is a friend

of B but B is not a friend of A). Consider a viral infection that first infects exactly one (randomly

chosen) person in the town. Assume that the probability that an infected person will transmit the

infection to a given uninfected friend is p. For example, if p = 0:4, an infected person who has

5 uninfected friends can be expected to infect an average of 5 0:4 = 2 friends. If p = 0:4, an

infected person who has 4 uninfected friends and one infected friend can be expected to pass on

his/her infection to 4 0:4 = 1:6 of his/her previously uninfected friends. What the expected

percentage of people in the town who will get infected if p = 0:2? What if p = 0:3? What if

p = 0:5?

To solve this exercise, it would be easiest to create 100,000 Person objects, each of whom has

a simple method called infect() in which the person acquires the infection and then calls the

infect method in his/her friends with probability p (thus infecting each friend with probability p,

each of whom will now automatically spread the infection to their friends and so on). This exercise

illustrates how a very complex problem like this one can be easily simulated using object-oriented

programming and the programmer does not have to do the messy task of keeping track of who

is infecting whom. The programmer merely has to worry about how one person infects his/her

friends.



I am so close to finishing this program. The only thing I am having a problem with is keeping track of what "person" objects have already gone through the infect() method. The questions says that you do not need to keep track of who is infecting who but that doesn't make sense to me. You need to keep track of this or else your loop will never end. Can someone advise: The code I have below is as far as I have gotten. Thank you so much!



public class Contagion1 {

    

   private int infection_state;

   private int to_used_state;

   

   private static int INFECTED = 1;

   private static int NOT_INFECTED = 0;

   

   private static int TO_USE = 1;

   private static int NOT_TO_USE = 0;

   

   public static double infectchance = 0.3;

   public static int peoplecounter = 0;

   public static int infectedpeople = 0;

   

   public static int randomperson = 0;

   public static int personnumber = 0;

   

   public static Contagion1[] people = new Contagion1[100000];

   public static ArrayList<Double> percentages = new ArrayList<Double>();

    

    public static void main(String[] args) {

        

    

     // Initiate 100,000 People and Assign Infected State

     for(int i =0; i<people.length;i++)

     {

         people[i] = new Contagion1();

         people[i].setinfectionstate(NOT_INFECTED);

         people[i].setkeepusing(TO_USE);

            

     }

     ////////////

     

     

     //Infect a Random Person

     Random r = new Random();

     randomperson = r.nextInt(100000);

     people[randomperson].infect();

    ////////////

     

     

     //Calculate Infected People

     int count = 0;

     for(int j = 0; j<people.length; j++)

     {

         if(people[j].getinfectionstate() == INFECTED)

         {

             count++;

         }

     }

     double infectpercentage = (double) count/100000*100;

     System.out.println(infectpercentage+"%");

     ///////////

    

   }

    

    public void infect()

    {

        people[randomperson].setinfectionstate(INFECTED);

        

        Random r = new Random();

        double infectprob = Math.random();

        

        for(int i =0; i<5; i++)

        {

        personnumber = r.nextInt(100000);

         if(infectprob<infectchance)

         {

            people[personnumber].setinfectionstate(INFECTED);

            people[personnumber].infect();

            

            

         }

         

        }

    }

  

    

    public int getinfectionstate()

    {

        return infection_state;

    }

    

    public void setinfectionstate(int newstate)

    {

        infection_state = newstate;

    }

    

     public void setkeepusing(int touse)

    {

        to_used_state = touse;

    }

    

     public int getkeepusingstate()

    {

        return to_used_state;

    }

     

            

}


Explanation / Answer

That is the beauty of this code. You actually do not need to keep track of who is getting infected.

You see your loop will end only under two circumstances:

1. A person does not infect anyone. That is the random number genrated for each of the five persons lie below the required probability value. This means that the chain reaction stops here.

2. All the five friends are infected. For this case, you will have to check whether all the five friends of the infected person are already infected or not. If they are infected, that means your loop should end there.

Note that the second case is a general scenario for the specialized case of everybody getting infected.


Excellent programming statement btw. Its been a long time since I have seen such an interesting question