Please HELP! my code is wrong somewhere.
I am attempting to create a program for class that flips a coin 1000 times and counts the number of heads and tails. I have to use the following methods in this class:
public boolean isHeads(); returns true if heads
void flip();flips the coin. I also have to use Math.random here and im not 100% on this
String toString(); outputs the current face of the coin
here is what i have so far any guidance would be a blessing. Thank you.
Code java:
public class Coin {
int TAILS;
int HEADS;
double randomNum;
void flip() {
flip();
randomNum = (int)Math.random();
}
public boolean isHeads() {
return (randomNum == HEADS);
}
public String toString() {
String faceName;
if (randomNum == HEADS)
faceName = "HEADS";
else
faceName = "TAILS";
return faceName;
}
public static void main(String[] args) {
Coin myCoin = new Coin();
int h = 0;
int t = 0;
for(int flips = 0; flips < 1000; flips++)
{
if (myCoin.flip < 0.5)
h++;
else
t++;
}
}
}
my errors are with the myCoin.flip because the "." im not sure what to put as my if statement. i believe i have confused myself.
Re: Please HELP! my code is wrong somewhere.
Please use [code=java] before your code and [/code] after your code.
Code java:
void flip() {
flip();
randomNum = (int)Math.random();
}
Does this method call itself over and over? What did you intend to do here?
Quote:
my errors are with the myCoin.flip because the "." im not sure what to put as my if statement. i believe i have confused myself.
What do the error messages say? What line number does it say to look at?
Re: Please HELP! my code is wrong somewhere.
You're error is as you said the myCoin.flip. You have to put myCoin.flip() and also change the flip method return type to double.
Code java:
for(int flips = 0; flips < 1000; flips++)
{
if (myCoin.flip() < 0.5)
h++;
else
t++;
}
double flip() {
return randomNum = Math.random();
}
And i also wonder why you call the flip() method inside the flip() method. That would get stuck in an endless loop.
Re: Please HELP! my code is wrong somewhere.
I wasnt really sure where to put it to be honest, but the void flip() method is supposed to flip the coin using Math.random.....Do i need to input my if statements here and do a function call into my main? Is that even possible?
My error now is Coin.java:57: error: ';' expected
double flip() {
Code java:
public class Coin {
int TAILS;
int HEADS;
double randomNum;
void flip() {
randomNum = (int)Math.random();
}
public boolean isHeads() {
return (randomNum == HEADS);
}
public String toString() {
String faceName;
if (randomNum == HEADS)
faceName = "HEADS";
else
faceName = "TAILS";
return faceName;
}
public static void main(String[] args) {
Coin myCoin = new Coin();
int h = 0;
int t = 0;
for(int flips = 0; flips < 1000; flips++)
{
if (myCoin.flip < 0.5)
h++;
else
t++;
}
double flip() {
return randomNum = Math.random();
}
}
Re: Please HELP! my code is wrong somewhere.
A method can not be defined inside of another method. Make sure there is a } ending the last method before defining the next method. About line 56.
Re: Please HELP! my code is wrong somewhere.
Proper indentation helps errors like this stand out, not to mention it keeps the code easier to read. See how far:
double flip() {
is tabbed in, it should line up with other methods within the class.
Re: Please HELP! my code is wrong somewhere.
Yea I have changed the code a little to be able to use the boolean more but im not sure on my main set up. I know its wrong, but im not 100% sure how to use methods that are outside the main in the actual main. here is what i have a little more updated after getting hints from my instructor.
Code java:
/*Write a Java program that simulates the flipping of a coin 1000 times and prints the total number of heads and tails.
You must create a class called Coin with the following public methods:
void flip(): flips the coin (Hint: Math.random() generates a random floating-point number between 0 and 1.)
boolean isHeads(): returns true if the coin shows heads
String toString(): returns the current face of the coin as a string*/
public class Coin {
boolean heads;
public void flip() {
double randomNum = Math.random();
if (randomNum < 0.5) {
heads = true;
}
else {
heads = false;
}
}
public boolean isHeads() {
return (heads);
}
public String toString() {
String faceName;
if (heads == true)
faceName = "HEADS";
else
faceName = "TAILS";
return faceName;
}
public static void main(String[] args) {
Coin myCoin = new Coin();
int h = 0;
int t = 0;
for(int flips = 0; flips < 1000; flips++)
{
if (isHeads == true) {
h++;
}
else {
t++;
}
}
System.out.println(h);
System.out.println(t);
}
}
Re: Please HELP! my code is wrong somewhere.
What is your question now?
What is not working the way you expected? ...and what is it doing instead?
Re: Please HELP! my code is wrong somewhere.
your flip method is okay... how can isHeads() return a result if the method that makes the random flip is never called? all your isHeads() is doing is returning the initially declared heads at the top not the result from flip().... your public String toString() is not a good idea for a declaration because there is a toString() method that is predefined already so just change the name to something like public displayResult() and make it void.... in there put a print statement to show the faceName result
Re: Please HELP! my code is wrong somewhere.
Okay i got it to work finally lol I had to call my flip method in my main right before the boolean. here is what i ended up with and it does it every time. The String toString method really doesnt have to do anything King. My instructor just wants to make us think and do a little extra work because there are so many examples of this online.
Code java:
import java.util.Random;
public class Coin {
boolean heads;
public void flip() {
double randomNum = Math.random();
if (randomNum < 0.5) {
heads = true;
}
else {
heads = false;
}
}
public boolean isHeads() {
return (heads);
}
public String toString() {
String faceName;
if (heads == true)
faceName = "HEADS";
else
faceName = "TAILS";
return faceName;
}
public static void main(String[] args) {
Coin myCoin = new Coin();
int headCount = 0;
int tailCount = 0;
for(int flips = 0; flips < 1000; flips++)
{
myCoin.flip();
if(myCoin.isHeads())
headCount++;
else
tailCount++;
}
System.out.println(headCount);
System.out.println(tailCount);
}
}
Re: Please HELP! my code is wrong somewhere.
yeah because i was wondering why u were not using it anywhere but that's great that you got it to work
Re: Please HELP! my code is wrong somewhere.
thanks guys for the help! how do you change this to solved?
Re: Please HELP! my code is wrong somewhere.
Quote:
Originally Posted by
mcpanthers22
thanks guys for the help! how do you change this to solved?
Under Thread Tools near the top of the page