Data storage
[Agent practical 1 of 9]


Having now built an array of Agents with variables inside them, we'll now do the reverse, and build a single Environment, with an array of data inside it...


Make a new class Environment, in an Environment.java file, inside the same directory as the other files. Again, for now, it just needs a class block.

Then clean up the Model class, so it looks like this:

class Model {
   public static void main (String args[]) {
      Agent[] agents = new Agent[3];
   }
}

Then, at the bottom of the main block (but inside the main block), make an object out of the Environment class. Call it world. Remember, we're making a single object, as we did at first with our Agent class, not an array. Compile and test your code still runs.

 


Next, open up the Environment class, and, inside the class block, make a 2D array with the following qualities:

Type: double[][]
Name: data
Size: [2] by [3]

Note that, unlike our Agent array, this one has two dimensions.

 


Back in the Model class main block, you should now be able to set values inside this array, for example, doing:

world.data[1][2] = 20.0;

Add this code to the Model class and then see if you can you get the value of world.data[1][2] back out and print it to the screen.

Doomed: What happens if you access a value you haven't yet set up, like world.data[0][2]? How is this different from when we tried to access an Agent that didn't exist? (note the difference between a primitive array (like this) and an object array (like the array of Agents)).

Once you're happy you understand setting and getting the values out of the world.data array from inside the Model class, fill all the locations in the array with values.

Doomed: After the point where you've made and filled the data array, add the following code:

System.out.println("Number at [0][0] = " + world.data[0][0]);
System.out.println("Number at [1][1] = " + world.data[1][1]);
System.out.println("Number at [3][2] = " + world.data[3][2]);

If any of these lines don't work, make a note of the error message you get back when you run the code, and see if you can work out why it doesn't work. What line number is it complaining about? Why do you think this is? Fix it.

 


Once you've finished that lot we're done for this practical.

Hopefully, we've done two things. Firstly, you should now be familiar with the following elements of coding:

This may seem like a confusing collection of stuff, and it is, but the key idea is thinking through what variable is inside what object, and using the dot operator to access it. You then need to think about what the label looks like, so, for example, if you want to set a primitive value inside an object, this works:

agent.x = 10.0;

but if you want to set a value inside an array in an object, this won't work:

world.data = 10.0;

because world.data is the label for an array, not a position within an array. It needs to be:

world.data[0][1] = 10.0;

The second thing we've done is to build up the basic elements of our Agent-Based Model. Indeed, we've actually got the complete structure built in one practical -- now all we have to do is fill it up!