SlideShare a Scribd company logo
Using the Coin Class
The Coin class from Listing 5.4 in the text is in the file Coin.java. Copy it to your directory, then
write a program to find the length of the longest run of heads in 100 flips of the coin. A skeleton
of the program is in the file Runs.java. To use the Coin class you need to do the following in the
program:
1. Create a coin object. 2. Inside the loop, you should use the flip method to flip the coin, the
toString method (used implicitly) to print the results of the flip, and the getFace method to see if
the result was HEADS. Keeping track of the current run length (the number of times in a row
that the coin was HEADS) and the maximum run length is an exercise in loop techniques! 3.
Print the result after the loop.
// ******************************************************************* //
Coin.java Author: Lewis and Loftus // // Represents a coin with two sides that can be flipped. //
*******************************************************************
public 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 the current face of the coin as an
integer. // ----------------------------------------------------- public int getFace() { return face; }
// ---------------------------------------------------- // Returns the current face of the coin as a string. //
---------------------------------------------------- public String toString() { String faceName;
if (face == HEADS) faceName = "Heads";
Chapter 5: Conditionals and Loops 87
else faceName = "Tails";
return faceName; } }
// ******************************************************************** //
Runs.java // // Finds the length of the longest run of heads in 100 flips of a coin. //
********************************************************************
import java.util.Scanner;
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
Scanner scan = new Scanner(System.in);
// Create a coin object
// Flip the coin FLIPS times for (int i = 0; i < FLIPS; i++) { // Flip the coin & print the result
// Update the run information
}
// Print the results
} }
Solution
// *******************************************************************
// Coin.java
// Represents a coin with two sides that can be flipped.
// *******************************************************************
public 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 the current face of the coin as an integer.
// -----------------------------------------------------
public int getFace() { return face; }
// ----------------------------------------------------
// 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;
}
}
// ********************************************************************
// Runs.java
// Finds the length of the longest run of heads in 100 flips of a coin.
// ********************************************************************
import java.util.Scanner;
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
Scanner scan = new Scanner(System.in);
// Create a coin object
Coin c = new Coin();
int face;
// Flip the coin FLIPS times
for (int i = 0; i < FLIPS; i++)
{
// Flip the coin & print the result
c.flip();
face = c.getFace();
System.out.println("Flip " + (i+1) +": " + c.toString());
if(face == 0)
currentRun++;
else
{
// Update the run information
if(currentRun > maxRun)
maxRun = currentRun;
currentRun = 0;
}
}
// Print the results
System.out.println("Maximum run of heads in a row: " + maxRun);
}
}
/*
OUTPUT:
Flip 1: Heads
Flip 2: Heads
Flip 3: Heads
Flip 4: Heads
Flip 5: Tails
Flip 6: Heads
Flip 7: Tails
Flip 8: Heads
Flip 9: Heads
Flip 10: Heads
Flip 11: Tails
Flip 12: Tails
Flip 13: Heads
Flip 14: Tails
Flip 15: Heads
Flip 16: Heads
Flip 17: Tails
Flip 18: Tails
Flip 19: Heads
Flip 20: Heads
Flip 21: Heads
Flip 22: Tails
Flip 23: Tails
Flip 24: Heads
Flip 25: Tails
Flip 26: Tails
Flip 27: Heads
Flip 28: Tails
Flip 29: Tails
Flip 30: Tails
Flip 31: Heads
Flip 32: Tails
Flip 33: Tails
Flip 34: Heads
Flip 35: Tails
Flip 36: Heads
Flip 37: Heads
Flip 38: Heads
Flip 39: Tails
Flip 40: Heads
Flip 41: Tails
Flip 42: Heads
Flip 43: Tails
Flip 44: Tails
Flip 45: Heads
Flip 46: Tails
Flip 47: Heads
Flip 48: Heads
Flip 49: Heads
Flip 50: Tails
Flip 51: Tails
Flip 52: Heads
Flip 53: Heads
Flip 54: Heads
Flip 55: Tails
Flip 56: Heads
Flip 57: Tails
Flip 58: Heads
Flip 59: Heads
Flip 60: Heads
Flip 61: Heads
Flip 62: Heads
Flip 63: Tails
Flip 64: Heads
Flip 65: Heads
Flip 66: Tails
Flip 67: Heads
Flip 68: Tails
Flip 69: Heads
Flip 70: Heads
Flip 71: Tails
Flip 72: Heads
Flip 73: Heads
Flip 74: Heads
Flip 75: Tails
Flip 76: Tails
Flip 77: Tails
Flip 78: Heads
Flip 79: Heads
Flip 80: Heads
Flip 81: Tails
Flip 82: Heads
Flip 83: Heads
Flip 84: Tails
Flip 85: Heads
Flip 86: Heads
Flip 87: Heads
Flip 88: Heads
Flip 89: Heads
Flip 90: Tails
Flip 91: Heads
Flip 92: Tails
Flip 93: Tails
Flip 94: Heads
Flip 95: Tails
Flip 96: Tails
Flip 97: Heads
Flip 98: Tails
Flip 99: Tails
Flip 100: Heads
Maximum run of heads in a row: 5
*/

