zoukankan      html  css  js  c++  java
  • 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

     

  • 相关阅读:
    [Outlook] Outlook2013能收但无法发送邮件-0x800CCC13, 0x800CCC0B, 0x8004210B
    [Mobile] 手机浏览器输入框-数字输入框
    [Qcon] 百姓网开发总结
    [QCon] Scrum阅读随想
    [Spring] 事务级别定义
    [Monitor] 监控规则定义
    [Spring Batch] 图解Spring Batch原理
    [JavaCore] 微信手机浏览器版本判断
    Python 编码简单说
    矩阵或多维数组两种常用实现方法
  • 原文地址:https://www.cnblogs.com/pacoson/p/4003489.html
Copyright © 2011-2022 走看看