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

PreQuiz 4 - Spring 2006

Below is the code for act() in the class PersonAgent, as part of the DiseaseSimulation.

  /**
   * 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();
      }
    }

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


Imagine that we want to change how people behave such that:

Rewrite act() to implement these new rules.

Questions? Comments? Answers? Comments on Answers?


Well, it sorta depends on how we can access the person's speed, doesn't it?
I mean if we don't know that... I'm assuming that a person's speed is called 'speed' (as it is in PersonAgent).
  /**
   * 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();
        // If the closest person within 10 returns the same person, Extra Infect!
        if( ((PersonAgent) getClosest(10,simulation.getAgents())).equals(closePerson)){
/****** if( (PersonAgent) getClosest(10,simulation.getAgents())==closePerson){ *********/
          this.getSpeed()=this.setSpeed()/2;  
        }
      }
    }

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

Yes? No?
~Jim?

I was thinking of something a lot simpler, without .equals. You can use getSpeed and setSpeed. Mark Guzdial

Oh, ok. Well, I can do it with 2 closest person checks...
  /**
   * How a Person acts
   **/
  public void act()
  {
    // Is there a person within infection range of me?
    PersonAgent closePerson = (PersonAgent) getClosest(10,simulation.getAgents());
    if (closePerson != null) {
      // If this person is infected, and I'm not infected
      if (closePerson.infection && !this.infection) {
        // I slow down and become infected...
        this.setSpeed(this.getSpeed()/2);
        this.infect();
      }
    }
    else{
    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();
  }

I suppose that would work too? Not sure what else you'd be looking for.
~Jim

The .equals looks a little easier to me....and shorter. It cuts out the need to redefine closePerson twice.

I agree. Actually, I'm wondering if you could use == for that one, since you want the same object. The 'content' isn't important (whatever it is). I think getting the memory location would work here.
Can I get a yes/no on that?
~Jim



Link to this Page