Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Please HELP! my code is wrong somewhere.

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.
    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.
    Last edited by mcpanthers22; September 17th, 2012 at 09:59 AM. Reason: coding


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please HELP! my code is wrong somewhere.

    Please use [code=java] before your code and [/code] after your code.

    void flip() {
    flip();
     
    randomNum = (int)Math.random();
     
    }
    Does this method call itself over and over? What did you intend to do here?

    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?

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default 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.

    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.

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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() {


    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();
    		}
     
    	}

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    mcpanthers22 (September 19th, 2012)

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  8. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

    /*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);
     
     
    	}
     
    }

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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?

  10. #9
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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

  11. The Following User Says Thank You to C++kingKnowledge For This Useful Post:

    mcpanthers22 (September 19th, 2012)

  12. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

     
    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);
     
     
    	}
     
     
    }

  13. #11
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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

  14. #12
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please HELP! my code is wrong somewhere.

    thanks guys for the help! how do you change this to solved?

  15. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please HELP! my code is wrong somewhere.

    Quote Originally Posted by mcpanthers22 View Post
    thanks guys for the help! how do you change this to solved?
    Under Thread Tools near the top of the page

Similar Threads

  1. what's wrong with my code?
    By vanitha in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 1st, 2012, 07:23 AM
  2. What is wrong with this code?
    By dannyboi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 24th, 2012, 12:31 AM
  3. what's wrong with this code
    By gcsekhar in forum Java Networking
    Replies: 5
    Last Post: July 21st, 2011, 06:01 AM
  4. What is Wrong with my code
    By xew123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:59 AM
  5. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM