Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

FinalExam Review Sp2005: Slow! Sick People Crossing!

Post Answers, Questions, Comments, etc. here.

(Back to Final Exam Review Sp2005)


import java.awt.Color; // Color for colorizing
import java.util.REMOVEDkedList;

/**
 *PersonAgent -- Person as a subclass of Agent**/
public class PersonAgent extends Agent {

  public boolean infection;
  
  /*** Initialize, by setting color and making move fast
   **/
  public void init(Simulation thisSim){
    // Do the normal initializations
    super.init(thisSim);
    
    // Make it lightGray
    setColor(Color.lightGray);
    
    // Don't need to see the trail
    setPenDown(false);
    
    // Start out uninfected
    infection = false;
    
    // Make the speed large
    speed = 100;
  }
  
  /**
   *Count infected**/
  public int infected() {
    int count = 0;
    REMOVEDkedList agents = simulation.getAgents();
    PersonAgent check;
    
    for (int i = 0; iagents.size(); i++){
      check = (PersonAgent) agents.get(i);
      if (check.infection) {count++;}
    }
    
    return count;
  }
  /*** Become infected
   **/
  public void infect(){
    this.infection = true;
    this.setColor(Color.red);
    
    // Print out count of number infected
    System.out.println("Number infected: "+infected());
  }
  
  /**
   *How a Person acts**/
  public void act()
  {
    // Is there a person within infection range of me?
    PersonAgent closePerson = (PersonAgent) getClosest(20,
                                simulation.getAgents());
    
    if (closePerson != null) {
      // If this person is infected, and I'm not infected
      if (closePerson.infection && !this.infection) {
        // I become infected
        this.infect();
        //once infected, slow down by 75%
        this.foward(speed*(0.25));
    }
    }

    // Run the normal act() -- wander aimlessly
    super.act();
  }
  
    ////////////////////////////// Constructors ////////////////////////
  // Copy this section AS-IS into subclasses, but rename Agent to 
  // Your class.
  
  /**
   *Constructor that takes the model display (the original* position will be randomly assigned)
   *@param modelDisplayer thing that displays the model
   * @param thisSim my simulation
   */
  public PersonAgent (ModelDisplay modelDisplayer,Simulation thisSim)
  {
    super(randNumGen.nextInt(modelDisplayer.getWidth()),
          randNumGen.nextInt(modelDisplayer.getHeight()),
          modelDisplayer, thisSim);
  }
  
  /**Constructor that takes the x and y and a model* display to draw it on
   *@param x the starting x position
   * @param y the starting y position
   *@param modelDisplayer the thing that displays the model
   * @param thisSim my simulation
   /
  public PersonAgent (int x, int y, ModelDisplay modelDisplayer, 
               Simulation thisSim) 
  {
    // let the parent constructor handle it
    super(x,y,modelDisplayer,thisSim);
  }
  

}


Can you multiple speed by a factor?



Since speed set to be 100 at the top of the page, 25 is 75% slower, so the only thing that needs to be changed is the value of speed, and you do this at the same time the turtle becomes infected. This is the entire Act method with the speed set to 25.

 public void act()
  {
    // Is there a person within infection range of me?
    PersonAgent closePerson = (PersonAgent) getClosest(20,
                                simulation.getAgents());
    
    if (closePerson != null) {
      // If this person is infected, and I'm not infected
      if (closePerson.infection & !this.infection) {
        // I become infected
        this.infect();
    }
    }

    // Run the normal act() -- wander aimlessly
    super.act();
  }


On the latter example, can you show what infect looks like now? Mark Guzdial

Actually, at first i had not changed changed infect, but i went back and put speed =25; into the infect method. This actually works a little better than before.

Why does it matter if you change the speed in the act method (ex: this.forward ((int) (speed/4))) vs. doing it in the infect method...or am i misunderstanding and it really doesn't matter?

It doesn't matter in terms of "it working." Either way will work. However, it's a better design to change infect(). Then it's clear that the infect() method does everything it needs to in terms of making a person "sick." Leaving the reduction of speed in act() says "First, you get infected, and then you reduce your speed" which really isn't right in terms of a model of the real world. In the real world, your speed gets reduced because you're infected–not an afterthought. Does that make sense? Mark Guzdial




Link to this Page