Data storage
[Agent practical 1 of 9]
|
|
Start by copying your HelloWorld file to a new directory: \java\src\unpackaged\framework1.
Each practical we'll take a fresh copy of the code to work on, so you can always backtrack to something
that works and see how the projects build up. Once you've got the code copied, change the
class name to Model, and the filename to match. Compile the code using "javac Model.java
" and check it works using
"java Model
".
Why have we done something so simple? As we said in the last lecture, the best way to code is to build up a few lines at a time, and compile regularly. We've built and tested something so simple so we can now have confidence to build on it.
Note that it is always worth thinking about how you would test any code you write to make sure it is working ok.
Now build a second class, "Agent" in a second file, "Agent.java" in the same directory. It doesn't need a main block, just an empty class block.
Again, compile the files - this time using "javac *.java" to compile both files. You should
still be able to run the Model class using "java Model
".
Doomed: What happens if you try
"java Agent
", given that Agent doesn't have a main block?*
*Note that if we get you to try something that will definitely fail, we'll put it in red with the word "Doomed:" infront. The idea with these is to get you use to seeing particular debugging messages and recognising what causes them.
Now make an object variable of type "Agent" in the main method of the Model class. Call it "agent" (lower case), so we know what it is (we could call it anything - "harry" if we wanted - it just wouldn't be very helpful).
If you can't remember the syntax for making a new object and attaching it to an object label, look it up in on the Key Ideas page for the last lecture (if you want to open both the Key Ideas page and this practical at the same time in different tabs (which might be useful), see the Firefox/Chrome tutorial for advice).
Again, compile all the files and use "java Model
" to start the
model running via the main "Model" class. As this has an Agent object in it, it will automatically
find the Agent file and use it to build an object
(at the moment it has to be in the same directory to do this -- we'll look later in the course at
connecting different directories).
Congratulations, if that compiled, you've built an Agent-Based Model! Ok, it doesn't do much, but we've got to start somewhere!
Ultimately we're going to make an array of agents, but for now we'll stick with one, and we'll move on to giving our agent a location in space.
In the Agent class, inside the class block, make two int
variables:
x
and y
. Set x to 50 and y to 150, so we can tell the variables apart if
we see the figures. Compile the code.
Now, back in the Model class, see if you can use the dot operator to
get hold of the x
variable inside the agent
object and print it to the
screen. You can either get hold of the value and attach it to another variable label inside Model, or
directly dump it straight into System.out.println()
. Compile the code and
run it. If you're unsure how to do this, see the Point
example given in the lecture materials on Objects or the Key Ideas page.
Have a play if you like. See if you can set the agent's x
variable to 100 from within
Model and print it out from within Model to check it has been set ok. Try making another
agent (remember, it'll need the same class type, but a different name) and convince yourself
that setting the x
variable in one doesn't set it in both. Get used to the idea that you
can use the dot operator to look inside the agent object for its variables.
Once you're happy with that, we'll move on to set up an array of agents.