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

时间: 2024-08-24 13:47:44

Flips测试类(page43)的相关文章

JUnit 3.8 演示递归删除文件目录的 测试类程序 .

用递归方式来实现删除硬盘的文件或目录(空文件夹) 首先要找到递归的入口及出口,这点很重要,成败在此,呵呵! 代码实现: [java] view plain copy import java.io.File ; class RecursionDeleteFileDemo //利用递归 删除 文件或目录 操作 { public static void deleteFiles(File file) { //递归出口 //判断目前文件,如果是文件 或 是一个空的文件夹,则删除 if(file.isFil

需求:有一个猜数字小游戏,请写一个程序实现在测试类中只能使用5次,超过5次提示:游戏试玩结束,请付费。

package cn.idcast4; import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Reader;import java.io.Writer;import java.util.Properties; /* * 需求:有一个猜数字小游戏,请写一个程序实现在测试类中只能使用5次, *

android编写测试类

由于网上很多教程,我就不多说.在这里我就说说我遇到的问题. 在android studio编写单元测试类中,在androidTest包下编写了两个类,分别如下 import junit.framework.TestCase; public class Test extends TestCase { Test1 mc = new Test1(); public void test(){ int num = mc.testMethod(1, 2); // System.out.println("这是

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

#29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类Truck是Car类的子类,其中包含的属性有载重量payload.每个 类都有构造方法和输出相关数据的方法.最后,写一个测试类来测试这些类的功 能. package hanqi; public class Vehicle { private int wheels; private int weight

spring hibernate 事务整合 使用测试类 事务不自动提交的问题!!!

使用JUnit 测试hibernate 事务管理的时候应注意 ,测试类完成是默认回滚的. 所以只能查询数据库却不能增删改数据库. 应该在测试类上面加上注解 @Rollback(false)  表似默认不回滚. package TestHibernate; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import or

JAVA测试类

用eclipse引入JUnit包 创建测试类 添加待测试类,我这里继承了之前创建的测试类,如果不继承的话  可以直接从测试类的test()里写测试代码 运行方式调成JUnit测试,完成

WebService之CXF注解之四(测试类)

TeacherTest.java: /** * @Title:TeacherTest.java * @Package:com.test.service * @Description: * @author:Youhaidong(游海东) * @date:2014-5-5 下午11:14:09 * @version V1.0 */ package com.test.service; import org.apache.cxf.interceptor.LoggingInInterceptor; imp

一次运行多个测试类2-1个测试类重复测试

package com.mengdd.junit; import junit.extensions.RepeatedTest; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; public class TestAll extends TestCase { public static Test suite() { // 创建一个测试套件 TestSuite

Junit 核心&mdash;&mdash;测试类(TestCase)、测试集(TestSuite)、测试运行器(TestRunner)

首先,把这三个定义简单的说明一下: 1.测试类(TestCase):一个包含一个或是多个测试的类,在Junit中就是指的是包含那些带有@Test注解的方法的类,同一样也被称作"测试用例"; 2.测试集(TestSuite):测试集是把多个相关测试归入一个组的表达方式,在Junit中,如果我们没有明确的定义一个测试集,那么Juint会自动的提供一个测试集,一个测试集一般将同一个包的测试类归入一组: 3.测试运行器(TestRunner):执行测试集的程序. 定义说完了,还有两个好理解的定