// Program Name
Runs.java
// Course:
CSE 1302J LAB Section 003
// Student Name:
Bradley Shedd
// Assignment Number:
Lab1
// Due Date:
08/25/2010
// Purpose:
This program will calculate the maximum number of times a coin
lands on heads.
//
Next, the program prints the number of times the coin lands on heads
//
consecutively. Finally, the max number of consecutive heads is the
output.
// ********************************************************************
// 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
lastFlip = -1;
// records the last flip
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 newCoin =
new
Coin();
// Flip the
coin FLIPS times
for
(int i = 0; i < FLIPS; i++){
// Flip the
coin & print the result
newCoin.flip();
// Update
the run information
if
(newCoin.getFace() != lastFlip)
System.out.println();
System.out.print(newCoin.toString());
if(newCoin.getFace()
== 0)
currentRun++;
else{
if
(currentRun > maxRun)
maxRun = currentRun;
currentRun=0;
}
lastFlip =
newCoin.getFace();
}
//********Prints the max number of consecutive lands on
heads.**************
System.out.print("\nThe
maximum run length was: " + maxRun);
System.out.print("\nThis
program coded by: Bradley J. Shedd.");
}
}