More Related Content

DOC
Quiz using C++
PDF
#include deck.h .pdf
PPTX
Treatment, Architecture and Threads
DOCX
Your task in this assignment is to implement a simplified guessing
PPTX
Cvim half precision floating point
PDF
PPTX
Workshop 1: Good practices in JavaScript
Quiz using C++
#include deck.h .pdf
Treatment, Architecture and Threads
Your task in this assignment is to implement a simplified guessing
Cvim half precision floating point
Workshop 1: Good practices in JavaScript

Similar to Using the Coin Class The Coin class from Listing 5.4 in the text i.pdf (20)

PDF
The Ring programming language version 1.9 book - Part 68 of 210
PPTX
PDF
Marko Gargenta_Remixing android
DOCX
project4-ast.DS_Storeproject4-astast.c#include symbolTa.docx
PDF
Itsecteam shell
PPTX
SQL and PLSQL features for APEX Developers
PDF
Please follow the cod eand comments for description CODE #incl.pdf
PDF
Get into the FLOW with Extbase
DOCX
6Modify the bfs.java program (Listing A) to find the minimu.docx
TXT
PDF
Flex入門
PDF
Di yquadricopter
PPT
computer notes - Data Structures - 14
PDF
The Ring programming language version 1.10 book - Part 16 of 212
DOCX
Lab08Lab08.cppLab08Lab08.cpp.docx
DOCX
i need an input of this program.  anything good or bad.  what could .docx
KEY
Sbaw091110
PDF
Scala Under the Hood / ScalaSwarm
PDF
Andrey Listochkin "Everybody stand back! I know regular expressions"
PPT
Computer notes data structures - 9
The Ring programming language version 1.9 book - Part 68 of 210
Marko Gargenta_Remixing android
project4-ast.DS_Storeproject4-astast.c#include symbolTa.docx
Itsecteam shell
SQL and PLSQL features for APEX Developers
Please follow the cod eand comments for description CODE #incl.pdf
Get into the FLOW with Extbase
6Modify the bfs.java program (Listing A) to find the minimu.docx
Flex入門
Di yquadricopter
computer notes - Data Structures - 14
The Ring programming language version 1.10 book - Part 16 of 212
Lab08Lab08.cppLab08Lab08.cpp.docx
i need an input of this program.  anything good or bad.  what could .docx
Sbaw091110
Scala Under the Hood / ScalaSwarm
Andrey Listochkin "Everybody stand back! I know regular expressions"
Computer notes data structures - 9
Ad

More from anandastores (19)

