学习笔记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();

}

}

}

java swarm_java for swarm 学习笔记4相关推荐

  1. 零基础学习Java开发,这些学习笔记送给你

    因为Java具备很多特点,并且在企业中被广泛应用为此很多小伙伴选择学习Java基础开发,但是零基础学习Java技术开发需要我们制定Java学习路线图对于我们之后的学习会非常有帮助. 零基础学习Java ...

  2. Java 8 函数式编程学习笔记

    Java 8 函数式编程学习笔记 @(JAVASE)[java8, 函数式编程, lambda] Java 8 函数式编程学习笔记 参考内容 Java 8中重要的函数接口 扩展函数接口 常用的流操作 ...

  3. 【Java】函数式编程学习笔记——Stream流

    学习视频:https://www.bilibili.com/video/BV1Gh41187uR?p=1 (1)[Java]函数式编程学习笔记--Lambda表达式 (2)[Java]函数式编程学习笔 ...

  4. java 编程思想 多线程学习笔记

    java 编程思想 多线程学习笔记 一.如何创建多线程? 1.继承 java.lang.Thread 类 2.实现 java.lang.Runnable 接口 3.Callable接口 总之,在任何线 ...

  5. JAVA基础与高级学习笔记

    JAVA基础与高级学习笔记 /记录java基础与高级,除了较简单的内容,没有必要记录的没有记录外,其余的都记录了/ java初学者看这一篇就够了,全文 6万+ 字. JAVA基础 java会出现内存溢 ...

  6. 第10课:底实战详解使用Java开发Spark程序学习笔记

    第10课:底实战详解使用Java开发Spark程序学习笔记 本期内容: 1. 为什么要使用Java? 2. 使用Java开发Spark实战 3. 使用Java开发Spark的Local和Cluster ...

  7. Java并发编程艺术学习笔记(五)

    Java并发编程艺术学习笔记(五) Java并发容器和框架 Java为开发者也提供了许多开发容器和框架,可以从每节的原理分析来学习其中精妙的并发程序. 一.ConcurrentHashMap的实现原理 ...

  8. 《Java编程思想》学习笔记【一对象导论】

    重头学习Java,大一没怎么学,大二上课也没听.(流下不学无术的眼泪) 所有编程语言都提供抽象机制,我们所能解决的问题的复杂性直接取决于抽象的类型和质量. 汇编语言是对底层机器的轻微抽象," ...

  9. Java之GUI编程学习笔记六 —— AWT相关(画笔paint、鼠标监听事件、模拟画图工具)

    Java之GUI编程学习笔记六 -- AWT相关(画笔paint) 参考教程B站狂神https://www.bilibili.com/video/BV1DJ411B75F 了解paint Frame自 ...

最新文章

  1. 使用jQuery和YQL,以Ajax方式加载外部内容
  2. itchat 动态注册
  3. Java程序员大神给初学者的学习方法路线建议
  4. 宅家过年 | 程序员消遣活动指南
  5. 旋转炫酷相册-快制作你喜欢源码
  6. c语言time.h时区不对,用C语言修改系统时区,发现一堆问题,请各位大侠不吝赐教。...
  7. shell 学习之for语句
  8. Coding Interview Guide -- 判断二叉树是否为平衡二叉树
  9. mumu 模拟器连不上adb
  10. 微信表情包 php,教你用PS给你的室友做一套动态微信豪华表情包
  11. html自定义属性jquery怎么拿到,jquery 获取自定义属性(attr和prop)的实现代码
  12. IT痴汉的工作现状23-乡关何处
  13. Natural number
  14. 如何设计一个可扩展的优惠券功能
  15. 后台管理登陆注册跳转以及基础模板 附Gitlab源码
  16. 处理IRP的几种方式
  17. javascript中加减时间
  18. 计算机安全原理与实践第3版PDF,windows安全原理与技术.pdf
  19. input 金额格式校验
  20. 信息学奥赛一本通 1302:股票买卖 | OpenJudge NOI 2.6 8464:股票买卖

热门文章

  1. 浅谈linux中shell变量$#,$@,$0,$1,$2,$?的含义解释
  2. 分布式监控系统开发【day38】:报警阈值程序逻辑解析(三)
  3. [转]jQuery ListBox Plugin(ListBox插件)
  4. 通过编写串口助手工具学习MFC过程——(三)Unicode字符集的宽字符和多字节字符转换...
  5. 深入理解 C 语言的函数调用过程
  6. mysql与mysqli
  7. smarty一维数组的引用
  8. docker安装tomcat下的日志查看
  9. 实体类 接口_Java 语言基础编程题 (二维数组, 五子棋游戏, 实体类和接口)
  10. 获取多个复选框的value