473,472 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with runs program

13 New Member
I figured out how to make a coin class. But i can't figure out how to write a program to find the length of the longest run of heads in 100 flips of the coin.
This is what i did so far the runs program. Thanks

// this is the coin class
import java.util.Random;
public class Coin
{
private int face;
private final int HEADS=0;
private final int TAILS=1;
public Coin()
{
flip();
}
public void flip()
{
Random gen=new Random();
face=gen.nextInt(2);
}
public boolean isHeads()
{
return(face==HEADS);
}
public int getFace ()
{
return face;
}
public String toString()
{
String facename;
if(face==TAILS)
facename="Tails";
else
facename="Heads";
return facename;
}
}


//************************************************** ******************
// Runs.java
//
// Finds the length of the longest run of heads in 100 flips of a coin.
// ************************************************** ******************
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

// Create a coin object
Coin myCoin = new Coin();

//this is the part i don't know how to do but tried something

// Flip the coin FLIPS times
for (int i = 0; i < FLIPS; i++)
{
// Flip the coin & print the result
myCoin.flip();
System.out.println(myCoin);

// Update the run information
if (myCoin.getFace() == 0)
{
currentRun = currentRun + 1;
}
else
{
if (currentRun > maxRun)
maxRun = currentRun;

currentRun = 0;
}
}

// Print the results
System.out.println("The longest run of heads is: " + maxRun);
}
}
Dec 9 '06 #1
4 3910
DeMan
1,806 Top Contributor
You seem to be counting the runs correctly, what is the output when you run it, does it give you a head tail sequence and then say max heads was 0.....?
Dec 10 '06 #2
sallyk57
13 New Member
----jGRASP exec: java Runs

Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
The maxmimum run is: 0
----jGRASP: operation complete.
Dec 10 '06 #3
DeMan
1,806 Top Contributor
I have tried it on my computer (java 1.5.0_09) and it work no worries. (I copy and pasted your code, so there is no problem with that....). You might like to try it on another machine....
Dec 10 '06 #4
sallyk57
13 New Member
it works thank you
Dec 10 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stephen Ferg | last post by:
I'm trying to use telnetlib to run a Java program on a remote server. I'm having strange problems, and I'm wondering if anyone might be able to help. I have two UNIX servers, A and Z. I have...
5
by: Matthew Speed | last post by:
(About me: I know very little about writing server applications. I have done plenty of VB6 desktop app work but this is my first server program. I got it to work by modifying examples. I...
4
by: Jason Hunt | last post by:
I'm hoping someone can help me out with using the Visual Studio IDE for debugging. I'm working on a Windows Forms application (it's an MDI if it matters), and I'm trying to fix a bug in my...
4
by: robinsand | last post by:
My apologies to those of you who are more advanced Visual C++ .NET programmers, but I am working on a project for an MBA course that is condensed into an eight-week schedule, and I need help...
2
by: kingsolomon2000 | last post by:
Hi! Can anyone please show me an example how to write a very simple C program such that it runs on a specific processor? I want to run a very simple program, 2 programs identical, and one runs...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
41
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that...
1
by: astrogirl77 | last post by:
I'm new to C++ and am hoping to find help with coding a simple C program, am wanting to obtain code and functioning exe's. I code in an old version of Visual Basic 4.0, I have a simple app that...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.