PDF
You have been made the GIS Manager for “Texas Infrastructures”. The .pdf
PDF
V. A florist has roses, carnations, lilies, and snapdragons in stock.pdf
PDF
utilization of computers and the subsequent increase in gethering of.pdf
PDF
Using two independent samples, we test for a hypothesized difference.pdf
PDF
Using Tichys 3 Types of Change approaches how can this be useful t.pdf
PDF
Using the two-phase method, solve the following LP problem. Then ver.pdf
PDF
Using the text Appendix concerning HofstedeSolutionBahrainPo.pdf
PDF
Using the Shifting property of unit impulse, prove thatSolution.pdf
PDF
Using the OCI instrument online, and the cultural change background .pdf
PDF
Using the Hofstede model, discuss the differences in power distance,.pdf
PDF
Using implicit differentiation, I gotIn order for the tangent line.pdf
PDF
Using the data set below, please calculate the find the median 10.pdf
PDF
Using the data below, I need answers to the following questionsa).pdf
PDF
Using the data set below, please calculate the find the mode 10 1.pdf
PDF
Using the current policy on immigration, analyze the overall manner .pdf
PDF
Using the bottom-up, or participatory approach a. budgets are first .pdf
PDF
Using non-zero whole numbers there is just one combination of two nu.pdf
PDF
Using lists (bullets and numbers) in documentsA. should be avoided.pdf
PDF
Using scientific notation, show that it takes light from the Sun abo.pdf
You have been made the GIS Manager for “Texas Infrastructures”. The .pdf
V. A florist has roses, carnations, lilies, and snapdragons in stock.pdf
utilization of computers and the subsequent increase in gethering of.pdf
Using two independent samples, we test for a hypothesized difference.pdf
Using Tichys 3 Types of Change approaches how can this be useful t.pdf
Using the two-phase method, solve the following LP problem. Then ver.pdf
Using the text Appendix concerning HofstedeSolutionBahrainPo.pdf
Using the Shifting property of unit impulse, prove thatSolution.pdf
Using the OCI instrument online, and the cultural change background .pdf
Using the Hofstede model, discuss the differences in power distance,.pdf
Using implicit differentiation, I gotIn order for the tangent line.pdf
Using the data set below, please calculate the find the median 10.pdf
Using the data below, I need answers to the following questionsa).pdf
Using the data set below, please calculate the find the mode 10 1.pdf
Using the current policy on immigration, analyze the overall manner .pdf
Using the bottom-up, or participatory approach a. budgets are first .pdf
Using non-zero whole numbers there is just one combination of two nu.pdf
Using lists (bullets and numbers) in documentsA. should be avoided.pdf
Using scientific notation, show that it takes light from the Sun abo.pdf
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Classroom Observation Tools for Teachers
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Updated Idioms and Phrasal Verbs in English subject
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Cell Types and Its function , kingdom of life
LDMMIA Reiki Yoga Finals Review Spring Summer
Classroom Observation Tools for Teachers
Microbial disease of the cardiovascular and lymphatic systems
Final Presentation General Medicine 03-08-2024.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Final Presentation General Medicine 03-08-2024.pptx
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Structure & Organelles in detailed.
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Updated Idioms and Phrasal Verbs in English subject
Microbial diseases, their pathogenesis and prophylaxis
Weekly quiz Compilation Jan -July 25.pdf
Paper A Mock Exam 9_ Attempt review.pdf.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
UNIT III MENTAL HEALTH NURSING ASSESSMENT
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3

