Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

could someone fix this program for me please?

  Asked By: Gene    Date: Jan 27    Category: Java    Views: 5711
  

I have this program that was written in C++ and now I kinda
tried to change it to Java but I don't really know much about it. So
could someone modify it for me plz?

here is the question: write a java program taht will simulate 500,000
coin tosses. Determine if the toss was a head or tails. Count the
number of times each comes up. print results. Also determine how much
or what the error is. Formula is:

Error = absolute_value(500000-number_of_heads)/500000*100

Use a stand alone application.

And here is what I came up with -yea i know it's all wrong :(


import java.io.*;

public class prog{

public static void main(String args[]) throws IOException
{

String name;
int heads;
int tails;
double error;
{
int tmp = rand() % 2;
if(tmp == 0)
heads++;
else
tails++;

int heads=0, tails=0;


srand( (unsigned)time( NULL ) );

for(int i=0;i<500000;i++)
toss(heads, tails);

error = double (abs(500000 - heads)) / 50000000;

System.out.println("Number of heads is: " +heads);
System.out.println("Number of tails is: " +tails);
System.out.println("Error: " +error);


return 0;
}
}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Funsani Chalthoum     Answered On: Jan 27

I will assist you to understand some Java. You will have to do some
writing on your own to get it to work. There are lots of similarities
to C++ but that only gets you close. I've numbered the program  lines
above, skipping spaces. I'll refer to these and give you my comments.
You'll have to interpret and modify  the program on your machine.

Line 1. You aren't doing anything with the io API. You don't need the
import statement. Ditto with the throws portion of the main method
declaration (3). You are not throwing anything in this module. You musta copied
that from somewhere else? Get rid of it. Get rid of lines 6 & 7, too. What were
you thinking? You declared heads and tails twice. You'll get compile errors all
day! Line 15 is a better declaration, anyway.

Line 9. Where did this brace come from? In Java, these things rarely
float by themselves. You'll see this with a static method declaration
but you aren't doing that here. Find it's closing mate and get rid of
both of them.

Line 10. First, I'd make tmp a double, simply because it will be less
messy when you start making the random method call. Don't call rand()
here. Just declare the variable. Then make a new line like 10, only
like this: tmp = Math.random(); and stick it after line 17.

The Math API is static, so you don't need to make any declarations
for it, just use it. Math.random() returns a double, which is why tmp
is a double (otherwise you get a warning message about losing
precision). random() returns a value of 0.0 up to but not including
1.0. On line 11, I recommend you change  the test to: tmp < .5 so that
half of all random() returns will be less than .5 and become heads.
Then take lines 11 through 14 and place them after the line you
already placed after line 17.

Line 16 is not Java. Dump it. You are using the Math API and it
handles seeding the random numbers. Read about it in the JavaDoc
http://java.sun.com/j2se/1.4/docs/api/index.html if you need the
details.

Line 17 is missing the brace at the end of the line. This and the
missing end brace which goes before line 19, define the scope of the
for statement. Line 18 looks like an attempt to call a method that is
not defined. The lines you moved from 11-14 do the job so you can get
rid of 18. If you have to have a method, you will have to change the
scope of your variables. Right now they are all local to the main()
method. Get it working first, then do the refactoring is my
suggestion.

Line 19 is attempting to cast the result to a double. You need to
enclose double in parentheses--> (double). The abs() method lives in
the Math API as well, so code it just like the random() method.

Once you make these changes and compile a few times, you'll have it.
You'll be ready to read the java  tutorial and start coding in Java,
not C++!

 
Didn't find what you were looking for? Find more on could someone fix this program for me please? Or get search suggestion and latest updates.




Tagged: