测试用例:所用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)的更多相关文章

  1. Spring-test使用JUnit时,测试类autowired报错,create bean error

    Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...

  2. 22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表。然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法showB输出大写的英文字母表。最后编写主类C,在主类的main方法 中测试类A与类B。

    22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表.然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法sh ...

  3. Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。

    #29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...

  4. 在Eclipse中生成接口的JUnit测试类

    在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...

  5. TestNG之执行测试类方式

    TestNG提供了很多执行方式,下面做简单介绍. 1.XML指明测试类,按照类名执行,其中可以指定包名,也可指定无包名: 带包名,运行ParameterSample类和ParameterTest类 & ...

  6. XCode中的单元测试:编写测试类和方法(内容意译自苹果官方文档)

    当你在工程中通过测试导航栏添加了一个测试target之后, xcode会在测试导航栏中显示该target所属的测试类和方法. 这一章演示了怎么创建测试类,以及如何编写测试方法. 测试targets, ...

  7. 各种数据库连接代码的测试类(java)

    测试类: public class Mytest { Connection conn=null; Statement stmt=null; String myDriver="com.mysq ...

  8. 编写测试类,了解ArrayList的方法

    这篇文章主要介绍了C#中动态数组用法,实例分析了C#中ArrayList实现动态数组的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了C#中动态数组用法.分享给大家供大家参考.具体分析如下 ...

  9. maven编译的时候排除junit测试类

    maven项目中使用junit进行单元测试,在进行编译的时候,可以通过2种方式排除test测试类的编译. 有2种方式 : 使用命令的时候带上参数 mvn install -Dmaven.test.sk ...

随机推荐

  1. My集合框架第四弹 HashTable(链表解决冲突)

    package com.wpr.collection; import java.util.LinkedList; import java.util.List; public class HashTab ...

  2. 从scanf的学习接口设计

    对大多数程序员来说scanf可以能是最熟悉,也是陌生的工具.在学习C语言时,大家一定没少用它,但是对它也知道不多.比如说,它有哪些可能的返回值?又比如怎么样才能跳过回车,读一个字符?我们可以一起来研究 ...

  3. 机器学习笔记之人工神经网络(ANN)

    人工神经网络(ANN)提供了一种普遍而且实际的方法从样例中学习值为实数.离散值或向量函数.人工神经网络由一系列简单的单元相互连接构成,其中每个单元有一定数量的实值输入,并产生单一的实值输出. 上面是一 ...

  4. Windows Server Backup 2008 R2 备份Hyper-V

    要备份 Hyper-V 虚拟机从父分区在 Windows Server 2008 上使用 Windows 服务器备份,您必须注册 Microsoft Hyper-V VSS 编写器 Windows 服 ...

  5. PE文件数字签名信息读取存储及格式具体解释图之上(历史代码,贴出学习)

    // 注意下图PE文件格式具体解释图中的 // IMAGE_NT_HEADERS------->OptionalHeader------>DataDirectory[IMAGE_DIREC ...

  6. Python-面向对象 (二 继承)

    一 继承   基类定义例如以下: class people:     #define attribute     name = ''     age  = 0     #define private ...

  7. JAVA组程序优化综合考试试题

    题目原型: 有一张标准的树状结构表,里面有Structure_Id和 Parent_Id两个关键列,记录了结点的父子关系.现在要求添加一个字段为 Structure_Code ,标记为 三位一个节点关 ...

  8. 企业应用架构模式阅读笔记 - Martin Fowler

    1. 数据读取

  9. Codeforces Gym 100531D Digits 暴力

    Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...

  10. Codeforces Gym 100463D Evil DFS

    Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...