473,944 Members | 17,311 Online
Bytes | Software Development & Data Engineering Community
+ 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.Rando m;
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.nextIn t(2);
}
public boolean isHeads()
{
return(face==HE ADS);
}
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.prin tln(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.prin tln("The longest run of heads is: " + maxRun);
}
}
Dec 9 '06 #1
4 3933
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
1909
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 an application named LATTE that runs on server A. When the user starts LATTE, the user can choose to create a telnet connection back to A, or to Z. Then LATTE uses the telnet connection to run a Java program (run it several times, on different...
5
1550
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 understand what it is doing but not much about what is involved in extending it. ) I am not necessarily looking for code examples here, just some pointers as to what direction I need to proceed to resolve these things. I have written a...
4
1538
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 program. What's happening is when the program is running, it runs into an exception error and switches back to the Visio Studio screen. The problem is that it's not showing me the line of code which actually caused the exception. Instead it is...
4
2232
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 getting a program up and running with proper files and documentation to be handed in for a grade (on Microsoft Visual Studio .NET 2003). I am being graded on how well I incorporate advanced C++ features such as inheritance, polymorphic programming,...
2
1413
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 one one processor and the other program (same one) runs on a different processor. How do I specify which processor in the code to force the program to
12
3045
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 the instructions and couldnt get the correct results. heres the code stuff... temperature=input("what is the temperature of the spam?") if temperature>50: print "the salad is properly cooked." else:
41
2739
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 will calculate the factorial of 10, 10 millions time and print the reusult in a file with the name log.txt.. I wrote something like this
1
2086
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 is about 3 and a half pages of code long it does some relatively simple math additions and subtractions The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is...
0
9974
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11140
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10679
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9871
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8239
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7402
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4519
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3523
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.