ch4 - Sheet1 Page 1

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Sheet1 Page 1 //******************************************************************** // CountFlips.java Author: Lewis/Loftus // // Demonstrates the use of a programmer-defined class. //******************************************************************** public class CountFlips { //----------------------------------------------------------------- // Flips a coin multiple times and counts the number of heads // and tails that result. //----------------------------------------------------------------- public static void main (String args) { final int NUM_FLIPS = 1000 int heads = 0, tails = 0 Coin myCoin = new Coin() for (int count=1 { myCoin.flip() if (myCoin.isHeads()) heads++ else tails++ } System.out.println ("The number flips: " + NUM_FLIPS) System.out.println ("The number of heads: " + heads) System.out.println ("The number of tails: " + tails) } } //******************************************************************** // Coin.java Author: Lewis/Loftus // // Represents a coin with two sides that can be flipped. //******************************************************************** import java.util.Random public class Coin { private final int HEADS = 0 private final int TAILS = 1 private int face //----------------------------------------------------------------- // Sets up the coin by flipping it initially. Sheet1 Page 2 //----------------------------------------------------------------- public Coin () { flip() } //----------------------------------------------------------------- // Flips the coin by randomly choosing a face value. //----------------------------------------------------------------- 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 } } //******************************************************************** // FlipRace.java Author: Lewis/Loftus // // Demonstrates the existence of separate data space in multiple // instantiations of a programmer-defined class. //******************************************************************** public class FlipRace { //----------------------------------------------------------------- // Flips two coins until one of them comes up heads three times // in a row....
View Full Document

Page1 / 36

ch4 - Sheet1 Page 1

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online