5 Replies - 7881 Views - Last Post: 30 January 2012 - 12:36 PM Rate Topic: -----

#1 psuggs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-January 12

I'm confused, error in code.

Posted 30 January 2012 - 11:37 AM

You are supposed to do this: Extend Critter to make a new BlackPlagueCritter that will turn all neighboring actors(not rocks) black. Override the processActors method so that all neighboring actors, except for Rocks and other BlackPlagueCritters, are turned black. Use the setColor method to change the color of the neighboring actors.

The Code I inputed changed the color of the Critter to match the color of the actor it surronded , it did not change it's neighboring actors colors to black. My teacher is telling me to add a for lood but i do not know where to add one.

My Code:
// A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Class -
//Lab  -

import java.awt.Color;
import java.util.ArrayList;
import info.gridworld.grid.Grid;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;

public class BlackPlagueCritter extends Critter
{
	//processActors will turn all Actors around this critter Black
	//unless the Actor is a Rock or another BlackPlagueCritter
  public void processActors(ArrayList<Actor> dudes)
  {
	   int n = dudes.size();
	   if(n == 0)
	   {
	   return;
           }
	   int r = (int) (Math.random() *n );
	   Actor other = dudes.get(r);
	   setColor(other.getColor()); 
	   
   }
  }

____________________________________________________________________________-
In the Main Class:
// A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Class -
//Lab  -

import java.awt.Color;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;
import info.gridworld.actor.Flower;
import info.gridworld.actor.Bug;
import info.gridworld.grid.Location;
import info.gridworld.grid.BoundedGrid;
import info.gridworld.actor.ActorWorld;

public class BlackPlagueCritterRunner
{
	public static void main(String[] args)
	{
		ActorWorld world = new ActorWorld(new BoundedGrid<Actor>(8,8));
		world.add(new Location(1, 1), new BlackPlagueCritter());
		world.add(new Location(3, 1), new Rock());
		world.add(new Location(5, 2), new Actor());
		world.add(new Location(7, 6), new Flower());
		world.add(new Location(6, 6), new Actor());
		world.add(new Location(0, 5), new Actor());
		world.add(new Location(2, 6), new Bug(Color.GREEN));
		world.add(new Location(3, 5), new Actor());				
		world.show(); 	}
}

This post has been edited by jon.kiparsky: 30 January 2012 - 11:42 AM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I'm confused, error in code.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,417
  • Joined: 27-December 08

Re: I'm confused, error in code.

Posted 30 January 2012 - 11:53 AM

Moved to GridWorld. Look at your method header. Loop through the parameter ArrayList<Actor> named dudes.
public void processActors(ArrayList<Actor> dudes)


Was This Post Helpful? 0
  • +
  • -

#3 psuggs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-January 12

Re: I'm confused, error in code.

Posted 30 January 2012 - 11:54 AM

What Do YOu Mean By That?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,417
  • Joined: 27-December 08

Re: I'm confused, error in code.

Posted 30 January 2012 - 11:56 AM

Example:
for(int i = 0; i < dudes.size(); i++){
   //deal with dudes.get(i)
}

//or
for(Actor a:dudes){
   //deal with a
}



You should read more up on loops, arrays, and ArrayLists as well.
Was This Post Helpful? 0
  • +
  • -

#5 psuggs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-January 12

Re: I'm confused, error in code.

Posted 30 January 2012 - 11:58 AM

Alright I'll Try that. Thank You
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,417
  • Joined: 27-December 08

Re: I'm confused, error in code.

Posted 30 January 2012 - 12:36 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1