DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Nov 2005
Posts
1

I need help.

I'm new to Java and I'm taking a course in it. The book helps with some stuff but for this question I'm completely lost. Any help would be appreciated.

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
// **********************************************************

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;

else
faceName = "Tails";

return faceName;
}
}

// **********************************************************
// Runs.java
// **********************************************************

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

}
}

2. Registered User
Join Date
Aug 2003
Posts
313
It sounds like your main method should look somthing like this:
Code:
```// This will be the number of times you flip the coin
static int FLIP_TIMES = 100;
public static void main(String[] argv) {
int totalFlips = 0; // total number of flips
int curHeads = 0; // the number of heads seen in a row
int maxHeads = 0; // the maximum number of heads seen in a row.

Coin c = new Coin();

for(int i = 0; i < FLIP_TIMES; i++) {
totalFlips++;
c.flip();
int face = c.getFace();
if( face == Coin.HEAD ) {
} else {
}
}
}```
Hope this helps, if you have any quesitons about how it works, post them.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise