Hi everyone. I'm writing a program that is using a class called Coin.java, that flips a coin. The program is supposed to find the longest run of Heads in 100 flips of the coin. I'm having trouble returning the final value of how many heads were flipped. In order to return this value I need to use the toString method that is included in the Coin class that was provided from my lab book. What paramaters am I supposed to put in the "System.out.println()" statement in order to return the toString? Below is the Coin class with my program below it. Thanks for your help!
Coin class
Runs programpublic class Coin { public final int HEADS = 0; public final int TAILS = 1; private int face; // --------------------------------------------- // Sets up the coin by flipping it initially. // --------------------------------------------- public Coin () { flip(); } // ----------------------------------------------- // Flips the coin by randomly choosing a face. // ----------------------------------------------- public void flip() { face = (int) (Math.random() * 2); } // --------------------------------------------------------- // Returns true if the current face of the coin is heads. // --------------------------------------------------------- public boolean isHeads() { return (face == HEADS); } // ---------------------------------------------------- // Returns the current face of the coin as a string. // ---------------------------------------------------- public String toString() { String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; } }
public class Runs { public static void main (String[] args) { final int FLIPS = 100; // number of coin flips int currentRun = 0; // length of the current run of HEADS int maxRun = 0; // length of the maximum run so far int countH; Coin coin1= new Coin(); // Create a coin object coin1.flip(); // Flip the coin FLIPS times, Since flip methods returns "void", no parameters. for (int i = 0; i < FLIPS; i++) { currentRun=i; coin1.flip();// Flip the coin & print the result System.out.println(coin1); System.out.println("Current run is: " + currentRun);// Update the run information } // Print the results System.out.println( ); } }