Using the Coin Class The Coin class from Listing 5.4 in the text i.pdf

  • 1. Using the Coin Class The Coin class from Listing 5.4 in the text is in the file Coin.java. Copy it to your directory, then write a program to find the length of the longest run of heads in 100 flips of the coin. A skeleton of the program is in the file Runs.java. To use the Coin class you need to do the following in the program: 1. Create a coin object. 2. Inside the loop, you should use the flip method to flip the coin, the toString method (used implicitly) to print the results of the flip, and the getFace method to see if the result was HEADS. Keeping track of the current run length (the number of times in a row that the coin was HEADS) and the maximum run length is an exercise in loop techniques! 3. Print the result after the loop. // ******************************************************************* // Coin.java Author: Lewis and Loftus // // Represents a coin with two sides that can be flipped. // ******************************************************************* public 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 the current face of the coin as an integer. // ----------------------------------------------------- public int getFace() { return face; } // ---------------------------------------------------- // Returns the current face of the coin as a string. // ---------------------------------------------------- public String toString() { String faceName; if (face == HEADS) faceName = "Heads"; Chapter 5: Conditionals and Loops 87 else faceName = "Tails"; return faceName; } } // ******************************************************************** // Runs.java // // Finds the length of the longest run of heads in 100 flips of a coin. // ******************************************************************** import java.util.Scanner; 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
  • 2. Scanner scan = new Scanner(System.in); // Create a coin object // Flip the coin FLIPS times for (int i = 0; i < FLIPS; i++) { // Flip the coin & print the result // Update the run information } // Print the results } } Solution // ******************************************************************* // Coin.java // Represents a coin with two sides that can be flipped. // ******************************************************************* public 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 the current face of the coin as an integer. // ----------------------------------------------------- public int getFace() { return face; } // ---------------------------------------------------- // Returns the current face of the coin as a string. // ---------------------------------------------------- public String toString()
  • 3. { String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; } } // ******************************************************************** // Runs.java // Finds the length of the longest run of heads in 100 flips of a coin. // ******************************************************************** import java.util.Scanner; 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 Scanner scan = new Scanner(System.in); // Create a coin object Coin c = new Coin(); int face; // Flip the coin FLIPS times for (int i = 0; i < FLIPS; i++) { // Flip the coin & print the result c.flip(); face = c.getFace(); System.out.println("Flip " + (i+1) +": " + c.toString()); if(face == 0)
  • 4. currentRun++; else { // Update the run information if(currentRun > maxRun) maxRun = currentRun; currentRun = 0; } } // Print the results System.out.println("Maximum run of heads in a row: " + maxRun); } } /* OUTPUT: Flip 1: Heads Flip 2: Heads Flip 3: Heads Flip 4: Heads Flip 5: Tails Flip 6: Heads Flip 7: Tails Flip 8: Heads Flip 9: Heads Flip 10: Heads Flip 11: Tails Flip 12: Tails Flip 13: Heads Flip 14: Tails Flip 15: Heads Flip 16: Heads
  • 5. Flip 17: Tails Flip 18: Tails Flip 19: Heads Flip 20: Heads Flip 21: Heads Flip 22: Tails Flip 23: Tails Flip 24: Heads Flip 25: Tails Flip 26: Tails Flip 27: Heads Flip 28: Tails Flip 29: Tails Flip 30: Tails Flip 31: Heads Flip 32: Tails Flip 33: Tails Flip 34: Heads Flip 35: Tails Flip 36: Heads Flip 37: Heads Flip 38: Heads Flip 39: Tails Flip 40: Heads Flip 41: Tails Flip 42: Heads Flip 43: Tails Flip 44: Tails Flip 45: Heads Flip 46: Tails Flip 47: Heads Flip 48: Heads Flip 49: Heads Flip 50: Tails Flip 51: Tails Flip 52: Heads
  • 6. Flip 53: Heads Flip 54: Heads Flip 55: Tails Flip 56: Heads Flip 57: Tails Flip 58: Heads Flip 59: Heads Flip 60: Heads Flip 61: Heads Flip 62: Heads Flip 63: Tails Flip 64: Heads Flip 65: Heads Flip 66: Tails Flip 67: Heads Flip 68: Tails Flip 69: Heads Flip 70: Heads Flip 71: Tails Flip 72: Heads Flip 73: Heads Flip 74: Heads Flip 75: Tails Flip 76: Tails Flip 77: Tails Flip 78: Heads Flip 79: Heads Flip 80: Heads Flip 81: Tails Flip 82: Heads Flip 83: Heads Flip 84: Tails Flip 85: Heads Flip 86: Heads Flip 87: Heads Flip 88: Heads
  • 7. Flip 89: Heads Flip 90: Tails Flip 91: Heads Flip 92: Tails Flip 93: Tails Flip 94: Heads Flip 95: Tails Flip 96: Tails Flip 97: Heads Flip 98: Tails Flip 99: Tails Flip 100: Heads Maximum run of heads in a row: 5 */