Flips测试类(page43)
测试用例:所用java类: StdOut,StdIn , Counter, StdRandom,
public class Flips {
public static void main(String[] args) {
int T = Integer.parseInt(args[0]);
Counter heads = new Counter("heads");
Counter tails = new Counter("tails");
for (int t = 0; t < T; t++)
if(StdRandom.bernoulli(0.5))
heads.increment();
else tails.increment();
StdOut.println(heads);
StdOut.println(tails);
int d = heads.tally() - tails.tally();
StdOut.println("delta : " + Math.abs(d));
}
}
打印结果:public class Counter implements Comparable<Counter> {
private final String name; // counter name
private int count = 0; // current value
/**
* Initializes a new counter starting at 0, with the given id.
* @param id the name of the counter
*/
public Counter(String id) {
name = id;
}
/**
* Increments the counter by 1.
*/
public void increment() {
count++;
}
/**
* Returns the current count.
*/
public int tally() {
return count;
}
/**
* Returns a string representation of this counter
*/
public String toString() {
return count + " " + name;
}
/**
* Compares this counter to that counter.
*/
public int compareTo(Counter that) {
if (this.count < that.count) return -1;
else if (this.count > that.count) return +1;
else return 0;
}
}

public class Flips {
public static void main(String[] args) {
Counter c1 = new Counter("ones");
c1.increment();
Counter c2 = c1;
c2.increment();
StdOut.println(c1);
}
}
//赋值语句的区别: 复制的是引用类型还是原始数据类型。
打印结果:
2 ones
Flips测试类(page43)的更多相关文章
- Spring-test使用JUnit时,测试类autowired报错,create bean error
Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...
- 22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表。然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法showB输出大写的英文字母表。最后编写主类C,在主类的main方法 中测试类A与类B。
22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表.然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法sh ...
- Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
#29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...
- 在Eclipse中生成接口的JUnit测试类
在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...
- TestNG之执行测试类方式
TestNG提供了很多执行方式,下面做简单介绍. 1.XML指明测试类,按照类名执行,其中可以指定包名,也可指定无包名: 带包名,运行ParameterSample类和ParameterTest类 & ...
- XCode中的单元测试:编写测试类和方法(内容意译自苹果官方文档)
当你在工程中通过测试导航栏添加了一个测试target之后, xcode会在测试导航栏中显示该target所属的测试类和方法. 这一章演示了怎么创建测试类,以及如何编写测试方法. 测试targets, ...
- 各种数据库连接代码的测试类(java)
测试类: public class Mytest { Connection conn=null; Statement stmt=null; String myDriver="com.mysq ...
- 编写测试类,了解ArrayList的方法
这篇文章主要介绍了C#中动态数组用法,实例分析了C#中ArrayList实现动态数组的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了C#中动态数组用法.分享给大家供大家参考.具体分析如下 ...
- maven编译的时候排除junit测试类
maven项目中使用junit进行单元测试,在进行编译的时候,可以通过2种方式排除test测试类的编译. 有2种方式 : 使用命令的时候带上参数 mvn install -Dmaven.test.sk ...
随机推荐
- static和extern的区别
extern(外部) 1.对函数 完整的定义一个外部函数(可以省略extern) 完整的声明一个外部函数(可以省略extern) 2.对变量 只能声明一个外部变量(不能省 ...
- Jupyter增加内核
本例的Jupyter安装在Python3下,以增加Python2内核为例. 首先确认在Python3下已安装了内核: ipython kernel install --user #or python3 ...
- [转] [Visual Studio 2012] 找回 建立單元測試 選單
原文链接:http://www.dotblogs.com.tw/yc421206/archive/2013/03/08/95920.aspx Step1.建立選單 在VS2012選單,Tools→Cu ...
- Sublime Text3 激活教程
Sublime Text3激活 在使用Sublime时会定期弹出购买提示框,避免出现购买提示,影响工作效率,我们可以使用网上的激活码,虽然有些不厚道,但是工作以后,一定选择购买正版支持一下. 打开Su ...
- Hello OSGI --- Apache Felix
Apache Felix Felix是一个OSGi版本4规范的Apache实现. OSGi是一个基于Java的服务平台规范,其目标是被需要长时间运行.动态更新.对运行环境破坏最小化的系统所使用.有许多 ...
- Extjs随笔
{ columnWidth : .1, layout : 'form', items : [{ fieldLabel : "至", labelSeparator:'', label ...
- c语言操作符 “++”另类行为
正常情况下,我们使用++ int a = 1; a++; printf(“%d”,a); // 2; 很简单没什么好说的. #include <stdio.h> int main() ...
- map的实现
1.map的实现是使用平衡树,AVL树或者红黑树. 2.在无序的情况下,查找为常数时间.有序的时候,查找为对数时间.二叉排序树(BST)就是为了解决这个问题. 3.但是,极端情况下,BST的查找效率退 ...
- Codeforces Beta Round #13 E. Holes 分块暴力
E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...
- Nginx_handler模块发开(hello模块结构解析)
声明:请在文章页面明显位置给出原文连接 http://www.cnblogs.com/paulweihan/p/4654173.html,否则保留追究法律责任的权利. 近期查了非常多资料.入门的样例都 ...