## Questions on HW8-Spring07

 Question:How do you do probability? Like if there is 25% chance that some sick will die and .05% that there will be a typhoid mary, how would we do that?
• I'm going to ask if I can release some code for this. In the meantime, all you really have to do is to convert the percentage to a decimal number (in case you don't know how to convert a percentage into a decimal, divide by 100). Use Math.random() to generate a random number between 0 and 1, excluding 1 ([0, 1) for the math folks out there). If this random number is less than the decimal percentage then return true, if not return false. Hope that makes sense. Dawn Finney.
• So I've found another way to do this (Actually Professor Guzdial pointed me in this direction). In the Agent class within the act method, you will find some helpful code (Note the numbering is arbitrary):
```1 public void act(){
2    // Default action: wander aimlessly
3
4    // if the random number is > prob of NOT turning then turn
5    if (randNumGen.nextFloat() > PROB_OF_STAY)
6      this.turn(randNumGen.nextInt(360));
7
8    // go forward some random amount
9    forward(randNumGen.nextInt(speed));
10 } // end act()```
The most helpful line here would line 5 and is basically what you need to do except yours will probably be less than instead of greater than. Dawn Finney.

 Question: When a person dies, how do we make them stop wandering aimlessly? We are assuming that they need to be kept on the list so that they are able to continue infecting the other people.
• Zombies! Oh cool. I would give each person a new boolean instance variable (something like dead would work). Every person defaults to dead = false. When the person dies just change dead to true. Before having the person act just check whether dead is true or false. I think would be cool to have the dead person turn into a zombie for a period of time. For more information on how to survive zombie attacks, read The Zombie Survival Guide. Dawn Finney.

 Question: on the implementation of the public Health policies. Do they need to be implemented separately? meaning you to one policy and then start over and do the second one ... or do you do one policy (changing the classes) and then do the second one by making additional changes to those classes you just changed?
• It's probably a good idea to create two different simulation classes (by this I mean classes you write that extends Simulation), one for each policy. I would just write the class with the first policy, save. Then I would manipulate the class I used with the first policy but change things to suit the second policy and save as something else. There should be a great deal of similarity between the two classes so you shouldn't have to write the second policy entirely from scratch. Dawn Finney.
• I actually don't think of the policy as classes – it's probably possible, but it's hard to get one's head around. And it's the case that each policy is only a few lines of code. Mark Guzdial

 Question: Okay we're confused by what the homework description is saying, Are we suppose to modify PersonAgent or do we just create all new subclasses for Agent? How exactly do you override Agent?We know some of this was discussed in lecture but we didn't entirely comprehend what we were suppose to create versus what was given to us.
• You may either modify PersonAgent, or create new subclasses for Agent, or both. It's your choice, and there are tradeoffs with each. To override a method in Agent, create a new method with the same name and same inputs in the subclass. (Note: I do start each lecture with "Questions on Anything?!?" and questions on homework are completely fair game. Mark Guzdial
• You can really do either option. I recommend editing the already existing classes, PersonAgent and DiseaseSimulation. Check out slides 29 - 40 of a-simulation-package-v2.ppt for more information on PersonAgent and DiseaseSimulation. You override a method in a super class by creating a method in the child class that has the same method signature of the super class method. The method signature for an object is the method name and the number and type of its parameters. Return types are not considered to be a part of the method signature (Wikipedia).
```public class Student{
public void doAssignments(Assignment[] assignments){
for (int i = 0; i < assignments.length; i++){
complete(assignments[i]);
}
}

public void complete(Assignment a){
...
}

public void goOnSpringBreak(){
//defaults to the beach
...
}
}

public class UGAStudent extends Student{
public void doAssignments(Assignment[] assignments){
for (int i = 0; i < assignments.length; i++){
party();
}
}

public void party(){
...
}

public void goOnSpringBreak(Destination place){
...
}
}```
UGAStudent overrides doAssignments but does not override goOnSpringBreak. Dawn Finney.

 Question: Any hints on how to actually set up time?
• Time is already set up for you in the run() method in the superclass Simulation. Mark Guzdial

 Question: This is from the homework: "People, once infected (come within 10 steps of an infected person), don't turn red for two days – though they do spread the disease." So, if I became sick on Monday (as in woke up and was sick) do I turn red when I wake up on Tuesday? (aka the next time step) Or do I wake up red on Wednesday? Is Monday counted as one of those 2 days? Just trying to clarify.
• You're infected in one time step. At the next time step, if you are infected and not yet red, turn red. Mark Guzdial

 Question: Our HWPersonAgent extends Agent, which extends Turtle, which extends SimpleTurtle. SimpleTurtle has a method called .getBodyColor() and none of the subclasses overwrite this method. However, when we try and call .getBodyColor() on a instance of HWPersonAgent, we get the following error:```File: C:\cs1316\java-source-Fall06\HWPersonAgent.java [line: 184] Error: cannot find symbol symbol : variable getBodyColor location: class HWPersonAgent```Why is that? And how can we check if someone is red so that we can run away from them? Thanks!
