java swarm_java for swarm 学习笔记4

学习笔记3没有体现面向对象。学习笔记4的功能和3是一样的,只不过用面向对象的方法来实现,就是先申明一个SimpleBug类,类有数据成员,成员函数,构造函数。

然后在StartSimpleBug中调用SimpleBug类。

需要掌握

1 import swarm.Globals;//任何类申明时都要用到。

2 import swarm.defobj.Zone;//Zone的定义

3 import swarm.objectbase.SwarmObjectImpl;//SwarmObjectImpl的定义

4 构造函数有六个参数

public SimpleBug(Zone aZone, int wXSize, int wYSize, int X, int Y,

int bNum)

Zone 参数表示世界的初始化。

SimpleBug类:

/SimpleJavaBug/SimpleBug.java

// SimpleBug.java

// Defines the class for our SimpleBug agents.

import swarm.Globals;

import swarm.defobj.Zone;

import swarm.objectbase.SwarmObjectImpl;

public class SimpleBug extends SwarmObjectImpl

{

// These instance variables keep track of the size of a given

// bug's world, its position within it, and its identity.

int worldXSize;

int worldYSize;

int xPos;

int yPos;

int bugNumber;

// Constructor to create a SimpleBug object in Zone aZone and to

// place it at the specified X,Y location in its world. The bug

// is also given a numeric id, bNum.

public SimpleBug(Zone aZone, int wXSize, int wYSize, int X, int Y,

int bNum)//构造函数

{

// Call the contructor for the bug's parent class.

super(aZone);//暂时没搞明白

// Record the bug's world size, its initial position and id

// number.

worldXSize = wXSize;

worldYSize = wYSize;

xPos = X;

yPos = Y;

bugNumber = bNum;

// Announce the bug's presence to the console.

System.out.println("SimpleBug number " + bugNumber +

" has been created at " + xPos + ", " + yPos);

}

// This is the method to have the bug take a random walk backward

// (-1), forward (+1), or not at all (0) in first the X and then

// the Y direction. The randomWalk method uses

// getIntegerWithMin$withMax() to return an integer between a

// minimum and maximum value, here between -1 and +1.

// Globals.env.uniformRand is an instance of the class

// UniformIntegerDistImpl, instantiated by the call to

// Globals.env.initSwarm in StartSimpleBug. Note that the bug's

// world is a torus. If the bug walks off the edge of its

// rectangular world, it is magically transported (via the modulus

// operator) to the opposite edge.

public void randomWalk()

{

xPos += Globals.env.uniformIntRand.getIntegerWithMin$withMax(

-1, 1);

yPos += Globals.env.uniformIntRand.getIntegerWithMin$withMax(

-1, 1);

xPos = (xPos + worldXSize) % worldXSize;

yPos = (yPos + worldYSize) % worldYSize;

}

// Method to report the bug's location to the console.

public void reportPosition()

{

System.out.println("Bug " + bugNumber + " is at " + xPos +

", " + yPos);

}

}

StartSimpleBug类:

在这里需要掌握:

1   Globals.env.initSwarm(),这是Swarm的Global环境的一个静态方法。这个方法建立了Swarm的全局变量和方法,每一个Swarm程序都以它开始。它有四个参数:仿真的类的名字、Swarm的版本、报告Swarm漏洞的email地址以及运行程序的命令字符串。

/SimpleJavaBug/StartSimpleBug.java

// StartSimpleBug.java

// The Java SimpleBug application.

import swarm.Globals;

import swarm.defobj.Zone;

public class StartSimpleBug

{// The size of the bug's world and its initial position.

static int worldXSize = 80;

static int worldYSize = 80;

static int xPos = 40;

static int yPos = 40;

public static void main (String[] args)

{

int i;

SimpleBug abug;

// Swarm initialization: all Swarm apps must call this first.

Globals.env.initSwarm ("SimpleBug", "2.1",

"bug-swarm@santafe.edu", args); //在这个过程中声明了SimpleBug用到的随机生成函数

// Create an instance of a SimpleBug, abug, and place it

// within its world at (xPos, yPos). The bug is created in

// Swarm's globalZone and is given a "bug id" of 1.

abug = new SimpleBug(Globals.env.globalZone, worldXSize, worldYSize,

xPos, yPos, 1);

// Loop our bug through a series of random walks asking it to

// report its position after each one.

for (i = 0; i < 100; i++)

{

abug.randomWalk();

abug.reportPosition();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值