001package algs12; 002import stdlib.*; 003/* *********************************************************************** 004 * Compilation: javac FlipsMax.java 005 * Execution: java FlipsMax N 006 * Dependencies: Counter.java StdRandom.java StdOut.java 007 * 008 * A static method that takes two objects as arguments and returns 009 * an object. 010 * 011 * % java FlipsMax 1000000 012 * 500281 tails wins 013 * 014 *************************************************************************/ 015 016public class XFlipsMax { 017 018 public static Counter max(Counter x, Counter y) { 019 if (x.tally() > y.tally()) return x; 020 else return y; 021 } 022 023 public static void main(String[] args) { 024 args = new String[] { "1000000" }; 025 026 int T = Integer.parseInt(args[0]); 027 Counter heads = new Counter("heads"); 028 Counter tails = new Counter("tails"); 029 for (int t = 0; t < T; t++) { 030 if (StdRandom.bernoulli(0.5)) heads.increment(); 031 else tails.increment(); 032 } 033 034 if (heads.tally() == tails.tally()) 035 StdOut.println("Tie"); 036 else 037 StdOut.println(max(heads, tails) + " wins"); 038 } 039} 040