• I'm thinking you probably forgot the () for the method. There is a boolean infection in PersonAgentthat will tell you if the person is infected or not. Dawn Finney.

 Question: We created a variable called lengthOfInfection=0; that was initialized. We were wondering how we could increase it as the amount of days increases. For instance on day 12, a person gets infected so lengthOfInfection=0; but on day 13, this value should increase to 1. How do we make it do that?
• Two words. if statement. Increment lengthOfInfection when the time is incremented then check to see if lengthOfInfection is now 13; if it is 13, change it to 1. Dawn Finney.

### CHANGE TO ASSIGNMENT

Please run your simulation for 100 time steps, not just 50. From the runs that I'm seeing, 50 time steps is too early in the simulation, and there seems to be a critical mass a little after 50. You are welcome to change Simulation run() that lets you run for 100.

BTW, please count the number of LIVE infected, not dead infected, in your "infected" count. Mark Guzdial

 Question: We understand how to create a typhoid mary carrier but how does it stay one? We first created a for loop to count time and increment it. We then created the if statements for the various parameters. For the one in question we said for days between 1 and 6 a person is infected. Within that we created an if statement for the probability of typhoid mary with```if(randNumGen.nextFloat() > PROB_OF_TM) this.setColor(Color.red);```So how do we ensure that after you either become a typhoid mary carrier or don't that a person stays that way. We feel that at this time the code would cause a person to find a new Random Number everytime and thus possibly change what they were.
• Just check if the person is already assigned carrier or not before generating the random number. You can do this a number of ways. The most easily understandable way would be to use a boolean tmcarrier and boolean assigned. You can also just use one int variable where -1 is unassigned, 0 is non-tmcarrier, 1 is tmcarrier. So for the boolean version:
```if (assigned == false){
assigned = true;
if(randNumGen.nextFloat() > PROB_OF_TM){
tmcarrier = false;
this.setColor(Color.red);
}
else{
tmcarrier = true;
}
}```
Dawn Finney.

 Question: When I run my disease simulation everything seems to work, except how it prints out how the number infected. I end up getting a number over 100, which obviously doesn't make sense. Also, for some of the timesteps, 'number infected' shows up multiple times.
• You probably increment the number infected too many times. Just look at the places you increment the number infected. Dawn Finney.

 Question: I cannot seem to figure out how to say on day 2 turn red day 5 die. etc. i have put the code that i have tried to use below and i think my problem is that it goes through the entire for loop in one time step. how can i get int i to increase once every day?```public void infect(){ for (int i = 0; i < 8; i++){ this.infection = true; if (i == 0){ this.setColor(Color.lightGray);} if ( i == 2){ if(randNumGen.nextFloat() > .05) this.setColor(Color.red); if(i==5){ if (randNumGen.nextFloat() < .25){ this.dead=true;}} if (i == 6){ this.setColor(Color.lightGray);} if (i == 7){ this.infection = false;}```
• I suggest that you take a look the run(int timeRange) method in the Simulation class. Each time act() is called on an Agent it passes the current timestep as a parameter. You need to write a method that has the current timestep as a parameter and then have a series of if/else if/else statements within the method. Dawn Finney.

 Question: Dawn could you please clarify your answer to the previous question. We are having the same problem but we don't understand how make the current timestep as a parameter or even how to call it. We looked at the run() method in the Simulation class and it didn't really help us figure it out.Thanks so much
• ```1. public void run(int timeRange){
2.    // For storing the current agent
3.    Agent current = null;
4.
5.    // Set up the simulation
6.    this.setUp();
7.
8.    // loop for a set number of timesteps
9.    for (int t = 0; t < timeRange; t++){
10.      // loop through all the agents, and have them
11.     // act()
12.      for (int index=0; index < agents.size(); index++) {
13.        current = (Agent) agents.get(index);
14.        current.act(t); // pass in timestep
15.      }
16.      // Could separate acting from updating...
17.
18.      // repaint the world to show the movement
19.      // IF there is a world
20.      if (world != null) {
21.        world.repaint();}
22.
23.      // Do the end of step processing
24.      this.endStep(t);
25.
26.      // Wait for one second
28.    }
29.  }```
The important lines to look at here are 9 - 15. REMOVEDe 9, 28: The outer for loop will repeat for the amount of timesteps specified by the user. REMOVEDe 12, 15: The inner for loop will go through all of the Agents and have them act each timestep. REMOVEDe 14: Notice how the timestep is passed into the act method. Dawn Finney.
• Just to clarify: YOU don't have to modify the run() method. What Dawn is showing you is that it's already passing in the time step. But even more important: FOR HOMEWORK 8 and 9, YOU DO NOT NEED THE TIME STEP! You can do everything you need to do without it. Mark Guzdial

 Question: Is it normal to be getting "dry" runs on the simulation? Most of the time, the first time we run it after compiling, no one but the first gets infected. If we run it again, from the interactions pane without compiling it again, it usually runs fine but displays the result from running both the old world again and adding in the new world. Does this sound like a normal problem or would this be a problem with our code? I mean, if we change some things then it works really well killing many people and infecting many. If we change the area of infection or the probability of death then the disease is really deadly.
 Yes, we are.

 Question: Does a Typhoid Mary carrier (or an infected person who is not yet red) consider himself/herself to be healthy?
Since the instructions say a "healthy person" turns away from visibly sick people, does a Typhoid Mary turn away from a visibly sick person?|
• Typhoid Mary's are sick and should show up in infected counts. No one should turn away from them because they are not visibly sick. Mark Guzdial

 Question: We have a question about the graphs. We looked through the slides and we understand how to write in the interactions pane to open a file but not how to tell the file what it should write. We looked at the lineForFile in DiseaseSimulation and saw that it outputted the number of infected but it didn't match the number that was in our excel sheet. Also how would we get it to also output another variable such as the number of dead. We already have it the code to make it show up in the interactions pane but we weren't sure how to make it show up in excel.
• You change lineForFile to define what to write to the file. You must openFile to get data written to the file. It should not show up in the Interactions pane at all. We did this in class and it's in the PowerPoint. Mark Guzdial

 Question: How do you increment the days without using a time step?
• In act(), if (this.infected()) {this.illnessLength++;}. Mark Guzdial
• Remember, act() gets called on EACH agent for EACH timestep. Mark Guzdial
• Here's another hint if your code isn't working right: Select your code, then right click and choose "Indent lines." If things aren't working, it is most likely in your code!. So one of two things is going on, either (a) you don't quite understand how to do it right, and the code is implementing what you currently mean (so it makes your understanding debuggable and explorable! or (b) the code isn't implementing what you mean, even if you do know what needs to happen. In either case, you need to read your code clearly, with a fresh eye, as if someone else wrote, and try to understand what it's saying. You may need to use the debugger or to print values, to better understand the code. The key thing is that you have to understand the code that's in front of you, and then decide if it's right or not. Getting the indentation right can help. Mark Guzdial

 Question: If a person can become healthy again after 7 days, can he get sick again? So does that person get added to the total number of sick people again or are they removed from the number of sick and then added again?
• Yes people can get sick again. Don't worry about counting the number of cured people. He would be removed from the sick because he no longer is, and then added later. Joel Uthe

 Question: `this.turn(randNumGen.nextInt(360));` Is this line in act() in agent.java something we would use to make a healthy dude turn away from red dude?
• It is something similar to what you would use. There is some good code in WDSimulation when the deer are running away from wolves. Joel Uthe

 Question: Is our code supposed to create a graph our do we do that manually with our averages?
• You should be outputting a file with step by step data and then you will import that into excel to create your graphs. Joel Uthe

 Question: How do we get ALL the agents within a range? Not just the closest person in the range like when we use getClosest()?
• Check the political simulation. It uses ranges instead of getClosest()

 Of course healthy people can get sick again. The number of infected should be the number infected at that time. No – look at the wolf and deer code for how to turn away. You graph in Excel as was shown in class MANY times. To get all agents in a range, use getAgents() to get all agents. Look at the simulation examples Mark Guzdial

 Question: The political simulation only counts how many agents are in a range. I want to be able to have my current agent see if anyone in that range is infected and then get infected.
• You'll have to run through the list yourself and test each agent. Joel Uthe

 Question: It says the simulation is suppose to run for 100 timesteps, but we are only suppose to graph the first 30 days. So are we just graphing the first 30 timesteps or 3 timesteps equals 1 day?

 Question/Rant: What the hell I've tried everything for the vaccination. I have a counter that will count the total amount of people that go infected at the end of the run. So when I vaccinate one of them I get 99 total infected if I run it for a long time to the point everyone dies. However, there isn't a turtle moving around my screen that represents the vaccinated person.
• You haven't tried everything, because it can be made to work. It's actually the easiest to implement. Add a vaccinated field. Set it for a few people. In your die and infection methods, don't let it happen if the person is vaccinated. Nothing else changes. Mark Guzdial

### Why aren't you helping one another?

I am getting umpteen messages, asking me, "We're getting these results – is that normal?!?" Why aren't you all helping one another with this?!? We created the Gallery explicitly so that you could share your graphs. I expect to see a dozen graphs up there saying, "This is what we're getting for the base case – how about you?" and another half-dozen for each of the base health policies. Why aren't you helping one another by sharing your results?!? Mark Guzdial

 Question: I have a question. Are we supposed to implement policy 1 and have 3 graphs of it. 3 graphs of no policy implementation. and 3 graphs of policy 2. (or is is supposed to be 3 graphs of policy 1 and 2).
• If you look at the HW8 criteria page, you can see that we're looking for 9 graphs. So yes, 3 for no policy, 3 for 1 policy, and 3 for both. Derek Olejnik

 Question: Are we turning in two different PersonAgent's? One with policy one and the second with policy two?
• Just one PersonAgent and one DiseaseSimulation. Dawn Finney.

 Question: How do we write our our results as a string?
• Simulation has this functionality built in for you. First, you need to call the function openFile() when you are setting up your simulation. Then, override the lineToFile function with what you want to output to the file after each step. Derek Olejnik.

 Question: I can't find anywhere on the homework page that tells us we need to be outputting our results to a file. Will we be counted off if we do the graphs manually?
• You get that from coming to class and seeing how we generated the graphs in class. Mark Guzdial.